PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】

吾爱主题 阅读:138 2021-09-24 14:15:00 评论:0

本文实例讲述了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 );

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

原文链接:https://blog.csdn.net/theVicTory/article/details/80373319

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容