note 69699 added to function.imagecopyresized
| From: | kyledotflorenceatgmaildotcom at osu1 dot php dot net | Date: | Sat, 16 Sep 2006 01:38:55 +0000 |
| Subject: | note 69699 added to function.imagecopyresized | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117237@lists.php.net to get a copy of this message | ||
The function below will resize an image based on max width and height, then it will create a
thumbnail image from the center of the resized image of a width and height specified. This function
will not resize the image to max_w pixels by max_h pixels, those are only the max width's and
heights the image can be, it resizes the image to the first 1:1 ratio below max_w and max_h.
For example, if you have an image that is 800x600 and you specify your new image to be 400x200, it
will resize based on the smallest number (in this case 200) and maintain the images 1:1 ratio. So
your final image would end up at something like 262x200.
UPDATE: I have updated this function, I added the 'newdir' option in case you want the
images saved in a different directory than the script. I also fixed the thumbnail slice so it is
perfectly in the center now and fixed the bug that 'ob at babcom dot biz' mentioned so you
can safely resize based on width or height now.
<?
/**********************************************************
* function resizejpeg:
*
* = creates a resized image based on the max width
* specified as well as generates a thumbnail from
* a rectangle cut from the middle of the image.
*
* @dir = directory image is stored in
* @newdir = directory new image will be stored in
* @img = the image name
* @max_w = the max width of the resized image
* @max_h = the max height of the resized image
* @th_w = the width of the thumbnail
* @th_h = the height of the thumbnail
*
**********************************************************/
function resizejpeg($dir, $newdir, $img, $max_w, $max_h, $th_w, $th_h)
{
// set destination directory
if (!$newdir) $newdir = $dir;
// get original images width and height
list($or_w, $or_h, $or_t) = getimagesize($dir.$img);
// make sure image is a jpeg
if ($or_t == 2) {
// obtain the image's ratio
$ratio = ($or_h / $or_w);
// original image
$or_image = imagecreatefromjpeg($dir.$img);
// resize image?
if ($or_w > $max_w || $or_h > $max_h) {
// resize by height, then width (height dominant)
if ($max_h < $max_w) {
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}
// resize by width, then height (width dominant)
else {
$rs_w = $max_w;
$rs_h = $ratio * $rs_w;
}
// copy old image to new image
$rs_image = imagecreatetruecolor($rs_w, $rs_h);
imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h);
}
// image requires no resizing
else {
$rs_w = $or_w;
$rs_h = $or_h;
$rs_image = $or_image;
}
// generate resized image
imagejpeg($rs_image, $newdir.$img, 100);
$th_image = imagecreatetruecolor($th_w, $th_h);
// cut out a rectangle from the resized image and store in thumbnail
$new_w = (($rs_w / 2) - ($th_w / 2));
$new_h = (($rs_h / 2) - ($th_h / 2));
imagecopyresized($th_image, $rs_image, 0, 0, $new_w, $new_h, $rs_w, $rs_h, $rs_w, $rs_h);
// generate thumbnail
imagejpeg($th_image, $newdir.'thumb_'.$img, 100);
return true;
}
// Image type was not jpeg!
else {
return false;
}
}
?>
Example:
<?php
$dir = './';
$img = 'test.jpg';
resizejpeg($dir, '', $img, 600, 400, 300, 150);
?>
The example would resize the image 'test.jpg' into something 600x400 or less (retains the
1:1 ratio of the image) and creates the file 'thumb_test.jpg' at 300x150.
----
Server IP: 217.160.72.57
Probable Submitter: 68.105.66.135
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php
Edit -- https://master.php.net/note/edit/69699
Del: integrated -- https://master.php.net/note/delete/69699/integrated
Del: useless -- https://master.php.net/note/delete/69699/useless
Del: bad code -- https://master.php.net/note/delete/69699/bad+code
Del: spam -- https://master.php.net/note/delete/69699/spam
Del: non-english -- https://master.php.net/note/delete/69699/non-english
Del: in docs -- https://master.php.net/note/delete/69699/in+docs
Del: other reasons-- https://master.php.net/note/delete/69699
Reject -- https://master.php.net/note/reject/69699
Search -- https://master.php.net/manage/user-notes.php