From 697061fa9f6279b8d8f24d984543aeccb6214ba2 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 8 Sep 2012 23:26:19 +0200 Subject: [PATCH] add OC_Image::fitIn --- lib/image.php | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/image.php b/lib/image.php index f4b3c2cc07..861353e039 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);