spencerthayer
spencerthayer

Reputation: 77

My Imagick code never seems to resize and still produce a sharp image

I fear that Imagick may not support sharp image resizing. Even the best filters are returning blurry results which are sub par, even horrible in my humble opinion.

This is the BEST quality I can get out of Imagick: Imagick

This is resized using TimThumbs: TimThumbs

NOTICE the drastic quality difference? Can anyone provide an example of a sharp image being produced by Imagick?

Below is the Imagick code used to generate the first image:

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(); }
    }
};

I am aware there are similar questions but there has not been a sufficient answer to this image quality problem. Most attempts to address Imagick's image quality issues are to reduced the problem to, "well that's what you get when you shrink an image." But the TimThumbs image example I have provided in this post disproves that assumption. You can shrink an image and get stunning results using PHP.

Upvotes: 2

Views: 972

Answers (1)

BojanT
BojanT

Reputation: 801

This seems better to me, I just resized it 20% of original image. Using GD and 100% quality as JPEG

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');

GD

I have compiled php55-imagick extension to check your code , first of all you have misspelled 2 method names , when fixed resulting image is actually OK setCompression not setComression

$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100);

Imagick

Here is image with correct methods as showdev commented , doubling image size (121K vs 329K ) , but to me not much benefit

$im->setImageCompression(Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(100);

Imagick using setImageCompression

Upvotes: 3

Related Questions