tags:

views:

126

answers:

1

I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image.

And is there a better way to create thumbnails if so can you point me to a tutorial on how to create thumbnails using PHP.

Here is the code below.

<?php
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth)
{
$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);

$ratio = $origWidth / $thumbWidth;
$thumbHeight = $origHeight / $ratio;

$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);

imagejpeg($thumbImg, "$thumbDirectory/$imageName");
}

createThumbnail("images", "pic.jpg", "images/thumbs/", 180);

?>
A: 

Use imagecopyresampled() instead imagecopyresized(). Check PHP's doc for usage of this function.

imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);
doc
Is this the best way? Its better then what I did originally, but its still a little pixelated,
ImAGe
try imagejpeg() with quality settings ie imagejpeg($thumbImg, "$thumbDirectory/$imageName", 100); 100 is best quality, default is 75. Or use other format for storing thumbnails cause jpg is better for large images. I would suggest png files (function imagepng())
doc