PHP iMagick 图像压缩
我对 iMagick 相当陌生,只找到了非常有限的 PHP 库文档。我很高兴调整图像大小并将它们写回到硬盘驱动器,但我完全无法使用 JPG 压缩图像。
这是我到目前为止使用的代码:
function scale_image($size = 200,$extension)
{
if(!file_exists(ALBUM_PATH . $this->path . $this->filename . $extension))
{
$im = new imagick(ALBUM_PATH . $this->path . $this->filename);
$width = $im->getImageWidth();
$height = $im->getImageHeight();
if($width > $height)
$im->resizeImage($size, 0, imagick::FILTER_LANCZOS, 1);
else
$im->resizeImage(0 , $size, imagick::FILTER_LANCZOS, 1);
$im->setImageCompression(true);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(20);
$im->writeImage(ALBUM_PATH . $this->path . $this->filename . $extension);
$im->clear();
$im->destroy();
}
}
I'm fairly new to iMagick and have only found very limited documentation on the PHP library. I'm happily resizing images and writing them back to the hard drive, but I'm failing completely to compress the images using JPG for instance.
This is the code I'm using so far:
function scale_image($size = 200,$extension)
{
if(!file_exists(ALBUM_PATH . $this->path . $this->filename . $extension))
{
$im = new imagick(ALBUM_PATH . $this->path . $this->filename);
$width = $im->getImageWidth();
$height = $im->getImageHeight();
if($width > $height)
$im->resizeImage($size, 0, imagick::FILTER_LANCZOS, 1);
else
$im->resizeImage(0 , $size, imagick::FILTER_LANCZOS, 1);
$im->setImageCompression(true);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(20);
$im->writeImage(ALBUM_PATH . $this->path . $this->filename . $extension);
$im->clear();
$im->destroy();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
setImageCompression 似乎期望一个整数作为参数而不是布尔值(请参阅:http://www.php.net/manual/en/function.imagick-setimagecompression.php)。
我认为如果禁用此行,图像压缩可能会起作用:
setImageCompression seems to expect an integer as parameter rather than a boolean (see : http://www.php.net/manual/en/function.imagick-setimagecompression.php).
I think image compression may work if you disable this line :
源代码中压缩格式的完整列表:
原始文档: http: //www.imagemagick.org/script/command-line-options.php#compress
The full list of compression formats from a source code:
Original documentation: http://www.imagemagick.org/script/command-line-options.php#compress