creating thumbnails

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

creating thumbnails

Post by dannymc1983 »

Im using the following code to create a thumbnail of an image stored in a database. the code works fine, however the quality of the thumbnail isnt so great. does anyone know of any changes i could make to the code to improve the quality?

Code: Select all

$imageType = //function to get image type from database
$data = //function to get image data from database
		
header("Content-type: $imageType");
	
// get originalsize of image
$im = imagecreatefromstring($data);
		
$width  = imagesx($im);
$height = imagesy($im);

// Set thumbnail-width to 100 pixel
$imgw = 100;

// calculate thumbnail-height from given width to maintain aspect ratio
$imgh = $height / $width * $imgw;

// create new image using thumbnail-size
$thumb = imagecreatetruecolor($imgw,$imgh);

// copy original image to thumbnail
imagecopyresized($thumb,$im,0,0,0,0,$imgw,$imgh,imagesx($im),imagesy($im));

// show thumbnail on screen
$out = imagejpeg($thumb);
print($out);
    
// clean memory
imagedestroy ($im);
imagedestroy ($thumb);

feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm getting pretty tired of fixing your posts to use

Code: Select all

tags.  [b]Read the posting code guidelines.[/b]

imagecopyresampled() instead of imagecopyresized().
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Also, send the second parameter to imagejpeg. By default the function makes a JPEG at about 75% quality. Send 100 along for best quality.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply