From 392ddef2e8f3acbeb307ae3264313138aa51b6d9 Mon Sep 17 00:00:00 2001 From: Jeremy Shipman Date: Thu, 30 Oct 2014 12:30:51 +1300 Subject: [PATCH] FIX: Image resizing breaks when one of the resized image dimensions is between 0 and 1. Solution: Round up to 1 instead of down to 0. Converted php errors to exceptions in the process. --- filesystem/GD.php | 14 ++++++++------ filesystem/ImagickBackend.php | 16 +++++++++------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/filesystem/GD.php b/filesystem/GD.php index de657e0e6..a923d86f7 100644 --- a/filesystem/GD.php +++ b/filesystem/GD.php @@ -175,18 +175,20 @@ class GDBackend extends Object implements Image_Backend { */ public function resize($width, $height) { if(!$this->gd) return; + + if($width < 0 || $height < 0) throw new InvalidArgumentException("Image resizing dimensions cannot be negative"); + if(!$width && !$height) throw new InvalidArgumentException("No dimensions given when resizing image"); + if(!$width) throw new InvalidArgumentException("Width not given when resizing image"); + if(!$height) throw new InvalidArgumentException("Height not given when resizing image"); - $width = round($width); - $height = round($height); + //use whole numbers, ensuring that size is at least 1x1 + $width = max(1, round($width)); + $height = max(1, round($height)); // Check that a resize is actually necessary. if ($width == $this->width && $height == $this->height) { return $this; } - - if(!$width && !$height) user_error("No dimensions given", E_USER_ERROR); - if(!$width) user_error("Width not given", E_USER_ERROR); - if(!$height) user_error("Height not given", E_USER_ERROR); $newGD = imagecreatetruecolor($width, $height); diff --git a/filesystem/ImagickBackend.php b/filesystem/ImagickBackend.php index aa83dd520..74bb45cf1 100644 --- a/filesystem/ImagickBackend.php +++ b/filesystem/ImagickBackend.php @@ -104,9 +104,15 @@ class ImagickBackend extends Imagick implements Image_Backend { */ public function resize($width, $height) { if(!$this->valid()) return; - - $width = round($width); - $height = round($height); + + if($width < 0 || $height < 0) throw new InvalidArgumentException("Image resizing dimensions cannot be negative"); + if(!$width && !$height) throw new InvalidArgumentException("No dimensions given when resizing image"); + if(!$width) throw new InvalidArgumentException("Width not given when resizing image"); + if(!$height) throw new InvalidArgumentException("Height not given when resizing image"); + + //use whole numbers, ensuring that size is at least 1x1 + $width = max(1, round($width)); + $height = max(1, round($height)); $geometry = $this->getImageGeometry(); @@ -115,10 +121,6 @@ class ImagickBackend extends Imagick implements Image_Backend { return $this; } - if(!$width && !$height) user_error("No dimensions given", E_USER_ERROR); - if(!$width) user_error("Width not given", E_USER_ERROR); - if(!$height) user_error("Height not given", E_USER_ERROR); - $new = clone $this; $new->resizeImage($width, $height, self::FILTER_LANCZOS, 1);