hankcoder has asked for the wisdom of the Perl Monks concerning the following question:

I need suggestion on which is the best available module/method to compress PNG file with transparent/alpha support.

I tried with ImageMagick, it works great on JPG but when deal with PNG, I can't make the file size smaller. Tried all kinds of method.

I also tried with using PHP imagecreatefrompng, imagepng but it screw up the transparent on output.

The only best I tested is using pngquant (command line). But it is only on my Windows IIS ActivePerl environment. I'm not sure how to install/setup pngquant on my linux hosting server. It is too complicated to me.

So I seek for alternative method in perl which I'm more familiar with.

Any help are very much appreciated. Thanks.

* UPDATE *
ImageMagick using $image->Resize(geometry => "${width}x${height}"); able to resize the PNG (with transparent). Without transparent resize also ok but file size still not small compare to pngquant.

I still need a PNG compression without resizing dimension.

* UPDATE *
This only able to reduce png file size from 1,666kb to 1,228kb without resize dimension.

$image->Set(quality =>'95'); $image->Set(depth=>'8');

* UPDATE *
After done more test with imagemagick, the best it only able to reduce about 20%. My current final best test still on pngquant where from 1666kb to 377kb. That is Huge difference.

pngquant command used pngquant --quality 20-40 -speed 11 [png file]

* FINAL UPDATE *
I have decided to use pngquant and managed to get it installed on linux.

Replies are listed 'Best First'.
Re: PNG compression support transparent
by wjw (Priest) on Jul 23, 2015 at 07:09 UTC
    I have not done much myself with image manipulation with Perl, however, the first thing I thought of was Gimp, and as it turns out, there is a nice intro to controlling Gimp with Perl here -> Gimp-Perl

    I have found Gimp to be very capable of handling any image work in almost any format(with what little I have done with image manipulation).

    In spite of this being a shot in the dark, I hope that might be helpful.

    ...the majority is always wrong, and always the last to know about it...

    A solution is nothing more than a clearly stated problem...

Re: PNG compression support transparent
by tangent (Parson) on Jul 23, 2015 at 17:15 UTC
    I tried with ImageMagick
    What did you try? Can you tell us a bit more about the input image, how you are reading it in and how you write out the new one? There are lots of things you can do that aren't in the Perl docs.

      The testing codes I have done in ImageMagick are very messy now due to various attempt here and there. I abandon testing it further and currently able to have better luck in PHP using ImageCreateTrueColor, imagealphablending, imagesavealpha, ImageCopyResampled. But still not able to reduce the file size without resizing the dimension for PNG.

      Testing in ImageMagick I have done using functions like;

      $image->Set(units => 'PixelsPerInch'); $image->Set(depth=>8); $image->Set(density => '300x300'); $image->Set(quality =>'20');

      Do take note the codes above are not in actual order.

      Resizing is ok with

      $image->Resize( geometry => '400x400' ); $image->Extent( geometry => '400x400' );
      File size still not very small but that is not my purpose.

      Currently I tested using pngquant in Windows commandline, the PNG file are able to compressed a lot and still retain its original dimension.

      If you are able to give me some hints on ImageMagick, that would be very much appreciated.

      Image files I used are normal wallpaper size in JPG and PNG, most download from google search. Some of the file size about 2-3MB. One PNG is using smartphone model with transparent alpha background to test how is the result after resize (thumbnail).

Re: PNG compression support transparent
by Anonymous Monk on Jul 23, 2015 at 16:30 UTC
    I'm not sure how to install/setup pngquant on my linux hosting server. It is too complicated to me.
    I think you're going to have to bite the bullet and figure it out. The pngquant web page has pointers to packages for various linux distros.
      Thanks. I will look into this further.