From 1ea0155ad768dc7dd58e75a46d44865c6db03989 Mon Sep 17 00:00:00 2001 From: patdenice Date: Fri, 16 Dec 2011 17:00:06 +0000 Subject: bug:2535 Image is not cropped and ressized if requested size is bigger than image size. git-svn-id: http://piwigo.org/svn/trunk@12749 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/include/image.class.php | 79 +++++++++++++++++++++++++------------------ include/ws_functions.inc.php | 9 +++-- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/admin/include/image.class.php b/admin/include/image.class.php index 5586c021c..b6c33e269 100644 --- a/admin/include/image.class.php +++ b/admin/include/image.class.php @@ -97,42 +97,12 @@ class pwg_image $source_width = $this->image->get_width(); $source_height = $this->image->get_height(); - // Crop image - if ($crop) - { - $x = 0; - $y = 0; - - if ($source_width < $source_height and $follow_orientation) - { - list($max_width, $max_height) = array($max_height, $max_width); - } - - $img_ratio = $source_width / $source_height; - $dest_ratio = $max_width / $max_height; - - if($dest_ratio > $img_ratio) - { - $destHeight = round($source_width * $max_height / $max_width); - $y = round(($source_height - $destHeight) / 2 ); - $source_height = $destHeight; - } - elseif ($dest_ratio < $img_ratio) - { - $destWidth = round($source_height * $max_width / $max_height); - $x = round(($source_width - $destWidth) / 2 ); - $source_width = $destWidth; - } - - $this->image->crop($source_width, $source_height, $x, $y); - } - $rotation = null; if ($automatic_rotation) { $rotation = self::get_rotation_angle($this->source_filepath); } - $resize_dimensions = self::get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); + $resize_dimensions = self::get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation, $crop, $follow_orientation); // testing on height is useless in theory: if width is unchanged, there // should be no resize, because width/height ratio is not modified. @@ -150,6 +120,11 @@ class pwg_image // we save a few kilobytes. For example a thumbnail with metadata weights 25KB, without metadata 7KB. $this->image->strip(); } + + if (isset($resize_dimensions['crop'])) + { + $this->image->crop($resize_dimensions['crop']['width'], $resize_dimensions['crop']['height'], $resize_dimensions['crop']['x'], $resize_dimensions['crop']['y']); + } $this->image->resize($resize_dimensions['width'], $resize_dimensions['height']); @@ -164,7 +139,7 @@ class pwg_image return $this->get_resize_result($destination_filepath, $resize_dimensions['width'], $resize_dimensions['height'], $starttime); } - static function get_resize_dimensions($width, $height, $max_width, $max_height, $rotation=null) + static function get_resize_dimensions($width, $height, $max_width, $max_height, $rotation=null, $crop=false, $follow_orientation=true) { $rotate_for_dimensions = false; if (isset($rotation) and in_array(abs($rotation), array(90, 270))) @@ -176,6 +151,33 @@ class pwg_image { list($width, $height) = array($height, $width); } + + if ($crop) + { + $x = 0; + $y = 0; + + if ($width < $height and $follow_orientation) + { + list($max_width, $max_height) = array($max_height, $max_width); + } + + $img_ratio = $width / $height; + $dest_ratio = $max_width / $max_height; + + if($dest_ratio > $img_ratio) + { + $destHeight = round($width * $max_height / $max_width); + $y = round(($height - $destHeight) / 2 ); + $height = $destHeight; + } + elseif ($dest_ratio < $img_ratio) + { + $destWidth = round($height * $max_width / $max_height); + $x = round(($width - $destWidth) / 2 ); + $width = $destWidth; + } + } $ratio_width = $width / $max_width; $ratio_height = $height / $max_height; @@ -202,10 +204,21 @@ class pwg_image list($destination_width, $destination_height) = array($destination_height, $destination_width); } - return array( + $result = array( 'width' => $destination_width, 'height'=> $destination_height, ); + + if ($crop and ($x or $y)) + { + $result['crop'] = array( + 'width' => $width, + 'height' => $height, + 'x' => $x, + 'y' => $y, + ); + } + return $result; } static function get_rotation_angle($source_filepath) diff --git a/include/ws_functions.inc.php b/include/ws_functions.inc.php index 6c3838aa1..6adbfa065 100644 --- a/include/ws_functions.inc.php +++ b/include/ws_functions.inc.php @@ -3232,6 +3232,11 @@ SELECT id, path, tn_ext, has_high prepare_directory(dirname($thumb_path)); $img = new pwg_image($image_path, $params['library']); + if (!is_bool($params['crop'])) + $params['crop'] = get_boolean($params['crop']); + if (!is_bool($params['follow_orientation'])) + $params['follow_orientation'] = get_boolean($params['follow_orientation']); + $result = $img->pwg_resize( $thumb_path, $params['maxwidth'], @@ -3239,8 +3244,8 @@ SELECT id, path, tn_ext, has_high $params['quality'], false, // automatic rotation is not needed for thumbnails. true, // strip metadata - get_boolean($params['crop']), - get_boolean($params['follow_orientation']) + $params['crop'], + $params['follow_orientation'] ); $img->destroy(); -- cgit v1.2.3