Skip to content

Commit b5bd0d9

Browse files
Gary HoleGary Hole
authored andcommitted
Fix creating new image if file is unchanged.
1 parent 3dc2675 commit b5bd0d9

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Adapter/AdapterInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,10 @@ public function polygon(array $points, $color, $filled = false);
377377

378378
/**
379379
* Flips the image.
380-
*
380+
*
381381
* @param int $flipVertical
382382
* @param int $flipHorizontal
383-
*
383+
*
384384
* @return $this
385385
*/
386386
public function flip($flipVertical, $flipHorizontal);

Image.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,18 @@ public function init()
617617
$this->getAdapter()->init();
618618
}
619619

620+
/**
621+
* Determine if image is unchanged / no operations to be performed.
622+
*
623+
* @param int $quality
624+
*
625+
* @return bool
626+
*/
627+
protected function isUnchanged($quality)
628+
{
629+
return empty($this->operations) && $quality == 100;
630+
}
631+
620632
/**
621633
* Save the file to a given output.
622634
*/
@@ -647,6 +659,14 @@ public function save($file, $type = 'guess', $quality = 80)
647659

648660
try {
649661
$this->init();
662+
663+
// If the source is a file and it's unchanged just copy it.
664+
if ($this->source instanceof \Gregwar\Image\Source\File && $this->isUnchanged($quality)) {
665+
copy($this->source->getFile(), $file);
666+
667+
return $file;
668+
}
669+
650670
$this->applyOperations();
651671

652672
$success = false;

tests/ImageTests.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ public function testNoCache()
141141
$this->assertNotSame($monalisa, $image->guess());
142142
}
143143

144+
public function testImageIsUnchangedWithNoOperations()
145+
{
146+
$imageFile = __DIR__.'/files/monalisa.jpg';
147+
$image = $this->open('monalisa.jpg');
148+
$this->assertSame(md5_file($imageFile), md5_file($image->guess(100)));
149+
}
150+
144151
public function testActualCache()
145152
{
146153
$output = $this->open('monalisa.jpg')

0 commit comments

Comments
 (0)