...

View Full Version : need help in resizing image



doforumda
02-24-2010, 08:34 AM
hi

i am trying to resize an image to thumbnail size. the code below is working. but the problem with this is when my image is resizes to thumbnail size then its quality gets bad. so how can i maintain the quality of the image after resizing it?


<?php
function imagetothumb($imagename, $extension) {
$save = "thumb/".$imagename;
$filename = $imagename;//'image.jpg';
$percent = 0.05;
//$width = 50;
//$height = 50;

header('Content-type: image/'.$extension);

list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($thumb,$save,100);
}


function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$imagename = "image3.jpg";
$ext = getExtension($imagename);
$ext = strtolower($ext);

//imageresizer($imagename,$ext);

imagetothumb($imagename,$ext);

?>

mlseim
02-24-2010, 12:27 PM
You'll want to use "imagecopyresampled" instead of "imagecopyresized".

So use this:
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

Instead of this:
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);


If I didn't get the properties right in my example, look it up in the PHP manual:
http://php.net/manual/en/function.imagecopyresampled.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum