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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <?php
function createImage($word,$imagePath,$type,$imageName){
$fontPath = 'msyh.ttc'; $fontSize = 20 * 0.75; foreach ($word as $v ) { $fontarea = imagettfbbox($fontSize, 0, $fontPath, $v); $textWidth = $fontarea[2] - $fontarea[0]; $textHeight = $fontarea[1] - $fontarea[7]; $tmp['text'] = $v; $tmp['size'] = $fontSize; $tmp['width'] = $textWidth; $tmp['height'] = $textHeight; $textArr[] = $tmp; } list($imageWidth, $imageHeight, $imageType) = getimagesize($imagePath);
for($i=0;$i<count($textArr);$i++){ list($x, $y) = randPosition($textArr, $imageWidth, $imageHeight, $textArr[$i]['width'], $textArr[$i]['height'],$i,$type); $textArr[$i]['x'] = $x; $textArr[$i]['y'] = $y; } unset($v); $image = imagecreatefromstring(file_get_contents($imagePath)); $color = imagecolorallocate($image, 0, 0, 0); foreach($textArr as $v){ imagefttext($image, $v['size'], 0, $v['x'], $v['y'], $color, $fontPath, $v['text']); }
if(imagepng($image,$imageName)){ echo $imageName."\n"; }
}
function randPosition($textArr, $imgW, $imgH, $fontW, $fontH,$i,$type){ switch ($type) { case 0: $x = rand($i*60, ($i+1)*60-$fontW-3); $y = rand(40,80); break; case 1: $x = ($i)*25+5; $y = 25; default: break; } $return = array($x, $y); return $return; }
$ap_imagePath = 'ap_bg.png'; $mp_imagePath = 'mp_bg.png'; $ap_imageName = "ap_".time().".png"; $mp_imageName = "mp_".time().".png"; $ap_word = array('请','依','次','点','击','图','中','的','猎', '户','室') ; $mp_word = array('猎', '户', '实','验','室'); createImage($ap_word,$ap_imagePath,1,$ap_imageName); createImage($mp_word,$mp_imagePath,0,$mp_imageName); ?>
|