PDA

View Full Version : [PHP] Need Help With Image Resizing



Multiplex
12-10-2006, 12:59 AM
I made an image resizing function and it basically grabs an image, gets it dimensions, and then attempts to resize it based off a maxSize value which is the largest height OR width in pixels...

My only problem is I need a way to get GD to do some smoothing or anti-aliasing on the image to get rid of the jaggies... because this function doesn't do quite as good of a job as photoshop.

I was wondering if any PHP gurus here (if any) know of a GD function which smoothes images.

Here's the code if you're curious or need it:


<?php
function resizer($image,$maxSize) {

//LOAD SOURCE IMAGE AND GET DIMENSIONS
$sourceImage = imagecreatefromstring($image);
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);

//CALCULATE RESIZE VALUES
if($sourceWidth > $sourceHeight){
$percent = $maxSize / $sourceWidth;
} else {
$percent = $maxSize / $sourceHeight;
}

$newWidth = $percent * $sourceWidth;
$newHeight = $percent * $sourceHeight;

//BEGIN PUTTING THE NEW IMAGE TOGETHER
$targetImage = imagecreatetruecolor($newWidth,$newHeight);
$thumb = imagecopyresized($targetImage,$sourceImage,0,0,0,0 ,$newWidth,
$newHeight,$sourceWidth,$sourceHeight);

//CAPTURE IMAGE STREAM AND THROW IT INTO A VARIABLE
ob_start();
imagejpeg($targetImage,'',100);
$image = ob_get_contents();
ob_end_clean();

//RETURN IMAGE STRING, WIDTH, AND HEIGHT
return array(1 => $image,$newWidth,$newHeight);
} //END FUNCTION

$resizerArray = resizer(fread(fopen($tmp_name,"r"),filesize($tmp_name)),$img_size);
?>

Multiplex
12-10-2006, 01:10 AM
Neeeeeeeeevermind! I just realized I was using imagecopyresized instead of imagecopyresampled which became available with GD2.

Anyway, I made the change and now it's pumping out resized images on par with a photoshop resize.

Admins can delete this thread at their discretion hehe.

sir_digalot
12-10-2006, 10:16 PM
you know i was just going to point that out to you... honest ;) *LOL*