summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2012-09-08 23:26:19 +0200
committerRobin Appelman <icewind@owncloud.com>2012-09-08 23:26:19 +0200
commit697061fa9f6279b8d8f24d984543aeccb6214ba2 (patch)
tree2db02b0749fc03c3595d854995b760e4ed0d88e0
parentabc930c57c02f605229d6caa8f5aedef39d7ee76 (diff)
downloadnextcloud-server-697061fa9f6279b8d8f24d984543aeccb6214ba2.tar.gz
nextcloud-server-697061fa9f6279b8d8f24d984543aeccb6214ba2.zip
add OC_Image::fitIn
-rw-r--r--lib/image.php38
1 files changed, 23 insertions, 15 deletions
diff --git a/lib/image.php b/lib/image.php
index f4b3c2cc071..861353e039d 100644
--- a/lib/image.php
+++ b/lib/image.php
@@ -543,21 +543,7 @@ class OC_Image {
$new_height = $maxsize;
}
- $process = imagecreatetruecolor(round($new_width), round($new_height));
- if ($process == false) {
- OC_Log::write('core',__METHOD__.'(): Error creating true color image',OC_Log::ERROR);
- imagedestroy($process);
- return false;
- }
-
- imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
- if ($process == false) {
- OC_Log::write('core',__METHOD__.'(): Error resampling process image '.$new_width.'x'.$new_height,OC_Log::ERROR);
- imagedestroy($process);
- return false;
- }
- imagedestroy($this->resource);
- $this->resource = $process;
+ $this->preciseResize(round($new_width), round($new_height));
return true;
}
@@ -666,6 +652,28 @@ class OC_Image {
return true;
}
+ /**
+ * @brief Resizes the image to fit within a boundry while preserving ratio.
+ * @param $maxWidth
+ * @param $maxHeight
+ * @returns bool
+ */
+ public function fitIn($maxWidth, $maxHeight) {
+ if(!$this->valid()) {
+ OC_Log::write('core',__METHOD__.'(): No image loaded', OC_Log::ERROR);
+ return false;
+ }
+ $width_orig=imageSX($this->resource);
+ $height_orig=imageSY($this->resource);
+ $ratio = $width_orig/$height_orig;
+
+ $newWidth = min($maxWidth, $ratio*$maxHeight);
+ $newHeight = min($maxHeight, $maxWidth/$ratio);
+
+ $this->preciseResize(round($newWidth), round($newHeight));
+ return true;
+ }
+
public function destroy() {
if($this->valid()) {
imagedestroy($this->resource);