My Imagick code never seems to resize and still produce a sharp image
我担心Imagick可能不支持清晰的图像尺寸调整。即使是最好的过滤器,也会返回模糊的结果,这些结果低于标准,在我的拙见中,这甚至是可怕的。
这是我可以从Imagick获得的最佳质量:
使用TimThumbs调整大小:
注意巨大的质量差异吗?任何人都可以提供Imagick产生的清晰图像的示例吗?
以下是用于生成第一张图像的Imagick代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | function imgSize($imagePath,$imageName,$imageExt,$width,$height,$copy) { $file ="img/".$imageName."-".$width."x".$height.".".$imageExt; if (!file_exists($file)) { try { $image = $imagePath; $im = new Imagick(); $im->pingImage($image); $im->readImage($image); $im->setImageFormat($imageExt); //$im->thumbnailImage($width,$height,true); $im->cropThumbnailImage($width,$height); $im->resizeImage($width,$height,Imagick::FILTER_LANCZOS,0,false); $im->cropThumbnailImage($width,$height); if ($imageExt=("jpg"||"JPG")) { $img->setComression(Imagick::COMPRESSION_JPEG); $img->setComressionQuality(100); } if (!$copy==null) { $draw = new ImagickDraw(); $draw->setFont("fpdf/font/Montserrat-Regular.ttf"); $draw->setFontSize(35); $draw->setFillColor("Gray"); $draw->setFillAlpha(0.5); $draw->setGravity(Imagick::INTERPOLATE_AVERAGE); $im->annotateImage($draw,0,0,-45,$copy); } $im->writeImage("img/".$imageName."-".$width."x".$height.".".$imageExt); $im->destroy(); return"img/".$imageName."-".$width."x".$height.".".$imageExt; } catch(Exception $e) { echo $e->getMessage(); } } }; |
我知道也有类似的问题,但是对于此图像质量问题还没有足够的答案。解决Imagick图像质量问题的大多数尝试都是将问题缩小为"收缩图像时得到的结果"。但是我在本文中提供的TimThumbs图片示例证明了这一假设。您可以使用PHP缩小图像并获得惊人的效果。
这对我来说似乎更好,我只是将其调整为原始图像的20%。使用GD和100%质量的JPEG
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function resizeImg($filename, $percent, $saveTo) { list($width, $height) = getimagesize($filename); $newwidth = $width * $percent; $newheight = $height * $percent; $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($filename); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($thumb, $saveTo, 100); imagedestroy($thumb); } resizeImg('image.jpg', 0.2, 'image02.jpg'); |
我已经编译了php55-imagick扩展名以检查您的代码,首先,您固定了2个方法名称的拼写错误,当固定生成的图像实际上可以时
1 2 | $im->setCompression(Imagick::COMPRESSION_JPEG); $im->setCompressionQuality(100); |
这是具有showdev注释的正确方法的图像,图像大小增加了一倍(121K与329K),但对我来说并没有太大好处
1 2 | $im->setImageCompression(Imagick::COMPRESSION_JPEG); $im->setImageCompressionQuality(100); |