PHP iMagick图像压缩

时间:2009-12-22 10:35:07

标签: php compression jpeg 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(); 
    }
}

非常感谢任何帮助。

非常感谢

罗布

3 个答案:

答案 0 :(得分:9)

试试这个:

$im->setImageCompression(Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(20);

答案 1 :(得分:3)

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

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

$im->setImageCompression(true);

答案 2 :(得分:0)

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

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