2016-05-23 08:56
浏览 693

PHP - 使用Imagick将所有图像转换为jpg - 质量差

I found more topics from this web site about quality with Imagick but nothing help me... I have to save all images as JPG. I created this script:

$image_url = '';
$image_code = file_get_contents($image_url);

$img = new Imagick();
$img -> readImageBlob($image_code);
$img->setResolution(300, 300);
$d = $img->getImageGeometry(); 
$img->cropImage($d['width'],($d['height']-120), 0,0);

echo '<img src="read.jpg?'.time().'">';exit;

Here is original image: and here is image which was converted by my script:

Where is a problem? Why this image is always convert in bad quality?


图片转代码服务由CSDN问答提供 功能建议

我从这个网站上找到了更多关于Imagick质量的主题,但没有什么能帮到我...... 我必须保存 所有图像均为JPG。 我创建了这个脚本:

  $ image_url ='';
$image_code  = file_get_contents($ image_url); 
 $ img = new Imagick(); 
 $ img  - &gt;  readImageBlob($ image_code); 
 $ img-&gt; setResolution(300,300); 
 $ d = $ img-&gt; getImageGeometry();  
 $ img-&gt; cropImage($ d ['width'],($ d ['height']  -  120),0,0); 
 $ img-&gt; setImageFormat('jpeg'); 
  $ img-&GT; setImageCompression(imagick :: COMPRESSION_JPEG);  
 $的img-&GT; setCompressionQuality(100);  
 $ img-&gt; writeImage('read.jpg'); 
 $ img-&gt; clear(); 
echo'&lt; img src =“read.jpg?'。time()。'”  &gt;';退出; 

这是原始图片: 这里是由我的脚本转换的图像:

问题出在哪里? 为什么这个图像总是转换质量差?


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dsfsdfsd34324 2016-05-23 09:03

    The image is not in "bad quality" (there is no blurry areas found), but the difference between 2 images is caused by transparent PNG to JPG conversion.

    Before you crop the image, add these two lines:

    // set background to white (Imagick doesn't know how to deal with transparent background if you don't instruct it)
    $img->setImageBackgroundColor(new ImagickPixel('white'));
    // flattens multiple layers
    $img = $img->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
    解决 无用
    打赏 举报

相关推荐 更多相似问题