博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php使用imagettftext()函数有干扰线但是没有文字的问题解决
阅读量:4582 次
发布时间:2019-06-09

本文共 2366 字,大约阅读时间需要 7 分钟。

public function code()    {        //主要参数        if($font_size == 0) $font_size = 20;        if($img_width == 0) $img_width = 110;        if($img_height == 0) $img_height = 50;        if($word_type == 0) $word_type = 3;   // 1:数字 2:英文 3:混合        $font_file  = 'E:\phpStudy\PHPTutorial\WWW\thinkphp_3.2.3_full\Application\Home\Controller\SIDESHOW.TTF';//字体的路径                //创建图片,并设置背景色        $im = @imagecreate($img_width, $img_height);        imagecolorallocate($im, 192,192,192);                //获取随机字符        if($word_type == 1) {            $verifyCode = implode('', range(2, 9));        }elseif ($word_type == 2) {            $verifyCode = implode('', range('A', 'Z'));        }else{            $verifyCode = implode('', array_merge(range(2, 9),range('A', 'Z')));            $verifyCode = str_replace(array('I','O'), array('P','N'), $verifyCode);        }        //打乱字符串        $verifyCode = str_shuffle($verifyCode);        $rndstring = substr($verifyCode,0,4);        //echo $rndstring;exit;                $rndcodelen = strlen($rndstring);                //干扰线        for($i = 0; $i < 5; $i++) {            $color = imagecolorallocate($im, 0, 0, 0);            imageline($im, rand(0, $img_width), rand(0, $img_height), rand(0, $img_width), rand(0, $img_height), $color);        }                //画边框        //$bordercolor = imagecolorallocate($im, 0, 0, 0);        //imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $bordercolor);                //输出文字        for($i = 0;$i < $rndcodelen;$i++){            $rndstring[$i] = strtoupper($rndstring[$i]);            $c_fontColor = imagecolorallocate($im, 0, 0, 0);            $y_pos = $i == 0 ? 10 : $i * ($font_size + 8);            $c = mt_rand(0, 15);            imagettftext($im, $font_size, $c, $y_pos, 35, $c_fontColor, $font_file, $rndstring[$i]);        }        header("Pragma:no-cache\r\n");        header("Cache-Control:no-cache\r\n");        header("Expires:0\r\n");        if(function_exists("imagejpeg")){            header("content-type:image/jpeg\r\n");            imagejpeg($im);        }else{            header("content-type:image/png\r\n");            imagepng($im);        }        imagedestroy($im);        exit();    }

生成的是这样的,原因在于字体的路径加载不道,红色部分改成自己服务器的路径

更改了字体的路径以后  就可以正常显示了

 

posted on
2019-06-25 10:20  阅读(
...) 评论(
...) 收藏

转载于:https://www.cnblogs.com/baker95935/p/11081278.html

你可能感兴趣的文章
OFO和摩拜共享单车
查看>>
数据适配 DataAdapter对象
查看>>
有序列表ol和定义列表dl,dt,dd
查看>>
联想小新Air 15 安装黑苹果macOS High Sierra 10.13.6过程
查看>>
公共POI导出Excel方法–java
查看>>
次短路——Dijkstra
查看>>
二分图
查看>>
hdu 1853 Cyclic Tour(费用流OR二分图最佳匹配,5级)
查看>>
js 对url进行某个参数的删除,并返回url
查看>>
Windows7装Linux虚拟机
查看>>
SQL 操作结果集 -并集、差集、交集、结果集排序
查看>>
linux上搭建nginx+php+mysql环境详细讲解
查看>>
RemoveDuplicatesFromSortedArrayI II,移除有序数组里的重复元素以及移除数组里的某个元素...
查看>>
Minimum Depth of Binary Tree,求树的最小深度
查看>>
解决Web部署 svg/woff/woff2字体 404错误
查看>>
fiddler 抓取 nodejs
查看>>
1.Nginx服务应用
查看>>
MySQL基础
查看>>
凹凸贴图与法线贴图
查看>>
sqlserver跨服务器数据库sql语句
查看>>