PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】
微wx笑
2020-05-15【图形图像】
260
9
0关键字:
php 图像处理 文字水印、图片水印、压缩图像
这篇文章主要介绍了PHP的图像处理,结合实例形式总结分析了PHP针对文字水印、图片水印、压缩图像等相关操作实现技巧,需要的朋友可以参考下1、添加文字水印//1、打开图片资源
这篇文章主要介绍了PHP的图像处理,结合实例形式总结分析了PHP针对文字水印、图片水印、压缩图像等相关操作实现技巧,需要的朋友可以参考下
1、添加文字水印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //1、打开图片资源 $src = "./material/sea.jpg" ; $info = getimagesize ( $src ); //获取图片信息 $type =image_type_to_extension( $info [2],false); //转化图片类型 //var_dump($info); $fun = "imagecreatefrom{$type}" ; //拼接成为imagecreatefromjpeg()方法 $image = $fun ( $src ); //新建GD图片资源 //操作图片 $font = "./material/segoepr.ttf" ; $content = "@SuperTory" ; $color =imagecolorallocate( $image ,255,255,255); imagettftext( $image ,10,0,0, $info [1]-5, $color , $font , $content ); //图片上写文字 //输出图片 header( "content-type:" . $info [ 'mime' ]); //$imfo['mine']='image/jpeg' $output = "image{$type}" ; //拼接成为imagejpeg()方法 $output ( $image ); //输出到页面 $output ( $image , './material/watermarked.' . $type ); //输出到本地路径 //销毁图片内存资源 imagedestroy( $image ); |
2、压缩图像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //打开图像 $src = "./material/logo.png" ; $info = getimagesize ( $src ); $type =image_type_to_extension( $info [2],false); $create = "imagecreatefrom" . $type ; $image = $create ( $src ); //压缩 $tinyImg =imagecreatetruecolor(100,100); //新建压缩后的图像资源 //将原图映射到压缩后的图像资源上 imagecopyresampled( $tinyImg , $image ,0,0,0,0,100,100, $info [0], $info [1]); header( "content-type:" . $info [ 'mime' ]); $output = "image{$type}" ; //$output($image); $output ( $tinyImg ); //销毁 imagedestroy( $image ); imagedestroy( $tinyImg ); |
3、添加水印图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //获取原图片 $src = "./material/sea.jpg" ; $info = getimagesize ( $src ); $type =image_type_to_extension( $info [2],false); $create = "imagecreatefrom" . $type ; $image = $create ( $src ); //获取水印图片资源 $markSrc = "./material/logo.png" ; $markInfo = getimagesize ( $markSrc ); $markType =image_type_to_extension( $markInfo [2],false); $create = "imagecreatefrom" . $markType ; $markImage = $create ( $markSrc ); $tinyImg =imagecreatetruecolor(100,100); imagecopyresampled( $tinyImg , $markImage ,0,0,0,0, 100,100, $markInfo [0], $markInfo [1]); imagecopymerge( $image , $tinyImg , $info [0]-100, $info [1]-100, 0,0,100,100,100); //合并图片:(原图,水印图,原图x位置,原图y位置,水印x起点,水印y起点,水印x终点,水印y终点,不透明度) header( "content-type:" . $info [ 'mime' ]); $output = "image{$type}" ; $output ( $image ); imagedestroy( $image ); imagedestroy( $markImage ); |
转自:https://www.jb51.net/article/176826.htm
本文为转载文章,版权归原作者所有,不代表本站立场和观点。