Can I compress a PNG image without losing its transparency and saving to a new file, how is this done? View a dozen feature articles have failed

  • shrink - reduce the size? width and height? or reduce file size? - Sergey Konovalov

1 answer 1

If it’s about optimizing the image file by reducing its size, then gd does not know how, I recommend using the optipng utility

http://optipng.sourceforge.net/

syntax optipng [options] file

If we are talking about a banal reduction in the size of a picture, then you just need to set the blending mode to false, the save alpha channel option to true:

<?php $newImg = imagecreatetruecolor($nWidth, $nHeight); imagealphablending($newImg, false); imagesavealpha($newImg,true); $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127); imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent); imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]); ?>