Home  >  Article  >  Backend Development  >  PHP solution to shrink png images without losing transparent color_PHP tutorial

PHP solution to shrink png images without losing transparent color_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:18:43973browse

Mainly use two methods of the gd library:

Copy code The code is as follows:

imagecolorallocatealpha //Assign colors + alpha

imagesavealpha //Set to save complete alpha channel information when saving png images

Code example:

Copy code The code is as follows:

//Get the source image gd image identifier
$srcImg = imagecreatefrompng(' ./src.png');
$srcWidth = imagesx($srcImg);
$srcHeight = imagesy($srcImg);

//Create a new image
$newWidth = round($srcWidth / 2);
$newHeight = round($srcHeight / 2);
$newImg = imagecreatetruecolor($newWidth, $newHeight) ;
//Assign color + alpha, fill the color onto the new image
$alpha = imagecolorallocatealpha($newImg, 0, 0, 0, 127);
imagefill($newImg, 0, 0, $alpha);

//Copy the source image to the new image, and set it to save the complete alpha channel information when saving the PNG image
imagecopyresampled($newImg, $srcImg, 0, 0, 0, 0, $newWidth, $ newHeight, $srcWidth, $srcHeight);
imagesavealpha($newImg, true);
imagepng($newImg, './dst.png');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621653.htmlTechArticleMainly use two methods of the gd library: Copy the code The code is as follows: imagecolorallocatealpha //Assign color + alpha imagesavealpha / /Set to save full alpha when saving png images...
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