How to reduce and resize PNG images with transparency in php

advertisements

I have 512 * 512 big size PNG images with transparency.

How to create 256 * 256 PNG images with smaller file size, which also support transparency and maintaining quality.

EDIT: I'm using this code but the output image is cropped and not supporting transparency.

   $image = imagecreatefrompng("C:\Users\HP\htdocs\icon_hd.png");  // 512 * 512
    $bg = imagecreatetruecolor(256, 256);
    imagefill($bg, 0, 0, 0);
    imagealphablending($bg, TRUE);
    imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
    imagedestroy($image);
    $quality = 100;
    imagepng($bg, "C:\Users\HP\out_icon.png", 9);
    imagedestroy($bg);


You can use ImageMagick :

Simple example:

$inFile = "big_img.png";
$outFile = "small_img.png";
$image = new Imagick($inFile);
$image->thumbnailImage(256, 256);
$image->writeImage($outFile);

More info