版权声明:转载请附上文章地址 https://blog.csdn.net/weixin_38134491/article/details/85476889
<?php
header('Content-type:image/jpeg');
$width=120;
$height=40;
$element=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$string='';//变量初始化
for($i=0;$i<5;$i++){
$string.=$element[rand(0,count($element)-1)];
}
$img=imagecreatetruecolor($width,$height);
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255)); //rand()是随机取值函数
$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorPoint=imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200));
$colorLine=imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200));
$colorString=imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200));
imagefill($img,0,0,$colorBg);
imagerectangle($img, 0, 0, $width-1, $height-1, $colorBorder); //画个矩形,从左上角到右下角
for($i=0;$i<100;$i++){
imagesetpixel($img, rand(0,$width-1), rand(0,$height-1), $colorPoint);//画点
}
//画线
for($i=0;$i<3;$i++){
imageline($img,rand(0,$width/2),rand(0,$width/2),rand($width/2,$height),rand($height/2,$height),$colorLine);
}
//写字母
/* imagestring($img, 5, 0, 0, 'abcd', $colorString); *///5是字体
imagettftext($img, 18, rand(-5,5), rand(5,15), rand(20,35), $colorString, 'font/Elements.ttf', $string);//http://www.font5.com.cn/tag.php?tag=ttf%B8%F1%CA%BD
imagejpeg($img);
imagedestroy($img); //释放资源