微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP基于GD库的图像处理方法小结

本文实例讲述了PHP基于GD库的图像处理方法分享给大家供大家参考,具体如下:

GD图像处理技术

PHP_gd2.dll

创建画布

画布,一种资源型数据,可操作的图像资源

创建画布(新建)

彩色的画布

基于图片创建画布(打开)

imagecreatefrompng(url) imageCreateFromGIF(url)

操作画布

分配颜色:如果需要在画布上使用某种颜色,应该先将颜色分配到画布上。

填充画布

输出画布

1. 输出图片文件

2. 直接输出,需要告知浏览器输出图片信息(

rush:PHP;"> imagePNG(img[,url]) imageJPEG() imageGIF()

销毁画布资源

rush:PHP;">

运行效果图如下:

验证码实现

rush:PHP;"> PHP header('content-type:image/png'); $code = '123456789abcdefghijklmnpqrstuvwxvz'; $length = strlen($code); $print = ''; for($i=0; $i<4; $i++){ $print.=$code[mt_rand(0,$length-1)]; } // echo $print; $img = imagecreatefrompng('./str.png'); $color = mt_rand(0,1)==1?imagecolorallocate($img,0):imagecolorallocate($img,255,255); //图片大小 $img_width = imagesx($img); $img_height = imagesy($img); //字体大小 $font = 5; $font_width = imagefontwidth($font); $font_height = imagefontheight($font); $fin_w = ($img_width-$font_width*4)/2; $fin_h = ($img_height-$font_height)/2; imagestring($img,$font,$fin_w,$fin_h,$print,$color); imagepng($img); imagedestroy($img); ?> PHP" onclick="this.src='gd_string.PHP?ra='+Math.random()">

运行效果图如下:

rush:PHP;"> PHP session_start(); $im=imagecreatetruecolor(80,30); $str=""; for ($i=0;$i<4;$i++){ $str.=dechex(rand(0,15)); } $_SESSION['code']=$str; $white=imagecolorallocate($im,255); imagestring($im,rand(2,5),rand(0,70),10),$str,$white); //imagettftext($im,180),100),$white,"simhei.ttf",$str); for($i=0;$i<20;$i++){ $color=imagecolorallocate($im,255),255)); imageline($im,90),20),$color); } header("content-type:image/png"); imagepng($im); imagedestroy($im); ?>

注意:

图片输出前后不能有额外输出

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

原文地址:https://www.jb51.cc/php/18936.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐