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