简体   繁体   English

ImageMagick 的 Perl API 创建非常大的 JPEG 缩略图

[英]ImageMagick's Perl API Creating Very Large JPEG Thumbnails

I'm using ImageMagick's Perl API to create thumbnails.我正在使用 ImageMagick 的 Perl API 创建缩略图。 I'm using suggestions on ImageMagick image optimization from this previous SO question :我正在使用上一个 SO question 中关于 ImageMagick 图像优化的建议:

open my $file, $params->{'openFile'};
my $imageData = do { local $/; <$file> };

if ($params->{'size'} eq "largesized") {
    $thumbnailWidth = 1000;
    $thumbnailHeight = 1000;
}   

$image->BlobToImage($imageData);
$image->SetAttribute('quality' => 80);
$image->SetAttribute('compression' => 'JPEG');
$image->SetAttribute('sampling-factor' => '4:2:0');
$image->SetAttribute('interlace' => 'Plane');
$image->SetAttribute('gaussian-blur' => '0.05');
$image->Thumbnail('geometry' => $thumbnailWidth . "x" . $thumbnailHeight);

my $thumbnailData = $image->ImageToBlob();


open(my $file, '>', $params->{'saveFile'}) or return { 'thumbnailGenerated' => 0, error => "Could not open file '" . $params->{'saveFile'} . "'." };
print $file $thumbnailData;
close $file;

As far as I can tell, this should create a nicely optimized thumbnail.据我所知,这应该会创建一个经过优化的缩略图。 However, while the input image is 3088x2320 and weighs in at 1.6 MB , the resulting thumbnail at 1000x1000 actually is larger at 2.3 MB (which seems positively huge for a JPEG of that size).然而,虽然输入图像是 3088x2320 并且重量为 1.6 MB但生成的 1000x1000 缩略图实际上更大,为 2.3 MB(对于这种大小的 JPEG 来说,这似乎是巨大的)。 Note: on the sample out, it is cropped a bit because the JavaScript that uploads the images forces one to select a 1:1 crop by design.注意:在示例中,它被裁剪了一点,因为上传图像的 JavaScript 会强制用户选择 1:1 裁剪。

I'm running ImageMagick 6.9.10-68 Q16 x86_64 2021-10-14 (CentOS).我正在运行 ImageMagick 6.9.10-68 Q16 x86_64 2021-10-14 (CentOS)。 The command line equivalent produces a smaller image as expected (84kb):命令行等效项按预期生成较小的图像(84kb):

  convert -thumbnail 1000x1000 -quality 80 -compress JPEG -sampling-factor 4:2:0 -interlace Plane -gaussian-blur 0.05  imageMagickSampleImage.jpg imageMagickSampleImage3.jpg

It makes me wonder if the SetAttribute attributes are not being passed to the Thumbnail function or something.这让我想知道SetAttribute属性是否没有传递给Thumbnail函数或其他东西。 I've found PerlMagick less than perfect at various points and have been wondering if I just need to go with GD instead.我发现 PerlMagick 在各个方面都不够完美,并且一直想知道我是否只需要使用GD

I notice that the thumbnails are still too large (and larger than the command line), but get smaller if I remove all of the "SetAttribute" lines.我注意到缩略图仍然太大(并且比命令行大),但如果我删除所有“SetAttribute”行会变小。 The output of the 1000x1000 thumbnail goes from 2.4MB to 976KB. 1000x1000 缩略图的输出从 2.4MB 变为 976KB。 That's still over 10x larger than the command line with these same attributes set.这仍然比具有这些相同属性集的命令行大 10 倍以上。

I thought maybe moving some of the attributes directly to the ImageToBlob method would help, but it didn't seem to change the outcome at all.我想也许将一些属性直接移动到ImageToBlob方法会有所帮助,但它似乎根本没有改变结果。 Here's that attempt:这是尝试:

my $thumbnailData = $image->ImageToBlob('quality' => 40, 'compression' => 'JPEG', 'sampling-factor' => '4:2:0', 'interlace' => 'Plane', 'gaussian-blur' => '0.05');

The issue apparently is that despite specifying JPEG compression and inputting a JPEG, the ImageToBlob method was outputting a PNG file, as @MarkSetchell observed in the comments above.问题显然是,尽管指定了 JPEG 压缩并输入了 JPEG,但 ImageToBlob 方法正在输出 PNG 文件,正如@MarkSetchell 在上面的评论中观察到的那样。

A helpful code snippet from Perl Graphics Programming offers the solution. Perl Graphics Programming的一个有用的代码片段提供了解决方案。 I modified my code accordingly to specifically tell ImageMagick to output a JPEG:我相应地修改了我的代码,专门告诉 ImageMagick 输出 JPEG:

 my $thumbnailData = $image->ImageToBlob(magick => 'jpeg');

The 1000x1000 graphic went from its previously massive size to just 167KB. 1000x1000 的图形从之前的巨大尺寸变为只有 167KB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM