Compression PNG / Image Size?

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
bastimm

Compression PNG / Image Size?

Post by bastimm »

Hello together,

first sorry for my bad english!! But i´m working to make it better ;-)


Since 1 week i try to make a small thumbnail with rounded corner.
This was no problem with imagick!
Great Tool, but the size of the PNG-File i get is always arround 93kB!!! The size is only 195x130px :s

Is there a way to copress this PNG?? I Tried the following things:

Code: Select all

$newimage->setCompression(Imagick::COMPRESSION_ZIP);
$newimage->setCompressionQuality(30);
$newimage->setImageCompression(Imagick::COMPRESSION_ZIP);
$newimage->setImageCompressionQuality(30);
$newimage->setImageInterlaceScheme(Imagick::INTERLACE_PNG);
$newimage->setImageDepth(8);
this is my whole code to get a thumbnail with rounded corners on top:

Code: Select all

$newWidth = 195;
$newHeight = 130;

$newimage = new Imagick();
$newimage->newImage( $newWidth, $newHeight + 20, new ImagickPixel( "#DBDBDB" ) );

$image = new Imagick($tempFile);

$image->stripImage();
$imageWidth = $image->getImageWidth();
$imageHeight = $image->getImageHeight();
	
$ratioWidth = $newWidth/$imageWidth;
$ratioHeight = $newHeight/$imageHeight;
if ($ratioWidth < $ratioHeight){
	$ratio = $ratioHeight;
	$newCutWidth = $newWidth;
	$newCutHeight = $newHeight;
	
	$newHeight = intval($ratio * $imageHeight);
	$newWidth = intval($ratio * $imageWidth);
}
else {
	$newCutWidth = $newWidth;
	$newCutHeight = $newHeight;
	
	$ratio = $ratioWidth;
	$newHeight = intval($ratio * $imageHeight);
	$newWidth = intval($ratio * $imageWidth);
}
$image->resizeImage($newWidth, $newHeight,0,1);
	
$image->cropImage($newCutWidth,$newCutHeight, 0,0);

$newimage->compositeImage( $image, Imagick::COMPOSITE_OVER, 0, 0 );
$image->destroy();

$newimage->setImageFormat("png");
$newimage->roundCorners(2,2);
$newimage->cropImage($newCutWidth,$newCutHeight,0,0);

$newimage->setCompression(Imagick::COMPRESSION_ZIP);
$newimage->setCompressionQuality(30);
$newimage->setImageCompression(Imagick::COMPRESSION_ZIP);
$newimage->setImageCompressionQuality(30);
$newimage->setImageInterlaceScheme(Imagick::INTERLACE_PNG);
$newimage->setImageDepth(8);

$sql = "INSERT into images (thumb, ID) VALUES ('" . $db->escape($newimage->getImageBlob()) . "', '" . $imageID . "')";
$db->execute($sql);

$fh = fopen("gallery/thumbs/" . $imageID . ".png","w+");
fwrite($fh,$newimage);
fclose($fh);

$newimage->destroy();
is here somebody who can help me to make this thumbnails smaller?
Or somebody who can tell me, what i´m doing wrong?

Thanks in advance!!!
Basti
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Compression PNG / Image Size?

Post by el_supremo »

The PNG compression quality value is explained here:
http://www.imagemagick.org/Usage/formats/#png_quality

Try a compression quality of 95 instead of 30

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
bastimm

Re: Compression PNG / Image Size?

Post by bastimm »

Hello Pete,

Thx for this very fast reply!!!

now my code is like this:

Code: Select all

$newimage->setImageDepth(8);
$newimage->setCompressionQuality(95);
now the image is about 53kB large instead of 73kB, but i think its yet too big!!!

Is there anything i did wrong?
Usually an image with this size (195/130) should be ~15kB large! And not 53kB?

Thanks a lot!
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Compression PNG / Image Size?

Post by el_supremo »

There may be meta data in the image, such as EXIF info. In MagickWand you would use the MagickStripImage function to remove the metadata. I presume there's an equivalent way of doing this with IMagick.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
bastimm

Re: Compression PNG / Image Size?

Post by bastimm »

Hello Pete,

first thanks for your answer, but it didn´t help....

Code: Select all

$newimage->stripImage();
doesn´t change the size of my png-thumbnail :s
always the size is more than 50% larger than it should be :s


I noticed:

Code: Select all

$tempFile = "gallery/large/1.jpg";

$newWidth = 195;
$newHeight = 130;


$image = new Imagick($tempFile);

$image->stripImage();
$imageWidth = $image->getImageWidth();
$imageHeight = $image->getImageHeight();
	


$ratioWidth = $newWidth/$imageWidth;
$ratioHeight = $newHeight/$imageHeight;
if ($ratioWidth < $ratioHeight){
	$ratio = $ratioHeight;
	$newCutWidth = $newWidth;
	$newCutHeight = $newHeight;
	
	$newHeight = intval($ratio * $imageHeight);
	$newWidth = intval($ratio * $imageWidth);
}
else {
	$newCutWidth = $newWidth;
	$newCutHeight = $newHeight;
	
	$ratio = $ratioWidth;
	$newHeight = intval($ratio * $imageHeight);
	$newWidth = intval($ratio * $imageWidth);
}
$image->resizeImage($newWidth, $newHeight,0,1);
	
$image->cropImage($newCutWidth,$newCutHeight, 0,0);

header("Content-type: image/jpeg");	

echo $image;die();
here the filesize is about 8kB!!!

If I add this:

Code: Select all

$image->setImageFormat("png");
$image->setImageInterlaceScheme(Imagick::INTERLACE_PNG);
$image->setCompressionQuality(95);
$image->setImageDepth(8);

header("Content-type: image/png");	
now the filesize is about 80kB!!!!

can somebody help me to find my failure?
Thanks in advance
bastimm

Re: Compression PNG / Image Size?

Post by bastimm »

no one here, who can help me?
Please tell me what i´m doing wrong?

Thanks in advance
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compression PNG / Image Size?

Post by fmw42 »

mindVex

Re: Compression PNG / Image Size?

Post by mindVex »

I have the same problem, I have an alphatransparent png which is a little bit to big. If I open it in Photoshop and save it without modifications it filesize is about 60% of the original one.

A code snippet:

Code: Select all

	$thumb->setImageFormat('png24');
	$thumb->setImageDepth(8);
	$thumb->setImageInterlaceScheme(Imagick::INTERLACE_PNG);
	
	$thumb->setCompression(Imagick::COMPRESSION_LZW);
	$thumb->setCompressionQuality(95);
	$thumb->setImageCompression(Imagick::COMPRESSION_LZW);
	$thumb->setImageCompressionQuality(95);
identifyImage() gets me this:

Code: Select all

'format' => string 'png24 (opaque 24-bit RGB)' (length=25)
  'geometry' => 
    array
      'width' => int 240
      'height' => int 198
  'type' => string 'TrueColorMatte' (length=14)
  'colorSpace' => string 'RGB' (length=3)
  'resolution' => 
    array
      'x' => float 96
      'y' => float 96
  'units' => string 'PixelsPerInch' (length=13)
  'fileSize' => string '84.043kb' (length=8)
  'compression' => string 'LZW' (length=3)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compression PNG / Image Size?

Post by fmw42 »

you may have to use non-IM tools. see http://www.imagemagick.org/Usage/formats/#png_non-im
Post Reply