Page 1 of 1

Compression PNG / Image Size?

Posted: 2009-12-03T01:26:24-07:00
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

Re: Compression PNG / Image Size?

Posted: 2009-12-03T10:28:49-07:00
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

Re: Compression PNG / Image Size?

Posted: 2009-12-04T00:38:32-07:00
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!

Re: Compression PNG / Image Size?

Posted: 2009-12-04T08:19:32-07:00
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

Re: Compression PNG / Image Size?

Posted: 2009-12-08T01:19:24-07:00
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

Re: Compression PNG / Image Size?

Posted: 2009-12-11T06:54:39-07:00
by bastimm
no one here, who can help me?
Please tell me what i´m doing wrong?

Thanks in advance

Re: Compression PNG / Image Size?

Posted: 2009-12-11T10:01:56-07:00
by fmw42

Re: Compression PNG / Image Size?

Posted: 2010-03-31T03:44:58-07:00
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)

Re: Compression PNG / Image Size?

Posted: 2010-03-31T08:58:12-07:00
by fmw42
you may have to use non-IM tools. see http://www.imagemagick.org/Usage/formats/#png_non-im