Image Quality Loss Resize - Phalcon GD vs PHP GD

To the left is an image created by Phalcon, to the right - by using PHP directly.

Click here

As you see, quality loss and difference when using Phalcon is significant. What could be the problem?

Here's Phalcon code:

$newWidth = 160;
$image = new \Phalcon\Image\Adapter\GD($filename);
$width = $image->getWidth();
$height = $image->getHeight();

$newHeight = floor($height * $newWidth / $width);

$image->resize($newWidth, $newHeight);
$explode = explode('.', $filename);
$ext = array_pop($explode);
$path = new Path();
$postfix = ($newWidth == $this->config->media->thumb) ? 'tn' : $newWidth;
$image->save($path->buildUploadDir($id, $ext, $postfix));

Here's PHP directly:

$w = 160;
$file = APP_PATH . 'public/tmp/test.jpg';
list($width, $height) = getimagesize($file);
$r = $width / $height;
$h = floor($r*$height);

$newwidth = $h*$r;
$newheight = $h;
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($dst, APP_PATH . 'public/tmp/tn.jpg');

What could be the issue? Is this a Phalcon bug which should be reported?



78.1k

my advice for people working wiuth images is never to use premade php packages or framework functions. Simply do it yourself, i mean .. you already did It anyways ?

you check check the source to see what phalcon is doing here https://github.com/phalcon/cphalcon/blob/master/phalcon/image/adapter/gd.zep#L126

p.s. if you feel brave enough use image magick ( not imagick !!! ) with shell_exec. This is what I came up with, for working with images last ~7 years. Imagemagick simply can do eveything you need, its just for some reson people are scared of using shell_exec / exec comands at all + its not exactly easy thing to learn, i had to ask more than few questions on stack overflow when i had to do complex image generations.

Unfortunately, my localhost is on Windows and I was not able to install Imagick there. I gave up after 4 failed attempts.

I have the same problem. Can you check this and tell me. Change interpolation mode with imagesetinterpolation

imagesetinterpolation($image->getImage(), IMG_BICUBIC_FIXED);

Try with this four algorithm IMG_NEAREST_NEIGHBOUR, IMG_BILINEAR_FIXED (default), IMG_BICUBIC, IMG_BICUBIC_FIXED.

Good luck

my advice for people working wiuth images is never to use premade php packages or framework functions. Simply do it yourself, i mean .. you already did It anyways ?

you check check the source to see what phalcon is doing here https://github.com/phalcon/cphalcon/blob/master/phalcon/image/adapter/gd.zep#L126

p.s. if you feel brave enough use image magick ( not imagick !!! ) with shell_exec. This is what I came up with, for working with images last ~7 years. Imagemagick simply can do eveything you need, its just for some reson people are scared of using shell_exec / exec comands at all + its not exactly easy thing to learn, i had to ask more than few questions on stack overflow when i had to do complex image generations.

The only difference are these two lines

imagealphablending($dst, true);
imagesavealpha($dst, true);

Otherwise, everything's identical. And adding them does not change anything. If solution is "using imagick" (which is an absolute nightmare to install on windows), then I shall add this as a bug report to Phalcon.