4

我对 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(); 
    }
}
4

3 回答 3

10

尝试这个:

$im->setImageCompression(Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(20);
于 2010-11-24T21:06:26.947 回答
3

setImageCompression似乎期望一个整数作为参数而不是布尔值(参见:http ://www.php.net/manual/en/function.imagick-setimagecompression.php )。

我认为如果禁用此行,图像压缩可能会起作用:

$im->setImageCompression(true);
于 2009-12-30T11:50:25.023 回答
1

源代码中压缩格式的完整列表:

const COMPRESSION_NO = 1;
const COMPRESSION_BZIP = 2;
const COMPRESSION_FAX = 6;
const COMPRESSION_GROUP4 = 7;
const COMPRESSION_JPEG = 8;
const COMPRESSION_JPEG2000 = 9;
const COMPRESSION_LOSSLESSJPEG = 10;
const COMPRESSION_LZW = 11;
const COMPRESSION_RLE = 12;
const COMPRESSION_ZIP = 13;
const COMPRESSION_DXT1 = 3;
const COMPRESSION_DXT3 = 4;
const COMPRESSION_DXT5 = 5;
const COMPRESSION_ZIPS = 14;
const COMPRESSION_PIZ = 15;
const COMPRESSION_PXR24 = 16;
const COMPRESSION_B44 = 17;
const COMPRESSION_B44A = 18;
const COMPRESSION_LZMA = 19;
const COMPRESSION_JBIG1 = 20;
const COMPRESSION_JBIG2 = 21;

原始文档:http ://www.imagemagick.org/script/command-line-options.php#compress

于 2019-05-30T09:50:12.213 回答