-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_code.php
47 lines (41 loc) · 1.73 KB
/
image_code.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
session_start();
header("Content-type: image/jpeg");
$string = $_REQUEST['txt'];
$imageArr = array('image_code_blue.jpg', 'image_code_green.jpg', 'image_code_orange.jpg');
$image = imagecreatefromjpeg("images/".$imageArr[rand()%3]);
//$color = imagecolorallocate($image, 220, 210, 60);
$color = imagecolorallocate($image, 255, 255, 255);
ImageStringAlignAndWrap($image, 17, $string, $color, 105, "center");
imagejpeg($image);
imagedestroy($image);
function ImageStringAlignAndWrap($image, $font, $text, $color, $maxwidth, $alignment){
$fonFamily = array('./imagefont/verdana.ttf', './imagefont/trebucbi.ttf', './imagefont/trebuc.ttf', './imagefont/tahomabd.ttf', './imagefont/tahoma.ttf', './imagefont/cour.ttf', './imagefont/comicbd.ttf', './imagefont/comic.ttf', './imagefont/arialbd.ttf', './imagefont/arial.ttf');
$fontwidth = imagefontwidth($font);
$fontheight = imagefontheight($font);
if ($maxwidth != NULL) {
$maxcharsperline = floor($maxwidth / $fontwidth);
$text = wordwrap($text, $maxcharsperline, "\n", 1);
}
$lines = explode("\n", $text);
$y = 7;
if ($alignment == "right") {
while (list($numl, $line) = each($lines)) {
imagestring($image, $font, imagesx($image) - $fontwidth*strlen($line), $y, $line, $color);
$y += $fontheight;
}
}
elseif ($alignment == "center") {
while (list($numl, $line) = each($lines)) {
//imagestring($image, $font, floor((imagesx($image) - $fontwidth*strlen($line))/2), $y, $line, $color);
imagettftext ($image, $font, 0, 5, 19, $color, $fonFamily[(rand()%10)], $line);
$y += $fontheight;
}
} else {
while (list($numl, $line) = each($lines)) {
imagestring($image, $font, 0, $y, $line, $color);
$y += $fontheight;
}
}
}
?>