Voting

: max(nine, two)?
(Example: nine)

The Note You're Voting On

mattura gmail com
17 years ago
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;
}
}
?>

<< Back to user notes page

To Top