note 81854 added to function.imagejpeg
| From: | Anonymous at osu1 dot php dot net | Date: | Sun, 16 Mar 2008 18:24:57 +0000 |
| Subject: | note 81854 added to function.imagejpeg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137107@lists.php.net to get a copy of this message | ||
Here is a function to resize an image and maintain aspect ratio. It will resize jpeg, gif or png and
could easily be modified to add bmp. The name field is the destination of the file minus the file
extension:
<?php
//name= filename minus type
function createImage($uploadedfile,$newWidth,$name)
{
// Capture the original size of the uploaded image
if(!$info=getimagesize($uploadedfile))
return false;
switch ($info['mime'])
{
case 'image/jpeg':
$src = imagecreatefromjpeg($uploadedfile);
break;
case 'image/gif':
$src = imagecreatefromgif($uploadedfile);
break;
case 'image/png':
$src = imagecreatefrompng($uploadedfile);
break;
default:
return false;
}
//Change the filename to add the filetype
$mime=split("image/",$info['mime']);
$filename=$name.".".$mime[1];
$size = getimagesize($uploadedfile);
$newHeight=aspect_ratio($size[0],$newWidth,$size[1]);
$tmp=imagecreatetruecolor($newWidth,$newHeight);
// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newWidth,$newHeight,$info[0], $info[1]);
switch ($info['mime'])
{
case 'image/jpeg':
imagejpeg($tmp,$filename,100); //100 is the quality settings, values range from 0-100.
break;
case 'image/gif':
imagegif($tmp,$filename,100); //100 is the quality settings, values range from 0-100.
break;
case 'image/png':
imagepng($tmp,$filename); //100 is the quality settings, values range from 0-100.
break;
}
imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
// has completed.
return true;
}
if(!createImage($uploadedfile,100, "uploaded_images/imgname"))
{
echo "error";
}
?>
----
Server IP: 83.170.119.58
Probable Submitter: 81.154.215.249
----
Manual Page -- http://www.php.net/manual/en/function.imagejpeg.php
Edit -- https://master.php.net/note/edit/81854
Del: integrated -- https://master.php.net/note/delete/81854/integrated
Del: useless -- https://master.php.net/note/delete/81854/useless
Del: bad code -- https://master.php.net/note/delete/81854/bad+code
Del: spam -- https://master.php.net/note/delete/81854/spam
Del: non-english -- https://master.php.net/note/delete/81854/non-english
Del: in docs -- https://master.php.net/note/delete/81854/in+docs
Del: other reasons-- https://master.php.net/note/delete/81854
Reject -- https://master.php.net/note/reject/81854
Search -- https://master.php.net/manage/user-notes.php