From b5bd0d9ad18c4d5f026d7faf7565de138fa5e227 Mon Sep 17 00:00:00 2001 From: Gary Hole Date: Wed, 18 May 2016 14:10:39 +0100 Subject: [PATCH] Fix creating new image if file is unchanged. --- Adapter/AdapterInterface.php | 4 ++-- Image.php | 20 ++++++++++++++++++++ tests/ImageTests.php | 7 +++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Adapter/AdapterInterface.php b/Adapter/AdapterInterface.php index d9866fa..fa4312c 100644 --- a/Adapter/AdapterInterface.php +++ b/Adapter/AdapterInterface.php @@ -377,10 +377,10 @@ public function polygon(array $points, $color, $filled = false); /** * Flips the image. - * + * * @param int $flipVertical * @param int $flipHorizontal - * + * * @return $this */ public function flip($flipVertical, $flipHorizontal); diff --git a/Image.php b/Image.php index 7b99117..1970a30 100644 --- a/Image.php +++ b/Image.php @@ -617,6 +617,18 @@ public function init() $this->getAdapter()->init(); } + /** + * Determine if image is unchanged / no operations to be performed. + * + * @param int $quality + * + * @return bool + */ + protected function isUnchanged($quality) + { + return empty($this->operations) && $quality == 100; + } + /** * Save the file to a given output. */ @@ -647,6 +659,14 @@ public function save($file, $type = 'guess', $quality = 80) try { $this->init(); + + // If the source is a file and it's unchanged just copy it. + if ($this->source instanceof \Gregwar\Image\Source\File && $this->isUnchanged($quality)) { + copy($this->source->getFile(), $file); + + return $file; + } + $this->applyOperations(); $success = false; diff --git a/tests/ImageTests.php b/tests/ImageTests.php index f023d35..9b389db 100755 --- a/tests/ImageTests.php +++ b/tests/ImageTests.php @@ -141,6 +141,13 @@ public function testNoCache() $this->assertNotSame($monalisa, $image->guess()); } + public function testImageIsUnchangedWithNoOperations() + { + $imageFile = __DIR__.'/files/monalisa.jpg'; + $image = $this->open('monalisa.jpg'); + $this->assertSame(md5_file($imageFile), md5_file($image->guess(100))); + } + public function testActualCache() { $output = $this->open('monalisa.jpg')