diff --git a/src/Intervention/Image/Gd/Commands/BackupCommand.php b/src/Intervention/Image/Gd/Commands/BackupCommand.php index 0b1e6713..8d21ee39 100644 --- a/src/Intervention/Image/Gd/Commands/BackupCommand.php +++ b/src/Intervention/Image/Gd/Commands/BackupCommand.php @@ -21,7 +21,7 @@ class BackupCommand extends \Intervention\Image\Commands\AbstractCommand imagesavealpha($clone, true); imagecopy($clone, $image->getCore(), 0, 0, 0, 0, $size->width, $size->height); - $image->setBackup($backupName, $clone); + $image->setBackup($clone, $backupName); return true; } diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 33e97c2e..0f5cb40a 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -19,9 +19,9 @@ class Image extends File protected $core; /** - * Image resource backup of current image processor + * Array of Image resource backups of current image processor * - * @var mixed + * @var array */ protected $backups = array(); @@ -162,9 +162,15 @@ class Image extends File * @param string $name * @return mixed */ - public function getBackup($name = 'default') + public function getBackup($name = null) { - $name = (is_null($name) ? 'default' : $name); + $name = is_null($name) ? 'default' : $name; + + if ( ! $this->backupExists($name)) { + throw new \Intervention\Image\Exception\RuntimeException( + "Backup with name ({$name}) not available. Call backup() before reset()." + ); + } return $this->backups[$name]; } @@ -172,27 +178,30 @@ class Image extends File /** * Sets current image backup * + * @param mixed $resource * @param string $name - * @param mixed $value * @return self */ - public function setBackup($name, $value = null) + public function setBackup($resource, $name = null) { - // If there is just 1 argument specified then it's the value - if (func_num_args() == 1) { - $value = func_get_arg(0); - $name = 'default'; - } - elseif (is_null($name)) - { - $name = 'default'; - } + $name = is_null($name) ? 'default' : $name; - $this->backups[$name] = $value; + $this->backups[$name] = $resource; return $this; } + /** + * Checks if named backup exists + * + * @param string $name + * @return bool + */ + private function backupExists($name) + { + return array_key_exists($name, $this->backups); + } + /** * Checks if current image is already encoded * diff --git a/src/Intervention/Image/Imagick/Commands/BackupCommand.php b/src/Intervention/Image/Imagick/Commands/BackupCommand.php index 36584367..90f2d721 100644 --- a/src/Intervention/Image/Imagick/Commands/BackupCommand.php +++ b/src/Intervention/Image/Imagick/Commands/BackupCommand.php @@ -15,7 +15,7 @@ class BackupCommand extends \Intervention\Image\Commands\AbstractCommand $backupName = $this->argument(0)->value(); // clone current image resource - $image->setBackup($backupName, clone $image->getCore()); + $image->setBackup(clone $image->getCore(), $backupName); return true; }