note 30690 deleted from function.imagecopyresized by aidan
| From: | aidan@php.net | Date: | Mon, 04 Oct 2004 09:39:48 +0000 |
| Subject: | note 30690 deleted from function.imagecopyresized by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77919@lists.php.net to get a copy of this message | ||
Note Submitter: agotterer at email dot com
----
I wrote a JPEG resize function that will allow you to spefify locations, quality, as well as
determine aspect ration. I guess it could be good for thumbnailing...
//resizes a jpg and place in a desired destination
//keeps aspect ratio normal
//directory must have read/write priv
//requires GD1 or 2
/* RETURN KEY
1 = source file not found
2 = successfully completed
3 = errors occured
*/
function image_resize_jpeg( $img_source, $img_dest, $img_quality, $size_limit )
{
if ( !file_exists( $img_source ) )
return (1); //file not found return 1
//load the original image and put its hieght/width in $img_info
$img_info = getimagesize ( $img_source );
//$img_info ARRAY KEY
//1 = hieght
//2 = width
//3 = hight and width string
$orig_height = $img_info[1]; //source hieght from $img_info
$orig_width = $img_info[0]; //source width from $img_info
$jpegQuality = $img_quality; //quality of the JPG
if(($orig_width > $size_limit) || ($orig_height > $size_limit)) //make sure the image isnt
already resized
{
if ( $orig_height > $orig_width )
$scaledown = $orig_height;
else
$scaledown = $orig_width;
$newscale = $size_limit / $scaledown; //set the new scale size
//calculate the new aspect ratio
$new_w = abs($orig_width * $newscale);
$new_h = abs($orig_height * $newscale);
//create the blank limited-palette image
$base_image = imageCreate($new_w, $new_h);
//convert and save it to temp.jpg
imagejpeg($base_image, './temp.jpg');
//get the image pointer to the temp jpeg
$image = imageCreateFromJpeg('./temp.jpg');
$dir = dir("./$img_source"); //directory path of the orig image
// get the image pointer to the original image
$imageToResize = imageCreateFromJpeg("./$img_source");
//resize the original image over temp.jpg
// NOTE: of you have GD2, you could also use imageCopyResampled
imageCopyResized($image, $imageToResize, 0, 0, 0, 0, $new_w, $new_h, $orig_width, $orig_height);
//create the resized image
imageJpeg($image, "./$img_dest", $jpegQuality); //image destination
unlink('./temp.jpg'); //del the temp image file
return (2); //completed successfully
}
else
{
return(3); //errors occured
}
}