To shrink the picture to the appropriate size, JPG image reduction is easier, PNG picture if with transparent color, by JPG to reduce, will cause transparent color loss.
Save transparent color mainly utilize two methods of GD library:
Imagecolorallocatealpha Assign color + Alpha
Imagesavealpha setting saves full alpha channel information when you save a PNG image
The code is as follows:
Get the source diagram GD image identifier
$srcImg = Imagecreatefrompng ('./source.png ');
$srcWidth = Imagesx ($SRCIMG);
$srcHeight = Imagesy ($SRCIMG);
Create a new diagram
$newWidth = round ($srcWidth/2);
$newHeight = Round ($srcHeight/2);
$NEWIMG = Imagecreatetruecolor ($newWidth, $newHeight);
Assign color + Alpha to fill the new image
$alpha = Imagecolorallocatealpha ($newImg, 0, 0, 0, 127);
Imagefill ($newImg, 0, 0, $alpha);
Copy the source diagram to the new diagram and set the full alpha channel information to be saved when the PNG image is saved
imagecopyresampled ($newImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $s Rcwidth, $srcHeight);
Imagesavealpha ($NEWIMG, true);
Imagepng ($NEWIMG, './thumb.png ');
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/