note 41446 added to function.imagecopyresized

From: Date: Sat, 10 Apr 2004 06:37:36 +0000
Subject: note 41446 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-67946@lists.php.net to get a copy of this message
I have built this function and it is fast as far as I'm concerned. Just input the source file and the destination file (the thumbnail filename you want to create). Note: set the $maxwidth and $maxheight variables before calling the function or make adjustments in the functions. I have set the thumbnail size according to some ratios that I need, but you might want to change that. <? function createThumb($sfile,$dfile) { // the passed variables are string filenames, the source and the destination global $maxwidth,$maxheight; $simg = imagecreatefromjpeg($sfile); $currwidth=imagesx($simg); $currheight=imagesy($simg); //set the dimensions of the thumbnail if ($currheight>$currwidth*1.7) { $zoom=$maxheight/$currheight; $newheight=$maxheight; $newweight=$currweight*$zoom; } else { $zoom=$maxwidth/$currwidth; $newwidth=$maxwidth; $newheight=$currheight*$zoom; } //create the resource img for the thumbnail $dimg = imagecreate($newwidth, $newheight); //convert truecolor immage resource to palette image resource (so we can count the colors...) imagetruecolortopalette($simg, false, 256); $palsize = ImageColorsTotal($simg); for ($i = 0; $i<$palsize; $i++) { $colors = ImageColorsForIndex($simg, $i); ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); } imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); imagejpeg($dimg,$dfile); ImageDestroy($simg); ImageDestroy($dimg); } ?> use the function like this: <? $maxwidth=100; $maxheight=80; createThumb("bigImage.jpg","bigImage_htumb.jpg"); ?> ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41446 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41446&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41446&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67946) next »