From 950b816ca6a404b151f1b3e3ecdee690a732d80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9?= Date: Thu, 28 Nov 2013 13:57:19 +0600 Subject: [PATCH 1/3] function resize, crop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Доработана функция resize Оптимизирован код функции crop --- libraries/Gelato/Image/Image.php | 72 ++++++++++++++------------------ 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/libraries/Gelato/Image/Image.php b/libraries/Gelato/Image/Image.php index 07abc38..235155f 100644 --- a/libraries/Gelato/Image/Image.php +++ b/libraries/Gelato/Image/Image.php @@ -176,6 +176,7 @@ class Image $width = (int) $width; $height = ($height === null) ? null : (int) $height; $aspect_ratio = ($aspect_ratio === null) ? null : (int) $aspect_ratio; + $xpos = $ypos = 0; // Resizes the image to {$width}% of the original size if ($height === null) { @@ -186,13 +187,18 @@ class Image } else { // Resizes the image to the smalles possible dimension while maintaining aspect ratio - if ($aspect_ratio === Image::AUTO) { + if ($aspect_ratio === Image::AUTO || $aspect_ratio === null) { // Calculate smallest size based on given height and width while maintaining aspect ratio $percentage = min(($width / $this->width), ($height / $this->height)); $new_width = round($this->width * $percentage); $new_height = round($this->height * $percentage); + + if ($aspect_ratio === null) { + $xpos = (int)(($width - $new_width) / 2); + $ypos = (int)(($height - $new_height) / 2); + } // Resizes the image using the width to maintain aspect ratio } elseif ($aspect_ratio === Image::WIDTH) { @@ -216,31 +222,27 @@ class Image } } - // Create a new true color image width new width and height - $resized = imagecreatetruecolor($new_width, $new_height); - $transPng = imagecolorallocatealpha($resized, 0, 0, 0, 127); - imagefill($resized, 0, 0, $transPng); + $old_image = $this->image; + + if ($aspect_ratio === null) { + $this->image = imagecreatetruecolor($width, $height); + } else { + $this->image = imagecreatetruecolor($new_width, $new_height); + } + + if ($this->type === IMAGETYPE_PNG) { + $bgcolor = imagecolorallocatealpha($this->image, 0, 0, 0, 127); + } else { + $bgcolor = imagecolorallocate($this->image, 255, 255, 255); + } + + imagefill($this->image, 0, 0, $bgcolor); // Copy and resize part of an image with resampling - imagecopyresampled($resized, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->width, $this->height); + imagecopyresampled($this->image, $old_image, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->width, $this->height); // Destroy an image - imagedestroy($this->image); - - // Create a new true color image width new width and height - $this->image = imagecreatetruecolor($new_width, $new_height); - $transPng = imagecolorallocatealpha($this->image, 0, 0, 0, 127); - imagefill($this->image, 0, 0, $transPng); - - // Copy and resize part of an image with resampling - imagecopyresampled($this->image, $resized, 0, 0, 0, 0, $new_width, $new_height, $new_width, $new_height); - - // Destroy an image - imagedestroy($resized); - - // Save new width and height - $this->width = $new_width; - $this->height = $new_height; + imagedestroy($old_image); return $this; } @@ -271,31 +273,19 @@ class Image if ($y + $height > $this->height) $height = $this->height - $y; if ($width <= 0 || $height <= 0) return false; - // Create a new true color image - $crop = imagecreatetruecolor($width, $height); - $transPng = imagecolorallocatealpha($crop, 0, 0, 0, 127); - imagefill($crop, 0, 0, $transPng); - - // Copy and resize part of an image with resampling - imagecopyresampled($crop, $this->image, 0, 0, $x, $y, $this->width, $this->height, $this->width, $this->height); - - // Destroy an image - imagedestroy($this->image); - + $old_image = $this->image; + // Create a new true color image $this->image = imagecreatetruecolor($width, $height); - $transPng = imagecolorallocatealpha($this->image, 0, 0, 0, 127); - imagefill($this->image, 0, 0, $transPng); + + $transparent = imagecolorallocatealpha($this->image, 0, 0, 0, 127); + imagefill($this->image, 0, 0, $transparent); // Copy and resize part of an image with resampling - imagecopyresampled($this->image, $crop, 0, 0, 0, 0, $width, $height, $width, $height); + imagecopyresampled($this->image, $old_image, 0, 0, 0, 0, $width, $height, $width, $height); // Destroy an image - imagedestroy($crop); - - // Save new width and height - $this->width = $width; - $this->height = $height; + imagedestroy($old_image); return $this; } From b54cac24b105597bb51a17029e969619b5749a4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9?= Date: Thu, 28 Nov 2013 14:00:41 +0600 Subject: [PATCH 2/3] Update Image.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit функция resize(200, 200) растягивала изображение, теперь нет. http://f3.s.qip.ru/2Q3rL699.jpg - сверху до, снизу после --- libraries/Gelato/Image/Image.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Gelato/Image/Image.php b/libraries/Gelato/Image/Image.php index 235155f..33c36c5 100644 --- a/libraries/Gelato/Image/Image.php +++ b/libraries/Gelato/Image/Image.php @@ -265,8 +265,8 @@ class Image // Redefine vars $width = (int) $width; $height = (int) $height; - $x = (int) $x; - $y = (int) $y; + $x = (int) $x; + $y = (int) $y; // Calculate if ($x + $width > $this->width) $width = $this->width - $x; From 05a9ff440f7ba0e555fb9f99762f8abfe4da3e97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9?= Date: Thu, 28 Nov 2013 14:02:22 +0600 Subject: [PATCH 3/3] Update Image.php --- libraries/Gelato/Image/Image.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Gelato/Image/Image.php b/libraries/Gelato/Image/Image.php index 33c36c5..c42dd64 100644 --- a/libraries/Gelato/Image/Image.php +++ b/libraries/Gelato/Image/Image.php @@ -173,8 +173,8 @@ class Image public function resize($width, $height = null, $aspect_ratio = null) { // Redefine vars - $width = (int) $width; - $height = ($height === null) ? null : (int) $height; + $width = (int) $width; + $height = ($height === null) ? null : (int) $height; $aspect_ratio = ($aspect_ratio === null) ? null : (int) $aspect_ratio; $xpos = $ypos = 0;