Page 1 of 1

Resizing Images with the GD Library

Posted: Tue Jan 31, 2006 9:36 am
by kendall
Hello,

I have a website for which i built an administration interface which allows admin to upload a jpeg image which is used to create a thumbnail image.

My problem is i find that the thumbnail isnt as crisp as it should be...its so fuzzy.

http://www.langstonroach.com/products.php?CATID=3

now its not really the image were looking at here its the text as compared to a thumbnail created by a graphic application like photoshop/ fireworks.

if you've looked at the link above the "perk" image was created with the gd library while the others were from a pplication software.

I have done this before but it hasnt really affected it as much as this has

my code to create the thumbnail is

Code: Select all

// Thumbnail function
function ImageResize($filename,$URL,$width,$height,$border=false){
        // build image
        $img = imagecreatefromjpeg($URL);// image to use
        // get image size
        $img_width = imagesx($img);
        $img_height = imagesy($img);
        // work out ratio
        $new_width = $width;
		if(!$height)
        	$new_height = ceil($img_height / ($img_width / $width));
		else $new_height = $height;
        $new_image = imagecreatetruecolor($new_width, $new_height); // blank image
        imagecopyresized($new_image,$img,0,0,0,0,$new_width,$new_height,$img_width,$img_height);
        // draw border
		if($border != false){
			$bordercolor = imagecolorallocate($new_image, 174, 212, 242); // create colour
			imagesetthickness($new_image,4);
			imageline ($new_image, 0, 0, $new_width, 0, $bordercolor); // top border
			imageline ($new_image, $new_width-1, 0, $new_width-1, $new_height, $bordercolor); // right border
			imageline ($new_image, 0, $new_height-1, $new_width, $new_height-1, $bordercolor); // bottom border
			imageline ($new_image, 0, 0, 0, $new_height, $bordercolor); // left border
 		}
        // lose original image
		//umask(0);
		//@chmod($filename);
        if(file_exists($filename))
            @unlink($filename);
        // save image
        imagejpeg($new_image,$filename,75);
        imagedestroy($new_image);
        // if save successful return true
        if(file_exists($filename))
                return true;
        else return false;
}
is there any advice here on how to make it better?

Kendall

Posted: Tue Jan 31, 2006 9:47 am
by feyd

Posted: Tue Jan 31, 2006 9:51 am
by kendall
So i should use resampled rather than resized?

Posted: Tue Jan 31, 2006 9:57 am
by feyd
yes.

Posted: Tue Jan 31, 2006 9:57 am
by pickle
PHP Manual wrote: imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.

imagecopyresized() copies a rectangular portion of one image to another image.
You'll be surprised how much of a better picture you get (Or maybe not, but it's a pretty stark difference ;))