[英]php imagick won't save PNG compressed but shows compressed in browser
I have the following code in PHP to take the screenshot of first page of the PDF.我在 PHP 中有以下代码来截取 PDF 第一页的屏幕截图。
$name = getcwd()."\\testfile";
$img = new imagick();
$img->setResolution(200,200);
$img->readImage($name.'.pdf[0]');
$img->setImageResolution(100,100);
$img->resampleImage(100,100,imagick::FILTER_LANCZOS,1);
$img->setImageCompression(\Imagick::COMPRESSION_ZIP );
$img->setImageCompressionQuality('0');
$img->setImageFormat('png8');
$img->writeImage($name.".png");
header("Content-type : image/png");
echo $img;
This code produces the PNG of 62kb only in the Google Chrome's Resource monitor tab.此代码仅在 Google Chrome 的资源监视器选项卡中生成 62kb 的 PNG。 But the image which is written by Imagick() is above 114kb.
但是由 Imagick() 写入的图像超过 114kb。 Just to make sure image isn't compressed and or any other issues i have used a online service called TinyPNG and they compressed the image shrinking it to exactly 62kb i get in browser...
只是为了确保图像没有被压缩或任何其他问题,我使用了一个名为 TinyPNG 的在线服务,他们压缩了图像,将其缩小到我在浏览器中得到的 62kb ......
What could be wrong in this code?这段代码可能有什么问题? Also i am using PNG8 format because thats more efficient.
我也使用 PNG8 格式,因为那更有效。
Best最好的
Ahsan阿山
I think this is caused by your writeImage statement.我认为这是由您的 writeImage 语句引起的。 If you write a PNG image without specifying png8: specifically in the filename your image will not be stored in that format.
如果您编写 PNG 图像而不指定 png8: 特别是在文件名中,您的图像将不会以该格式存储。 In essence setImageFormat will only affect when you retrieve the image as a string (echo $img).
本质上,setImageFormat 只会影响您将图像作为字符串检索(echo $img)。
If you do the following:如果您执行以下操作:
$img->writeImage ('png8:' . $name . ".png");
it should be stored as a png8.它应该存储为 png8。 You can verify this with identify -verbose and checking the Depth / Channel Depth.
您可以使用 identify -verbose 并检查深度/通道深度来验证这一点。
These are the default compression methods used for the following common image formats:这些是用于以下常见图像格式的默认压缩方法:
PNG: Imagick::COMPRESSION_ZIP
JPEG: Imagick::COMPRESSION_JPEG
GIF: Imagick::COMPRESSION_LZW
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.