PHP缩小png图片,保留透明色方法

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

将图片缩成合适的尺寸,jpg图片缩小比较容易,png图片如果带了透明色,按jpg方式来缩小,会造成透明色丢失。


保存透明色主要利用gd库的两个方法:

imagecolorallocatealpha 分配颜色 + alpha

imagesavealpha 设置在保存png图像时保存完整的 alpha 通道信息


代码如下:

//获取源图gd图像标识符$srcImg = imagecreatefrompng('./source.png');$srcWidth = imagesx($srcImg);$srcHeight = imagesy($srcImg);//创建新图$newWidth = round($srcWidth / 2);$newHeight = round($srcHeight / 2);$newImg = imagecreatetruecolor($newWidth, $newHeight);//分配颜色 + alpha,将颜色填充到新图上$alpha = imagecolorallocatealpha($newImg, 0, 0, 0, 127);imagefill($newImg, 0, 0, $alpha);//将源图拷贝到新图上,并设置在保存 PNG 图像时保存完整的 alpha 通道信息imagecopyresampled($newImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $srcWidth, $srcHeight);imagesavealpha($newImg, true);imagepng($newImg, './thumb.png');


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/hsyyff/article/details/84143310
今日推荐