PHP iMagick 图像压缩

发布于 2024-08-15 12:03:00 字数 947 浏览 5 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦毁影碎の 2024-08-22 12:03:00

试试这个:

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

Try this:

$im->setImageCompression(Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(20);
睫毛溺水了 2024-08-22 12:03:00

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

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

$im->setImageCompression(true);

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 :

$im->setImageCompression(true);
一个人的夜不怕黑 2024-08-22 12:03:00

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

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

The full list of compression formats from a source code:

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;

Original documentation: http://www.imagemagick.org/script/command-line-options.php#compress

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文