Reputation: 11
I am having problems creating a thumbnail from an uploaded image, my problem is
(i) the quality (ii) the crop
http://welovethedesign.com.cluster.cwcs.co.uk/phpimages/large.jpg http://welovethedesign.com.cluster.cwcs.co.uk/phpimages/thumb.jpg
If you look the quality is very poor and the crop is taken from the top and is not a resize of the original image although the dimesions mean it is in proportion.
The original is 1600px
wide by 1100px
high.
Any help would be appreciated.
$thumb =
$targetPath."Thumbs/".$fileName;
$imgsize =
getimagesize($targetFile); $image =
imagecreatefromjpeg($targetFile);
$width = 200; //New width of image
$height = 138; //This maintains
proportions
$src_w = $imgsize[0]; $src_h =
$imgsize[1];
$thumbWidth = 200; $thumbHeight =
138; // Intended dimension of thumb
// Beyond this point is simply code.
$sourceImage =
imagecreatefromjpeg($targetFile);
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);
$targetImage =
imagecreate($thumbWidth,$thumbHeight);
imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbWidth,imagesx($sourceImage),imagesy($sourceImage));
//imagejpeg($targetImage,
"$thumbPath/$thumbName");
imagejpeg($targetImage, $thumb);
chmod($thumb, 0755);
Upvotes: 1
Views: 481
Reputation: 44
You would not worry if you would use the Thumbnailer.
$th=new Thumbnailer("your-photo.jpg");
$th->thumbSymmetricWidth(200)->save("your-thumb.jpg");
The quality is superb. You can also round the corners.
Upvotes: 0
Reputation: 23244
You're using the wrong variable for the image height.
imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbWidth,imagesx($sourceImage),imagesy($sourceImage));
Should be:
imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbHeight,imagesx($sourceImage),imagesy($sourceImage));
This should improve image quality but you should use imagecopyresampled for resizing and use the quality parameter when using the imagejpeg() function when saving to disk.
Upvotes: 1
Reputation: 3678
Every time u create a thumbnail the DPI of the image has to go low and thus it is not possible to have the same quality, however u can check imagecreatetruecolor (https://www.php.net/manual/en/function.imagecreatetruecolor.php ) for improvement
Upvotes: 1