Skip to content

Commit

Permalink
Fix creating new image if file is unchanged.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Hole authored and Gary Hole committed May 18, 2016
1 parent 3dc2675 commit b5bd0d9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/ImageTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit b5bd0d9

Please sign in to comment.