Imagick::setCompressionQuality
(PECL imagick 2, PECL imagick 3)
Imagick::setCompressionQuality — Sets the object's default compression quality
说明
public Imagick::setCompressionQuality(int
$quality
): boolSets 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
-
An int between 1 and 100, 1 = high compression, 100 low compression.
返回值
成功时返回 true
。
范例
示例 #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();
}
?>

User Contributed Notes 4 notes
deeps chennai ¶
12 years ago
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)
charles dot hall at sas dot com ¶
11 years ago
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);
?>
nVaux.com ¶
14 years ago
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/*.jpg'));
foreach($images as $image)
{
// compression methods, see "Contants"-page for Imagick
$image->setCompression(imagick::COMPRESSION_JPEG);
// a value between 1 and 100, 1 = high compression, 100 low compression
$image->setCompressionQuality(80);
$image->writeImage();
}
?>
sebastian dot moser at gmail dot com ¶
14 years ago
Use this example to see how image compression works:
<?php
// load an image
$img = new Imagick("test.jpg");
// compression methods, see "Contants"-page for Imagick
$img->setComression(Imagick::COMPRESSION_JPEG);
// a value between 1 and 100, 1 = high compression, 100 low compression
$img->setComressionQuality(80);
// set content type
header("Content-type: image/jpeg");
// write image
echo $img->getImageBlob();
?>
备份地址:http://www.lvesu.com/blog/php/imagick.setcompressionquality.php