Update of /cvsroot/php-blog/serendipity
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16211
Modified Files:
serendipity_functions_images.inc.php
Log Message:
Image scaling: Calculate height and pay attention to broadscale images.
Patch from Thomas Weinert, thanks!
Index: serendipity_functions_images.inc.php
===================================================================
RCS file: /cvsroot/php-blog/serendipity/serendipity_functions_images.inc.php,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- serendipity_functions_images.inc.php 17 Feb 2004 16:39:56 -0000 1.4
+++ serendipity_functions_images.inc.php 26 Feb 2004 09:54:56 -0000 1.5
@@ -301,12 +301,30 @@
}
$in = $loadfn($infilename);
- /* todo: Never - ever - enlarge an original image */
+ $width = imagesx($in);
+ $height = imagesy($in);
+
if (is_null($newheight)) {
- $newheight = (imagesy($in) / imagesx($in)) * $newwidth;
+ // calculate aspect ratio
+ $div_width = $width / $newwidth;
+ $div_height = $height / $newwidth;
+
+ if ($div_width <= 1 && $div_height <= 1) {
+ // do not scale small images
+ $newheight = $width;
+ $newwidth = $height;
+ } elseif ($div_width >= $div_height) {
+ // max width - calculate height, keep width as scaling base
+ $newheight = round($height / $div_width);
+ } else {
+ // max height - calculate width, keep height as scaling base
+ $newheight = $newwidth;
+ $newwidth = round($width / $div_height);
+ }
}
+
$out = imagecreatetruecolor($newwidth, $newheight);
- imagecopyresampled($out, $in, 0, 0, 0, 0, $newwidth, $newheight, imagesx($in), imagesy($in));
+ imagecopyresampled($out, $in, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$savefn($out, $outfilename);
$out = null;
$in = null;
|