note 83292 added to function.imagecopyresampled
| From: | matturagmailcom at osu1 dot php dot net | Date: | Mon, 19 May 2008 17:53:17 +0000 |
| Subject: | note 83292 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139536@lists.php.net to get a copy of this message | ||
Here's a little function I wrote to resize images to a maximum dimension - based on what
facebook does in the galleries. You put in a source, destination and a maximum dimension in pixels
(eg 300), and for example if the image is long and thin, the longest edge will be 300px, yet the
image retains proportions. A square image will become 300x300, a 6x4 (landscape) will become
300x200, a 4x6 (portrait) - 200x300 etc.
It works on jpg images, but other formats can easily be added.
<?php
function createThumb($spath, $dpath, $maxd) {
$src=@imagecreatefromjpeg($spath);
if (!$src) {return false;} else {
$srcw=imagesx($src);
$srch=imagesy($src);
if ($srcw<$srch) {$height=$maxd;$width=floor($srcw*$height/$srch);}
else {$width=$maxd;$height=floor($srch*$width/$srcw);}
if ($width>$srcw && $height>$srch) {$width=$srcw;$height=$srch;} //if image is
actually smaller than you want, leave small (remove this line to resize anyway)
$thumb=imagecreatetruecolor($width, $height);
if ($height<100) {imagecopyresized($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src),
imagesy($src));}
else {imagecopyresampled($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src),
imagesy($src));}
imagejpeg($thumb, $dpath);
return true;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 79.71.146.208
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- https://master.php.net/note/edit/83292
Del: integrated -- https://master.php.net/note/delete/83292/integrated
Del: useless -- https://master.php.net/note/delete/83292/useless
Del: bad code -- https://master.php.net/note/delete/83292/bad+code
Del: spam -- https://master.php.net/note/delete/83292/spam
Del: non-english -- https://master.php.net/note/delete/83292/non-english
Del: in docs -- https://master.php.net/note/delete/83292/in+docs
Del: other reasons-- https://master.php.net/note/delete/83292
Reject -- https://master.php.net/note/reject/83292
Search -- https://master.php.net/manage/user-notes.php