Hi, I have a function that I use within my upload script that creates a thumbnail of each image that is uploaded (using php's GD library). The problem is, the thumbnails look absolutely horrible. They are not so much distorted, but just VERY blurry. Here is my function that is creating the thumbnails:

function create_thumb($pathToImage, $pathToThumb, $thumbWidth)
		    {
		    $img = imagecreatefromjpeg($pathToImage);
		    $width = imagesx($img);
		    $height = imagesy($img);
		    $new_width = $thumbWidth;
		    $new_height = floor($height * ($thumbWidth / $width));
		    $tmp_img = imagecreatetruecolor($new_width, $new_height);
		    imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		    imagejpeg($tmp_img, $pathToThumb);
	      	    imagedestroy($tmp_img);
		    imagedestroy($img);
		    }
		    create_thumb('uploads/' . $name, 'uploads/thumbs/' . $name, 100);
                  echo "1";
              }

I have googled about this, and everything I read just tells me to make sure I have the newest version of the GD library, to use imagecopyresample (instead of imagecopyresize) and to use imagecreatetruecolor (instead of imagecreate). Which, as you can see, I do use all of those.

Any help is appreciated!

Recommended Answers

All 4 Replies

Default quality of imagejpeg is 75, try setting it to 100.

pritaeas,

Thanks! That actually helped quite a bit. However, they still are a little blurry. - And this is for a photographer's website. Is there a reason why the thumbnails of the pictures that are portrait instead of landscape (the pics that are more tall than they are wide) are absolutely crystal clear! - but the landscape pics have a blur to them???

Thanks for your help!

From your code I read that you set the new width to 100px. That would mean that the landscape photos are reduced more than the portrait ones. That would explain why they are more clear. I suggest you find a percentage in which both types are clear, and use that instead of setting it to a fixed 100px.

Wow, I'm an idiot...I TOTALLY forgot that before I got this GD function working i was setting the height of the images in my gallery hard coded when I display each image. haha. Everything works GREAT now!

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.