Program to generate transparent background png thumbnails in php_PHP tutorial

WBOY
Release: 2016-07-13 10:46:17
Original
901 people have browsed it

The article introduces the program to generate a png thumbnail with a transparent background in PHP. It is very simple to process this in PHP. We only need to use imagealphablending($thumb,false); and imagesavealpha($thumb,true);. , see the program below.

When generating PNG thumbnails, the background is black. Today I wrote a function to make up for it. The code is very simple, that is, imagealphablending($thumb,false); and imagesavealpha($thumb,true); are very important. The main thing is to save the alpha value of PNG and not lose it.

 代码如下 复制代码
/*
*$sourePic:原图路径
* $smallFileName:小图名称
* $width:小图宽
* $heigh:小图高
* 转载注明 www.hzhuti.com */
function pngthumb($sourePic,$smallFileName,$width,$heigh){
$image=imagecreatefrompng($sourePic);//PNG
imagesavealpha($image,true);//这里很重要 意思是不要丢了$sourePic图像的透明色;
$BigWidth=imagesx($image);//大图宽度
$BigHeigh=imagesy($image);//大图高度
$thumb = imagecreatetruecolor($width,$heigh);
imagealphablending($thumb,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
imagesavealpha($thumb,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
if(imagecopyresampled($thumb,$image,0,0,0,0,$width,$heigh,$BigWidth,$BigHeigh)){
imagepng($thumb,$smallFileName);}
return $smallFileName;//返回小图路径 }

pngthumb("a.png", "c.png", 300, 300);//调用
?>

We all used the functions that come with PHP and did not use any third-party programs. Friends in need can take a look.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632965.htmlTechArticleThe article introduces the program to generate a png thumbnail with a transparent background in php. It is very simple to handle this in php. Just use imagealphablending($thumb,false); and imagesavealpha($thumb,tru...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template