©
本文檔使用 php中文網手册 發布
(PECL imagick 0.9.10-0.9.9)
Imagick::setCompressionQuality — Sets the object's default compression quality
$quality
)Sets the object's default compression quality.
This method only works for new images e.g. those created through Imagick::newPseudoImage. For existing images you should use Imagick::setImageCompressionQuality() .
quality
成功时返回 TRUE
。
Example #1 Imagick::setCompressionQuality()
<?php
function setCompressionQuality ( $imagePath , $quality ) {
$backgroundImagick = new \ Imagick ( realpath ( $imagePath ));
$imagick = new \ Imagick ();
$imagick -> setCompressionQuality ( $quality );
$imagick -> newPseudoImage (
$backgroundImagick -> getImageWidth (),
$backgroundImagick -> getImageHeight (),
'canvas:white'
);
$imagick -> compositeImage (
$backgroundImagick ,
\ Imagick :: COMPOSITE_ATOP ,
0 ,
0
);
$imagick -> setFormat ( "jpg" );
header ( "Content-Type: image/jpg" );
echo $imagick -> getImageBlob ();
}
?>
[#1] charles dot hall at sas dot com [2010-08-03 11:31:08]
I had to insert a call to "stripImage()" in order to actually see the filesize shrink.
<?php
$img = new Imagick();
$img->readImage($src);
$img->setImageCompression(imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(90);
$img->stripImage();
$img->writeImage($dest);
?>
[#2] deeps chennai [2010-03-18 02:59:33]
A note for people who just couldn't get this working..
With PHP 5.1.6, the below works:
<?php
$img->setCompression(Imagick::COMPRESSION_JPEG);
$img->setCompressionQuality(80);
?>
However, with higher versions of PHP (I tried on PHP 5.2.10), the code has no effect (and there are no exceptions or warnings thrown by Imagick as well).
The code that works instead is:
<?php
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(80);
?>
and this is backwards compatible (Works on PHP 5.1.6 as well as 5.2.10)
[#3] nVaux.com [2008-03-26 20:24:04]
Sebastian's example works excellent, just one minor spelling mistake, it will give you an error otherwise.
<?php
$img->setCompression(Imagick::COMPRESSION_JPEG);
$img->setCompressionQuality(80);
?>
I used Sebastians example, and made one that compresses all the images within a directory:
<?php
$images = new Imagick(glob('images
$im = new Imagick ();
$im -> setFont ( "example.ttf" );
$im -> newPseudoImage ( 100 , 100 , "caption:Hello" );
?>