Low quality in php imagejpeg() with quality=100

Viewed 78

I try to use simple resize/crop script to create thumbnails.

$imageObject = imagecreatefromstring(file_get_contents($path.$image));
$originalWidth = imagesx($imageObject);
$originalHeight = imagesy($imageObject);
$originalAspect = $originalWidth/$originalHeight;
$thumbAspect = $thumbWidth/$thumbHeight;
$newWidth = 0;
$newHeight = 0;
if($originalAspect >= $thumbAspect) {
    $newHeight = $thumbHeight;
    $newWidth = $originalWidth/($originalHeight/$thumbHeight);
}
else {
    $newWidth = $thumbWidth;
    $newHeight = $originalHeight/($originalWidth/$thumbWidth);
}

$thumb = imagecreatetruecolor($thumbWidth,$thumbHeight);
$dstX = 0-($newWidth-$thumbWidth)/2;
$dstY = 0-($newHeight-$thumbHeight)/2;
if($ext=='.png') {
    imagealphablending($thumb, false);
    imagesavealpha($thumb, true);
    $transparentColor = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
    imagefill($thumb, 0, 0, $transparentColor);
}
$crop = imagecopyresampled($thumb,$imageObject,$dstX,$dstY,0,0,$newWidth,$newHeight,$originalWidth,$originalHeight);
switch ($ext) {
    case '.jpg': $save = imagejpeg($thumb, $cacheFile, 100); break;
    case '.png': $save = imagepng($thumb, $cacheFile, 2); break;
    case '.gif': $save = imagegif($thumb, $cacheFile, 90); break;
    case '.webp': $save = imagewebp($thumb, $cacheFile, 90); break;
}

$destroy = imagedestroy($thumb);

When this script converts png or webp formats, no problem. But if it converts jpeg formats (especially small pictures like 400x800) quality of resulting images are very low even I set quality parameter in imagejpeg as 100. Do I do something wrong or GD function work this way?

0 Answers
Related