宝哥软件园

PHP图像处理技术示例总结[绘图、水印、验证码、图像压缩]

编辑:宝哥软件园 来源:互联网 时间:2021-08-28

本文结合实例总结了PHP图像处理技术。分享给大家参考,如下:

1、绘图

场景:验证码,图像水印,图像压缩处理

php绘图的坐标系是数值从0,0点向右增加,数值向下增加

您需要在php.ini中打开php的gd2扩展

参数1:图像资源(画布)参数2:起始x轴坐标参数3:起始y轴坐标参数4:终止x轴坐标参数5:终止y轴坐标参数6:线条颜色

(1)画线:imageline ($ P1、$ P2、$ P3、$ P4、$ P5、$6) (2)画三角形:imageline ($ P1、$ P2、$ P3、$ P4、$ P5、$ 6)//画矩形需要3次:imagerectangle $6)(3.1)画和填矩形:ImageFilled Rectangle ($ P1、$ P2、$ P3、$ P4、$ P5、$6) (4)画椭圆:image elliplex($ P1、$ P2、$ P3、$ P5、$6) (4.1

参数1:目标图像参数2:原始图像参数3:目标图像坐标x参数4:目标图像坐标y参数5:原始图像起始坐标x参数6:原始图像起始坐标y参数7:原始图像宽度参数8:原始图像高度

(5)在画布上绘画:图像副本($ P1、$ P2、$ P3、$ P4、$ P5、$6、$7、$8)

参数1:目标图像参数2:字体1、2、3、4或5,然后使用内置字体参数3:目标图像坐标x参数4:目标图像坐标y参数5:字符、文本参数6:颜色

(6)绘制字符串:ImageString ($ P1、$ P2、$ P3、$ P4、$ P5、$ 6)//将字符和单词写入画布

参数1:图像资源参数2:字体大小参数3:倾斜角度参数4: x轴坐标参数5: y轴坐标参数6:字体颜色参数7:字体文件参数8:文字

(7)绘制中文:imagettftext(P1、P2、P3、P4、P5、6、7、8美元)

参数1:图像资源参数2:弧起点x坐标参数3:弧起点y坐标参数4:弧宽参数5:弧高参数6:弧起点角度参数7:弧终点角度参数8:绘图颜色

(8)画弧:Imagearc ($ P1、$ P2、$ P3、$ P4、$ P5、$6、$7、$ 8)//三点钟位置为起点(0度),顺时针画

示例-arc

//创建200X200图像$ img=imagecreate真彩色(200,200);//分配颜色$ white=imagecolorrocent($ img,255,255,255);$black=imagecolorallocate($img,0,0,0);//画一个黑色圆圈imagearc ($ img,100,100,150,150,0,360,$ black);//将图像输出到浏览器头(' content-type : image/png ');image png($ img);//空闲内存image destroy($ img);参数1:图像资源参数2:弧起点x坐标参数3:弧起点y坐标参数4:弧宽参数5:弧高参数6:弧起点角度参数7:弧终点角度参数8:绘图颜色参数9:填充样式[IMG_ARC_PIE:通过直线连接创建圆形边界IMG_ARC_CHORD:通过直线连接起点和终点IMG_ARC_NOFILL:开放的弧或弦只有轮廓, 未填充IMG _ ARC _ edge:用直线连接起点和终点与中心点,并与IMG_ARC_NOFILL一起使用是绘制饼图轮廓的好方法(不填充)]

(9)画一个弧并填充:imagefiledarc($ P1、$ P2、$ P3、$ P4、$ P5、$6、$7、$8、$ 9)//三点钟位置为起点(0度),顺时针画

示例-弧形填充

//唉呀呀呀呀呀$ image=imagecreatetrue color(100,100);//云娥与云娥$ white=imagecolor allocate($ image,0xFF,0xFF,0 xff);$ gray=imagecolor allocate($ image . 0 xc 0.0 xc 0.0 xc 0);$ dark gray=imagecolor allocate($ image .0x 90.0 x 90.0 x 90);$ navy=imagecolor allocate($ image .0x 00.0 x 00.0 x 80);$ dark navy=imagecolor allocate($ image .0x 00.0 x 00.0 x 50);$ red=imagecolor allocate($ image . 0 xff .0x 00.0 x 00);$暗红色=imagecolor allocate($ image .0x 90.0 x 00.0 x 00);//哼哼哼三维(three dimension的缩写)效果阿九for($ I=60);50美元;$i-{ imagefilldac($ image 50、$ I、100、50、0.45、$darknavy、img _ arc _ pie);imagefilldarc($ image,50、$i、100、50、45、75、$darkgray、img _ arc _ pie);imagefilldarc($ image,50、$i、100、50、75、360、$darkred、img _ arc _ pie);} imagefilldarc($ image、50、50、100、50、0.45、$navy、img _ arc _ pie);imagefilldarc($ image、50、50、100、50、45、75、$gray、img _ arc _ pie);imagefilldarc($ image、50、50、100、50、75、360、$red、img _ arc _ pie);//菲兰达标头(“内容类型:图像/png”);image png($ image);图像破坏($ image);阿九

2、水印

年欧洲足球锦标赛僧曰imagestring()阿奈威imagettftext()

是张居正-(中文)

//314日100乘以30阿云哥$ im=图像创建(100.30);//阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳阿琳$bg=imagecolorallocate($im,255,255,255);$ text color=imagecolor allocate($ im,0,0,255);//阿亚达阿亚达阿亚达阿亚达阿亚达阿亚达阿亚达阿亚达阿亚达imagestring($im,5,0,0,' Hello world!',$textcolor//菲兰达标头(“内容类型:图像/png”);image png($ im);

3、验证码

年欧洲足球锦标赛是绿绿绿绿绿绿绿绿绿绿绿绿蓝绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿的

?Php/* *生成验证码*/class captcha { private $ _ width=100;private $ _ height=25private $ _ number=4;//显示的验证码字符数为private $ _ font=15//验证码字号private $ _ font file=' stxinwei . TTF ';//创建验证码图片公共函数makeImage() {# 1。创建图像资源(画布)$ image=image create true color($ this-_ width,$ this-_ height);//随机填充颜色//mt_rand(0,255)生成一个更唯一的随机数# 000 255 $ color=imagecolor locate($ image,mt _ rand (100,255),mt _ rand (100,255),mt _ rand (100,255) imagefill($image,0,0,$ color);# 2.绘制文本$ code=$ this-make code();//随机生成验证码字符ab3g $ color=imagecolor locate($ image,mt _ rand (0,100),mt _ rand (0,100),mt _ rand (0,100));for($ I=0;$ i $这个-_号;$i ){ imagettftext($image,$this-_font,mt_rand(-30,30),$ I *(this-_ width/$ this-_ number)5,20,$color,$this-_fontfile,$ code[$ I]);} # 3.为($i=0)绘制15条干涉线;$ i10$ I){ $ color=imagecolor allocate($ image,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));imageline($image,mt_rand(0,$this-_width),mt_rand(0,$this-_height),mt_rand(0,$this-_width),mt_rand(0,$this-_height),$ color);} # 4.为($i=0)设置100个干涉像素;100美元;$i ){ imagesetpixel($image,mt_rand(0,$this-_width),mt_rand(0,$this-_height),$ color);} # 5.保存验证码以备后用。//只能用session存储。会话将在明天谈论session _ start();$ _ SESSION[' captcha ']=$ code;//输出并在浏览器中显示标题(' content-type : image/png ');image png($ image);image destroy($ image);}/* * *随机生成随机数*/公共函数makeCode() {#获取字母的范围(大写字母和小写字母)$lower=range('a ',' z ');//创建一个字符范围从小A到小Z的数组$upper=range('A ',' Z ');//创建一个从大A到大Z的数组$number=range(3,9);//创建从3到9的数字//将以上三个数字组合成一个数组$ code=array _ merge ($ lower,$ upper,$ number);#打乱数组元素的顺序shuffle($ code);//从上面的数组中随机选择n个字符,需要通过下标得到数组的元素$ str=';for($ I=0;$ i $这个-_号;$i ){ $str。=$ code[$ I];}返回$ str}/* * *验证用户输入的验证码是否与我们产生的验证码一致* @ param[str]$ input[输入验证码值] * @ return */public function校验码($ input){ session _ start();if(str lower($ code)==str lower($ _ session[' captcha '])){//表示验证码正确//echo '验证码正确';返回真;}else{ //echo“错误的验证码”;返回false} }}?示例-验证码验证(结合上述验证类)

Html页面

form action='captcha.php?Act=verify' method='post '验证码:输入类型=' text ' name=' captcha ' img src=' http:captcha.php?Act=show' br输入类型=' submit '值=' submit '/表单验证码检测到captcha.php页面

//接收参数if ($ _ get ['act']==' verify ')地址栏上方{//解释提交的表单//接收用户输入的$ code=$ _ POST[' captcha '];//与创建的验证码session_start()进行比较;//将用户输入的验证码与我们创建的统一小写if(strlower($ code)==strlower($ _ session[' captcha ']))进行对比{//说明验证码正确echo '验证码正确';}else{ echo '错误的验证码';} } else if($ _ get[' act ']==' show '){//表示需要显示图片;需要' Captcha . class . PHP ';$ Captcha=new Captcha();$ captcha-Makeimage();}

4、图像压缩

按下图像

收缩非常简单,因为只有一个函数

参数1:目标图像资源(画布)参数2:等待压缩图像资源参数3:目标点的x坐标参数4:目标点的y坐标参数5:原图的x坐标参数6:原图的y坐标参数7:目的地宽度(画布宽)参数8:目的地高度(画布高)参数9:原图宽度参数10:原图高度

图象副本重采样(1美元、2美元、3美元、4美元、5美元、6美元、7美元、8美元、9美元、10美元)

封装的图像压缩类

?php/* *图像压缩处理类*/class Thumb { private $ _ filename;//等待压缩的图像private $ _ thumb _ path=' thumb/';//压缩图像的保存目录public function __set($p,$v) { if(property_exists($this,$ p)){ $ this-$ p=$ v;} } //构造方法初始化需要压缩的图像public function _ _ construct($ file){ if(!file_exists($file)){ echo '文件有误,不能压缩;返回;} $ this-_ filename=$ file;} //图像压缩处理函数makeThumb($area_w,$ area _ h){ $ src _ image=imagecreatefrompng($ this-_ filename);$ RES=getimagesize($ this-_ filename);回声“pre”;var _ dump($ RES);死去;$ dst _ x=0;$ dst _ y=0;$ src _ x=0;$ src _ y=0;//原图的宽度、高度$ src _ w=imagesx($ src _ image);//获得图像资源的宽度$ src _ h=imagesy($ src _ image);//获得图像资源的高度if($ src _ w/$ area _ w $ src _ h/$ area _ h){ $ scale=$ src _ h/$ area _ h;} if($ src _ w/$ area _ w=$ src _ h/$ area _ h){ $ scale=$ src _ w/$ area _ w;} $ dst _ w=(int)($ src _ w/$ scale);$ dst _ h=(int)($ src _ h/$ scale);$ dst _ image=imagecreatetrue color($ dst _ w,$ dst _ h);$ color=image color allocate($ dst _ image,255,255,255);//将白色设置为透明色image color transparent($ dst _ image,$ color);imagefill($dst_image,0,0,$ color);imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$ src _ h);//可以在浏览器直接显示//标头(“内容类型: IMage/png”);//image png($ dst _ image);//分目录保存压缩的图像$sub_path=date('Ymd ')./';//规范:上传的图像保存到上传目录,压缩的图像保存到拇指目录if(!is_dir($this - _thumb_path .$ sub _ path)){ mkdir($ this-_ thumb _ path .$sub_path,0777,true);} $filename=$this - _thumb_path .$子路径.拇指_ '。$ this-_ filename;//也可以另存为一个新的图像imagepng($dst_image,$ filename);返回$ filename} } $ Thumb=new Thumb('上传。jpg’);$ thumb-_ thumb _ path=' static/thumb/';$file=$thumb - makeThumb(100,50);//var _ dump($ file);更多关于服务器端编程语言(专业超文本预处理器的缩写)相关内容感兴趣的读者可查看本站专题: 《PHP图形与图片操作技巧汇总》 、 《PHP数组(Array)操作技巧大全》 、 《PHP数据结构与算法教程》 、 《php程序设计算法总结》 、 《PHP数学运算技巧总结》 、 《php字符串(string)用法总结》 及《php常见数据库操作技巧汇总》

希望本文所述对大家服务器端编程语言(专业超文本预处理器的缩写)程序设计有所帮助。

更多资讯
游戏推荐
更多+