note 41446 deleted from function.imagecopyresized by aidan
| From: | aidan@php.net | Date: | Mon, 04 Oct 2004 09:31:24 +0000 |
| Subject: | note 41446 deleted from function.imagecopyresized by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77900@lists.php.net to get a copy of this message | ||
Note Submitter: Felix Rusu <webmaster at betania dot ro>
----
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;
$newwidth=$currwidth*$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");
?>