Home  >  Article  >  Backend Development  >  PHP implementation of generating PNG thumbnail function with transparent background sharing, png thumbnail_PHP tutorial

PHP implementation of generating PNG thumbnail function with transparent background sharing, png thumbnail_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:22721browse

PHP implements sharing of PNG thumbnail function for generating transparent background, png thumbnail

I wrote a PHP function to generate thumbnails in the WEB development notes before. Although that function can generate thumbnails, it has certain flaws. When generating PNG thumbnails, the background is black. I wrote another function today. Come make amends. 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.

The function is as follows:

<&#63;PHP
/*
 *$sourePic:原图路径
 * $smallFileName:小图名称
 * $width:小图宽
 * $heigh:小图高
 * 转载注明 www.chhua.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;//返回小图路径 转载注明 www.chhua.com
}
 
pngthumb("a.png", "c.png", 300, 300);//调用
&#63;>

How to make the background of generated PNG thumbnail transparent? - PHP Advanced Discussion

Use PHOTOSHOP to do it, cut out what you need and make a copy, then delete the background layer and save it as PNG

Using the following php thumbnail function, thumbnails cannot be generated for some jpg images, but others can. Why is this?

Note

1: After judging the image, prepare it for output or save before using the method imagecreatefromjpeg

2: imagesy($src_img); Please consider using list($width,$ height) = getimagesize($src_img);

3: $ratio_h=1.0 * $new_height / $h;//n=unknown number, 1 multiplied by any n, the result is still n, why do we do this ?

4: Your program receives external input dimensions, which will cause the output or saved pictures to be distorted and deformed

5: $ratio=1.0;
if($ratio_w < ;=1 && $ratio_h <= 1) {
if($ratio_w < $ratio_h){}else {}//Note 4: Consider using the ternary algorithm

6: $inter_h= (int) ($new_height /$ratio);//PHP is not as sensitive to data types as Java, so there is no need to use int to force conversion

7: Remember to destroy the image after processing imagedestroy();

Finally: You can explain the effect you want

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840751.htmlTechArticlePHP implements the sharing of PNG thumbnail function that generates transparent background. Before png thumbnail, I wrote a PHP in the WEB development notes A function that generates thumbnails. Although that function can generate thumbnails,...
Statement:
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