手机版

php生成缩略图类共享 支持png透明图片

时间:2021-10-13 来源:互联网 编辑:宝哥软件园 浏览:

注意:该功能依赖于GD2图形库。

最近想用php生成缩略图。我在网上搜索,找到了这篇文章:PHP生成图片的缩略图。

经过试用,我发现有几个问题:

1.从png图片生成的缩略图是jpg格式的。

2.png图片生成的缩略图没有透明(半透明)效果(用黑色背景填充)。

3.代码语法相对较旧。

所以只是在这个版本的基础上做了简单的修改和优化。

用PHP生成缩略图类。

?php /* * desc:调整图像大小(png、jpg、gif) *作者:十年后的卢哥哥*日期: 2014。11 .13 */class ResizeImage {//图片类型私人$类型;//实际宽度private $ width//实际高度二等兵$身高;//改变后的宽度private $ resize _ width//改变后的高度private $ resize _ height//是否裁图私人美元削减;//源图象private $ srcimg//目标图象地址private $ dstimg//临时创建的图象私人$ imfunction _ _ construct($ IMgpath,$width,$height,$isCut,$ SavePath){ $ this-Sr cimg=$ IMgpath;$ this-resize _ width=$ width;$ this-resize _ height=$ height;$ this-cut=$ isCut;//图片的类型$ this-type=strtolow(substr(str chr($ this-srcimg,' .),1));//初始化图象$ this-initi _ img();//目标图象地址$ this-dst _ img($ SavePath);///$ this-width=imagesx($ this-im);$ this-height=imagesy($ this-im);//生成图象$ this-new img();图像破坏($ this-im);}私有函数newimg() { //改变后的图象的比例$ resize _ ratio=($ this-resize _ width)/($ this-resize _ height);//实际图象的比例$ ratio=($ this-width)/($ this-height);if($this-cut) { //裁图$ new img=imagecreatetrue color($ this-resize _ width,$ this-resize _ height);if($ this-type==' png '){ image fill($ new img,0,0,imagecoloallocatelpha($ new img,0,0,127));} if($ratio=$resize_ratio) { //高度优先imagecopyresholded($ new img,$this-im,0,0,0,$this-resize_width,$this-resize_height,($this-height)*$resize_ratio),$ this-height);} else { //宽度优先imagecopyresampled($newimg,$this-im,0,0,0,$this-resize_width,$this-resize_height,$this-width,($ this-width)/$ resize _ ratio));} } else { //不裁图if($ ratio=$ resize _ ratio){ $ new img=imagecreatetrue color($ this-resize _ width,($ this-resize _ width)/$ ratio);if($ this-type==' png '){ image fill($ new img,0,0,imagecoloallocatelpha($ new img,0,0,127));} imagecopyresampled($newimg,$this-im,0,0,0,$this-resize_width,($this-resize_width)/$ratio,$this-width,$ this-height);} else { $ new img=imagecreatetrue color($ this-resize _ height)* $ ratio,$ this-resize _ height);if($ this-type==' png '){ image fill($ new img,0,0,imagecoloallocatelpha($ new img,0,0,127));} imagecopy重新采样($ new img,$this-im,0,0,0,($this-resize_height)*$ratio,$this-resize_height,$this-width,$ this-height);} } if($ this-type==' png '){ image save alpha($ new img,true);imagepng ($newimg,$ this-dst img);} else { imagejpeg ($newimg,$ this-dstimg);} } //初始化图象私有函数initi _ img(){ if($ this-type==' jpg '){ $ this-im=imagecreatefrom JPEG($ this-srcimg);} if($ this-type==' gif '){ $ this-im=imagecreatefrom gif($ this-srcimg);} if($ this-type==' png '){ $ this-im=imagecreatefrompng($ this-srcimg);} } //图象目标地址私有函数dst _ img($ dst path){ $ full _ length=strlen($ this-Sr cimg);$ type _ length=strlen($ this-type);$ name _ length=$ full _ length-$ type _ length;$name=substr($this-srcimg,0,$ name _ length-1);$ this-dst img=$ dst路径;} }?使用

使用时,直接调用类的构造函数即可,构造函数如下:

$ resize image=新的resize image($ imgPath,$width,$height,$isCut,$ savePath);

参数$imgPath:原图片地址

$宽度:缩略图宽

$高度:缩略图高

$isCut:是否裁剪布尔值

$savePath:缩略图地址(可以跟原图片地址相同)

示例

?服务器端编程语言(Professional Hypertext Preprocessor的缩写)包括"调整图像大小。PHP”;//jpg $ jpgreize=新的重定大小影像(' img/test_1920_1200.jpg ',320,240,false,' img/test _ 320 _ 240。jpg’);//png $ pngreize=新的调整大小图像(' img/test _ 1024 _ 746。png ',320,240,false,' img/test _ 320 _ 240。png ');阿九

版权声明:php生成缩略图类共享 支持png透明图片是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。