ImAGe
ImAGe

Reputation: 45

How to Improve image quality when resized as a thumbnail using PHP?

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

?>

Upvotes: 2

Views: 1245

Answers (1)

mip
mip

Reputation: 8713

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

imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);

Upvotes: 2

Related Questions