慕神9130837
2020-09-15 18:26
<?php
$width = 200;
$height = 40;
$font_file = 'fonts/mini.ttf';
$image = imagecreatetruecolor($width,$height);
$white = imagecolorallocate($image,255,255,255);
function get_color($image){
$rand_color = imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
return $rand_color;
}
imagefilledrectangle($image,0,0,$width,$height,$white);
$string = join('',array_merge(range(0,9),range('a','z'),range('A','Z')));
$length = 4;
for($i = 0;$i < $length;$i++){
$angle = mt_rand(-15,15);
$fontsize = mt_rand(20,28);
$text = str_shuffle($string)[0];
//get font width and font height
$font_width = imagefontwidth($fontsize);
$font_height = imagefontheight($fontsize);
$x = 20 + ($width - 20 * 2) / 3 * $i;
$y = 40 - ((40 - $font_height) / 2);
$rand_color = get_color($image);
imagettftext($image,$fontsize,$angle,$x,$y,$rand_color,$font_file,$text);
}
//add interference of pixel
for($i = 0; $i < 50; $i++){
imagesetpixel($image, mt_rand(0,$width),mt_rand(0,$height),get_color($image));
}
//add interference of line
for($i = 0; $i < 3; $i++){
$x1 = mt_rand(0,$width);
$y1 = mt_rand(0,$height);
$x2 = mt_rand(0,$width);
$y2 = mt_rand(0,$height);
while(1){
if($x1 == $x2 and $y1 == $y2){
$x1 = mt_rand(0,$width);
$y1 = mt_rand(0,$height);
$x2 = mt_rand(0,$width);
$y2 = mt_rand(0,$height);
}
else{
break;
}
}
imageline($image,$x1,$y1,$x2,$y2,get_color($image));
}
//add interference of arc
for($i = 0; $i < 3; $i++){
$cx = mt_rand(0, $width);
$cy = mt_rand(0, $height);
$w = mt_rand(0, min($cx, $width - $cx));
$h = mt_rand(0, min($cy, $height - $cy));
$s = mt_rand(0, 160);
$e = mt_rand(0, 160);
imagearc($image, $cx, $cy, $w, $h, $s, $e, get_color($image));
}
header('content-type:image/png');
imagepng($image);
imagedestroy($image);<pre class="brush:php;toolbar:false"><?php
$width = 200;
$height = 40;
$font_file = 'fonts/mini.ttf';
$image = imagecreatetruecolor($width,$height);
$white = imagecolorallocate($image,255,255,255);
function get_color($image){
$rand_color = imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
return $rand_color;
}
imagefilledrectangle($image,0,0,$width,$height,$white);
$string = join('',array_merge(range(0,9),range('a','z'),range('A','Z')));
$length = 4;
for($i = 0;$i < $length;$i++){
$angle = mt_rand(-15,15);
$fontsize = mt_rand(20,28);
$text = str_shuffle($string)[0];
//get font width and font height
$font_width = imagefontwidth($fontsize);
$font_height = imagefontheight($fontsize);
$x = 20 + ($width - 20 * 2) / 3 * $i;
$y = 40 - ((40 - $font_height) / 2);
$rand_color = get_color($image);
imagettftext($image,$fontsize,$angle,$x,$y,$rand_color,$font_file,$text);
}
//add interference of pixel
for($i = 0; $i < 50; $i++){
imagesetpixel($image, mt_rand(0,$width),mt_rand(0,$height),get_color($image));
}
//add interference of line
for($i = 0; $i < 3; $i++){
$x1 = mt_rand(0,$width);
$y1 = mt_rand(0,$height);
$x2 = mt_rand(0,$width);
$y2 = mt_rand(0,$height);
while(1){
if($x1 == $x2 and $y1 == $y2){
$x1 = mt_rand(0,$width);
$y1 = mt_rand(0,$height);
$x2 = mt_rand(0,$width);
$y2 = mt_rand(0,$height);
}
else{
break;
}
}
imageline($image,$x1,$y1,$x2,$y2,get_color($image));
}
//add interference of arc
for($i = 0; $i < 3; $i++){
$cx = mt_rand(0, $width);
$cy = mt_rand(0, $height);
$w = mt_rand(0, min($cx, $width - $cx));
$h = mt_rand(0, min($cy, $height - $cy));
$s = mt_rand(0, 160);
$e = mt_rand(0, 160);
imagearc($image, $cx, $cy, $w, $h, $s, $e, get_color($image));
}
header('content-type:image/png');
imagepng($image);
imagedestroy($image);</pre><p><br /></p>
加油,加油
PHP进阶篇-GD库图像处理
10440 学习 · 72 问题
相似问题