views:

37

answers:

2

Hi,

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

+1  A: 

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 (http://in2.php.net/manual/en/function.imagecreatetruecolor.php ) for improvement

nik
A: 

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.

zaf
thanks got an acceptable result from that.
Ross
@Ross, if this answer solved your problem accept it as the correct answer
John Conde
With an upvote or two! ;)
zaf
You may want to use `imagecopyresampled()` as well. resize is the 'low quality' version, if you're in a hurry and don't care about the quality of the resize. Resample is slower but does a better job.
Marc B