0

I'm trying to set up a simple PHP image server to allow me to add just large file for each of my images and then scale and crop them as needed. For my test file I start with a png8 exported via "save for web" from illustrator of size 2400 x 1200, which has a filesize of 21.6KB.

When I use Imagick's cropThumbnailImage function to reduce it to 600 x 600 the resulting file is 62.1KB (three times the size for a substantially smaller image). A 600 x 600 crop of the same image saved from illustrator clocks in at about 8.2KB. I can accept a modest file size increase for the added convenience, but an ~8x increase is just too much.

When saving the file I make sure to force the output to png8 so it doesn't default to a lossless png format, but other than that I'm clueless as to how to resolve it.

Here is my processing code:

//create working image
$image = new Imagick( $this->orig_file );

// Set Compression
$image->setImageCompressionQuality( 9 );

//scale image to height
$image->cropThumbnailImage ( $this->w, $this->h );

// strip extra data
$image->stripImage();

// save file
$image->writeImage( 'png8:'.$this->output_file );

Here are my test files:

Original Full scale image outputted by illustrator.

Cropped 600 x 600 image generated by imagick.

[EDIT: As per Mark's suggestion below I added the following changes]

// replacing cropThumbnailImage with:
$image->resizeImage(0, $this->h, imagick::FILTER_TRIANGLE, 1);

// crop
$start = ($image->getImageWidth() / 2) - ($this->w / 2);
$image->cropimage($this->w, $this->h, $start, 0);

// reduce colors to 20
$image->quantizeImage($this->q, 1, 0, true, false); // using 20 as $this->q

The end result goes from 62.1KB to 50.4KB, better but still over double the size of the fullsized image, and many times larger that the illustrator save for web version at that size.

600x600 image reduced to 20 colors and resized not thumbnailed

  • 1
    I am sure Glenn will tell you the answer but maybe, while waiting, try `scaleImage()` rather than thumbnailing as that will not introduce new colours. Or maybe try reducing the number of colours in your thumbnail - try around 30, around 50-60 and see. – Mark Setchell Jul 10 '16 at 22:35
  • Thanks for the reply! I tried the following changes: Using QuantizeImage to reduce the colors to 20 like so - $image->quantizeImage($this->q, 1, 0, true, false); And using resize image (filter TRIANGLE) and crop instead of thumbnail. The net result was going from 62KB to about 50KB. A step in the right direction, but still not where I'm hoping to be. http://amorphia-apparel.com/image/jabba.600.reduce.q20.png – Jeremy Kalgreen Jul 11 '16 at 03:59

1 Answers1

1

Your original image has 33 colours and weighs in at 22kB.

If you resize like this (albeit at the command line):

convert jabba.png -resize 600x600 -strip png8:result.png

the output file will be 6.6kB.

If you resize like I suggested with -scale:

convert jabba.png -scale 600x600 -strip png8:result.png

the output file will be 5.0kB.

If you retain -quality 9 in there, you will end up with > 25kB.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Interesting, by removing the: $image->setImageCompressionQuality( 9 ); It reduce the filesize to 11KB on the PHP version. I thought for PNGs that compression quality only had an effect on the amount of CPU work it used to view the file, but I guess that's only true for lossless PNGs. Thanks! – Jeremy Kalgreen Jul 11 '16 at 18:29