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

2025-05-29 0 19

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】 https://www.kuaiidc.com/91396.html

相关文章

发表评论
暂无评论