note 86605 added to function.imagecreatefromjpeg
| From: | Ninjabear at osu1 dot php dot net | Date: | Sat, 25 Oct 2008 16:36:07 +0000 |
| Subject: | note 86605 added to function.imagecreatefromjpeg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145749@lists.php.net to get a copy of this message | ||
This code will allow you to resize any image (jpg, gif or png). It will even work with alpha
transparency on pngs. It will limit both dimensions so that neither is higher than requested
$thumb_width and preserve aspect ratio as well.
I searched for a very long time before finding this, I have made a few personal changes such as
those listed above.
Credit goes to "vandai" on www.akemapa.com.
<?php
function resize($img, $thumb_width, $newfilename)
{
$max_width=$thumb_width;
//Check if GD extension is loaded
if (!extension_loaded('gd') && !extension_loaded('gd2'))
{
trigger_error("GD is not loaded", E_USER_WARNING);
return false;
}
//Get Image size info
list($width_orig, $height_orig, $image_type) = getimagesize($img);
switch ($image_type)
{
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
/*** calculate the aspect ratio ***/
$aspect_ratio = (float) $height_orig / $width_orig;
/*** calulate the thumbnail width based on the height ***/
$thumb_height = round($thumb_width * $aspect_ratio);
while($thumb_height>$max_width)
{
$thumb_width-=10;
$thumb_height = round($thumb_width * $aspect_ratio);
}
$newImg = imagecreatetruecolor($thumb_width, $thumb_height);
/* Check if this image is PNG or GIF, then set if Transparent*/
if(($image_type == 1) OR ($image_type==3))
{
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent);
}
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig,
$height_orig);
//Generate the file, and rename it to $newfilename
switch ($image_type)
{
case 1: imagegif($newImg,$newfilename); break;
case 2: imagejpeg($newImg,$newfilename); break;
case 3: imagepng($newImg,$newfilename); break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}
return $newfilename;
}
echo resize("test4.png", 120, "thumb_test4.png")
?>
----
Server IP: 92.48.74.199
Probable Submitter: 82.39.112.228
----
Manual Page -- http://www.php.net/manual/en/function.imagecreatefromjpeg.php
Edit -- https://master.php.net/note/edit/86605
Del: integrated -- https://master.php.net/note/delete/86605/integrated
Del: useless -- https://master.php.net/note/delete/86605/useless
Del: bad code -- https://master.php.net/note/delete/86605/bad+code
Del: spam -- https://master.php.net/note/delete/86605/spam
Del: non-english -- https://master.php.net/note/delete/86605/non-english
Del: in docs -- https://master.php.net/note/delete/86605/in+docs
Del: other reasons-- https://master.php.net/note/delete/86605
Reject -- https://master.php.net/note/reject/86605
Search -- https://master.php.net/manage/user-notes.php