note 41446 modified in function.imagecopyresized by vincent
| From: | vincent@php.net | Date: | Tue, 27 Apr 2004 18:20:14 +0000 |
| Subject: | note 41446 modified in function.imagecopyresized by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-68898@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;
$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");
?>
--was--
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");
?>
http://www.php.net/manual/en/function.imagecopyresized.php