加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php生成图片缩略图,支持png透明

发布时间:2020-05-25 03:33:38 所属栏目:PHP 来源:互联网
导读:注:此功能依赖GD2图形库最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图试用了一下后,发现有这样几个问题:1、png图片生成的缩略图是jpg格式的2、png图片生成的缩略图没有了透明(半透明)效果(填充了黑色背景)3、代码语法比

试用了一下后,发现有这样几个问题:

1、png图片生成的缩略图是jpg格式的

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

3、代码语法比较老

因此,在这个版本的基础上简单修改优化了一下。

PHP生成缩略图类

</span><span style="color: #0000ff;"&gt;function</span> __construct(<span style="color: #800080;"&gt;$imgPath</span>,<span style="color: #800080;"&gt;$width</span>,<span style="color: #800080;"&gt;$height</span>,<span style="color: #800080;"&gt;$isCut</span>,<span style="color: #800080;"&gt;$savePath</span><span style="color: #000000;"&gt;) { </span><span style="color: #800080;"&gt;$this</span>->srcimg = <span style="color: #800080;"&gt;$imgPath</span><span style="color: #000000;"&gt;; </span><span style="color: #800080;"&gt;$this</span>->resize_width = <span style="color: #800080;"&gt;$width</span><span style="color: #000000;"&gt;; </span><span style="color: #800080;"&gt;$this</span>->resize_height = <span style="color: #800080;"&gt;$height</span><span style="color: #000000;"&gt;; </span><span style="color: #800080;"&gt;$this</span>->cut = <span style="color: #800080;"&gt;$isCut</span><span style="color: #000000;"&gt;; </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;图片的类型</span> <span style="color: #800080;"&gt;$this</span>->type = <span style="color: #008080;"&gt;strtolower</span>(<span style="color: #008080;"&gt;substr</span>(<span style="color: #008080;"&gt;strrchr</span>(<span style="color: #800080;"&gt;$this</span>->srcimg,"."),1<span style="color: #000000;"&gt;)); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;初始化图象</span> <span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;initi_img(); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;目标图象地址</span> <span style="color: #800080;"&gt;$this</span> -> dst_img(<span style="color: #800080;"&gt;$savePath</span><span style="color: #000000;"&gt;); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;--</span> <span style="color: #800080;"&gt;$this</span>->width = imagesx(<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;im); </span><span style="color: #800080;"&gt;$this</span>->height = imagesy(<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;im); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;生成图象</span> <span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;newimg(); ImageDestroy (</span><span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;im); } </span><span style="color: #0000ff;"&gt;private</span> <span style="color: #0000ff;"&gt;function</span><span style="color: #000000;"&gt; newimg() { </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;改变后的图象的比例</span> <span style="color: #800080;"&gt;$resize_ratio</span> = (<span style="color: #800080;"&gt;$this</span>->resize_width)/(<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;resize_height); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;实际图象的比例</span> <span style="color: #800080;"&gt;$ratio</span> = (<span style="color: #800080;"&gt;$this</span>->width)/(<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;height); </span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;cut) { </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;裁图</span> <span style="color: #800080;"&gt;$newimg</span> = imagecreatetruecolor(<span style="color: #800080;"&gt;$this</span>->resize_width,<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;resize_height); </span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$this</span>->type=="png"<span style="color: #000000;"&gt;) { imagefill(</span><span style="color: #800080;"&gt;$newimg</span>,imagecolorallocatealpha(<span style="color: #800080;"&gt;$newimg</span>,127<span style="color: #000000;"&gt;)); } </span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$ratio</span>>=<span style="color: #800080;"&gt;$resize_ratio</span><span style="color: #000000;"&gt;) { </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;高度优先</span> imagecopyresampled(<span style="color: #800080;"&gt;$newimg</span>,<span style="color: #800080;"&gt;$this</span>->im,<span style="color: #800080;"&gt;$this</span>->resize_width,<span style="color: #800080;"&gt;$this</span>->resize_height,((<span style="color: #800080;"&gt;$this</span>->height)*<span style="color: #800080;"&gt;$resize_ratio</span>),<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;height); } </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; { </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;宽度优先</span> imagecopyresampled(<span style="color: #800080;"&gt;$newimg</span>,<span style="color: #800080;"&gt;$this</span>->width,((<span style="color: #800080;"&gt;$this</span>->width)/<span style="color: #800080;"&gt;$resize_ratio</span><span style="color: #000000;"&gt;)); } } </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; { </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;不裁图</span> <span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$ratio</span>>=<span style="color: #800080;"&gt;$resize_ratio</span><span style="color: #000000;"&gt;) { </span><span style="color: #800080;"&gt;$newimg</span> = imagecreatetruecolor(<span style="color: #800080;"&gt;$this</span>->resize_width,(<span style="color: #800080;"&gt;$this</span>->resize_width)/<span style="color: #800080;"&gt;$ratio</span><span style="color: #000000;"&gt;); </span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$this</span>->type=="png"<span style="color: #000000;"&gt;) { imagefill(</span><span style="color: #800080;"&gt;$newimg</span>,127<span style="color: #000000;"&gt;)); } imagecopyresampled(</span><span style="color: #800080;"&gt;$newimg</span>,(<span style="color: #800080;"&gt;$this</span>->resize_width)/<span style="color: #800080;"&gt;$ratio</span>,<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;height); } </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; { </span><span style="color: #800080;"&gt;$newimg</span> = imagecreatetruecolor((<span style="color: #800080;"&gt;$this</span>->resize_height)*<span style="color: #800080;"&gt;$ratio</span>,<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;resize_height); </span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$this</span>->type=="png"<span style="color: #000000;"&gt;) { imagefill(</span><span style="color: #800080;"&gt;$newimg</span>,(<span style="color: #800080;"&gt;$this</span>->resize_height)*<span style="color: #800080;"&gt;$ratio</span>,<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;height); } } </span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$this</span>->type=="png"<span style="color: #000000;"&gt;) { imagesavealpha(</span><span style="color: #800080;"&gt;$newimg</span>,<span style="color: #0000ff;"&gt;true</span><span style="color: #000000;"&gt;); imagepng (</span><span style="color: #800080;"&gt;$newimg</span>,<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;dstimg); } </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; { imagejpeg (</span><span style="color: #800080;"&gt;$newimg</span>,<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;dstimg); } } </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;初始化图象</span> <span style="color: #0000ff;"&gt;private</span> <span style="color: #0000ff;"&gt;function</span><span style="color: #000000;"&gt; initi_img() { </span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$this</span>->type=="jpg"<span style="color: #000000;"&gt;) { </span><span style="color: #800080;"&gt;$this</span>->im = imagecreatefromjpeg(<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;srcimg); } </span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$this</span>->type=="gif"<span style="color: #000000;"&gt;) { </span><span style="color: #800080;"&gt;$this</span>->im = imagecreatefromgif(<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;srcimg); } </span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$this</span>->type=="png"<span style="color: #000000;"&gt;) { </span><span style="color: #800080;"&gt;$this</span>->im = imagecreatefrompng(<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;srcimg); } } </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;图象目标地址</span> <span style="color: #0000ff;"&gt;private</span> <span style="color: #0000ff;"&gt;function</span> dst_img(<span style="color: #800080;"&gt;$dstpath</span><span style="color: #000000;"&gt;) { </span><span style="color: #800080;"&gt;$full_length</span> = <span style="color: #008080;"&gt;strlen</span>(<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;srcimg); </span><span style="color: #800080;"&gt;$type_length</span> = <span style="color: #008080;"&gt;strlen</span>(<span style="color: #800080;"&gt;$this</span>-><span style="color: #000000;"&gt;type); </span><span style="color: #800080;"&gt;$name_length</span> = <span style="color: #800080;"&gt;$full_length</span>-<span style="color: #800080;"&gt;$type_length</span><span style="color: #000000;"&gt;; </span><span style="color: #800080;"&gt;$name</span> = <span style="color: #008080;"&gt;substr</span>(<span style="color: #800080;"&gt;$this</span>->srcimg,<span style="color: #800080;"&gt;$name_length</span>-1<span style="color: #000000;"&gt;); </span><span style="color: #800080;"&gt;$this</span>->dstimg = <span style="color: #800080;"&gt;$dstpath</span><span style="color: #000000;"&gt;; } }

?>

使用

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

= resizeimage($imgPath,$width,$height,$isCut,$savePath);

参数

$imgPath:原图片地址

$width:缩略图宽

$height:缩略图高

$isCut:是否裁剪,bool值

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读