doujindou4356 2016-05-23 08:56
浏览 735
已采纳

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 = 'http://limuzynamercedes.pl/wp-content/uploads/2014/06/3.png';
$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);
$img->setImageFormat('jpeg');
$img->setImageCompression(imagick::COMPRESSION_JPEG); 
$img->setCompressionQuality(100); 
$img->writeImage('read.jpg');
$img->clear();

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

Here is original image: http://limuzynamercedes.pl/wp-content/uploads/2014/06/3.png and here is image which was converted by my script: http://s5.ifotos.pl/img/demo1jpg_saeaxqx.jpg

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

Thanks.

  • 写回答

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?