summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordeva <deva>2010-04-16 17:58:50 +0000
committerdeva <deva>2010-04-16 17:58:50 +0000
commit78e8418d39975e22043b3ffe1df04a439c9ee25c (patch)
tree3d9ceb010a1358b07c9fa518dfd36edf81cc2454
parent9555417b4166557a33acc5daf230cb7d33f049ac (diff)
New improved scaling (and now cutting) function.
-rw-r--r--utils/imagecache.php38
1 files changed, 26 insertions, 12 deletions
diff --git a/utils/imagecache.php b/utils/imagecache.php
index 347e946..3f91d79 100644
--- a/utils/imagecache.php
+++ b/utils/imagecache.php
@@ -15,19 +15,33 @@ class ImageSize {
}
};
-function rescale($image, $maxwidth, $maxheight)
+function rescale($image, $width, $height)
{
- $width = imagesx($image);
- $height = imagesy($image);
-
- if($width <= $maxwidth && $height <= $maxheight) return $image;
-
- $scale = 1;
- if($width > $maxwidth) $scale = $width / $maxwidth;
- if($height / $scale > $maxheight) $scale = $height / $maxheight;
-
- $image_p = imagecreatetruecolor($width / $scale, $height / $scale);
- imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width / $scale, $height / $scale, $width, $height);
+ $aspect = $width / $height;
+
+ $w = imagesx($image);
+ $h = imagesy($image);
+
+ $image_p = imagecreatetruecolor($width, $height);
+
+ $margin = 0;
+ $scale = 1.0;
+
+ if($w / $h > $aspect) {
+ // Cut right and left sides
+ $scale = $height / $h;
+ $margin = ($w * $scale - $width) / 2;
+ $margin /= $scale;
+ imagecopyresampled($image_p, $image, 0, 0, 0 + $margin, 0,
+ $width, $height, $w - $margin * 2, $h);
+ } else {
+ // Cut top and bottom
+ $scale = $width / $w;
+ $margin = ($h * $scale - $height) / 2;
+ $margin /= $scale;
+ imagecopyresampled($image_p, $image, 0, 0, 0, 0 + $margin / 2,
+ $width, $height, $w, $h - $margin * 2);
+ }
return $image_p;
}