用imagestring怎么使输出位置随机?
<?php
//创建画布
$im=imagecreatetruecolor(600, 300);
$width=imagesx($im);
$hight=imagesy($im);
// 颜色随机
$color=imagecolorallocate($im, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
// 填充颜色
$fill=imagefill($im,0,0,$color);
$a=strtoupper('abcdefghijklmnopqrstuvwxyz');
$string='abcdefghijklmnopqrstuvwxyz1234567890'.$a;
for ($i=0; $i <4 ; $i++) {
// 颜色随机
$strcolor=imagecolorallocate($im, mt_rand(0,150), mt_rand(0,150), mt_rand(0,150));
//字符随机
$str=$string[mt_rand(0,strlen($string)-1)];
// 写入字符
imagestring($im, 5, 100+$i*20,100,$str, $strcolor);
}
header('content-type:image/jpeg');
imagejpeg($im,null,75);
正在回答
您好,imagestring函数的第三个,第四个参数设置字符位置。但由于函数写在循环中所以设置位置之后每个字符的位置都不同,所以可以将字符串内容拼接之后再输出。参考如下:
<?php //创建画布 $im=imagecreatetruecolor(600, 300); $width=imagesx($im); $hight=imagesy($im); // 颜色随机 $color=imagecolorallocate($im, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255)); // 填充颜色 $fill=imagefill($im,0,0,$color); $a=strtoupper('abcdefghijklmnopqrstuvwxyz'); $string='abcdefghijklmnopqrstuvwxyz1234567890'.$a; $str = ''; for ($i=0; $i <4 ; $i++) { // 颜色随机 $strcolor=imagecolorallocate($im, mt_rand(0,150), mt_rand(0,150), mt_rand(0,150)); //字符随机 $str.=$string[mt_rand(0,strlen($string)-1)]; } // 写入字符 imagestring($im, 5, mt_rand(10,500),mt_rand(10,280),$str, $strcolor); header('content-type:image/jpeg'); imagejpeg($im,null,75);
祝学习愉快!
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星