note 33630 deleted from function.imagecopyresized by aidan
| From: | aidan@php.net | Date: | Mon, 04 Oct 2004 09:39:20 +0000 |
| Subject: | note 33630 deleted from function.imagecopyresized by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77913@lists.php.net to get a copy of this message | ||
Note Submitter: viral at noeticsolutions dot com
----
First of all, I would like to thank people above for the sample code to do thumbnail(ing). I am
using Linux 4.2.2 and GD 1.6.2. I was have a wierd problem with the resizing. In most cases a
black line (of 1 pixel) used to be added either towards the right, bottom or at both places.
To solve this I modified one of the sample code above to chop off 1 pixel from both the width and
the height so that I do not get the black line.
The revised code is as under:
function image_resize_jpeg( $img_source, $img_dest, $img_quality, $size_limit )
{
if ( !file_exists( $img_source ) )
{
return (1); //file not found return 1
}
// increase the $size_limit variable by 1 as it is going to be chopped by 1 below
$size_limit++;
//load the original image and put its hieght/width in $img_info
$img_info = getimagesize ( $img_source );
//$img_info ARRAY KEY
//0 = width
//1 = hieght
//2 = image type
//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 = (int)abs($orig_width * $newscale);
$new_h = (int)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');
imagedestroy($base_image);
//get the image pointer to the temp jpeg
$image = imageCreateFromJpeg('./temp.jpg');
// 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);
$new_image = imageCreate($new_w - 1, $new_h - 1);
imagecopy ($new_image, $image, 0, 0, 0, 0, $new_w - 1, $new_h - 1);
//create the resized image
imageJpeg($new_image, "./$img_dest", $jpegQuality); //image destination
imagedestroy($image);
imagedestroy($new_image);
imagedestroy($imageToResize);
unlink('./temp.jpg'); //del the temp image file
return (2); //completed successfully
}
else
{
return(3); //errors occured
}
}