Skip to content

Commit

Permalink
Use imagine options in cache path hash, closes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Jan 23, 2017
1 parent 4068dda commit c09bfbf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Resizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private function processResize(ImageInterface $image, ResizeConfigurationInterfa
return $this->createImage($image, $image->getPath());
}

$cachePath = $this->cacheDir.'/'.$this->createCachePath($image->getPath(), $coordinates);
$cachePath = $this->cacheDir.'/'.$this->createCachePath($image->getPath(), $coordinates, $options);

if ($this->filesystem->exists($cachePath) && !$options->getBypassCache()) {
return $this->createImage($image, $cachePath);
Expand Down Expand Up @@ -163,13 +163,21 @@ protected function createImage(ImageInterface $image, $path)
*
* @param string $path
* @param ResizeCoordinatesInterface $coordinates
* @param ResizeOptionsInterface $options
*
* @return string The realtive target path
*/
private function createCachePath($path, ResizeCoordinatesInterface $coordinates)
private function createCachePath($path, ResizeCoordinatesInterface $coordinates, ResizeOptionsInterface $options)
{
$pathinfo = pathinfo($path);
$hash = substr(md5(implode('|', [$path, filemtime($path), $coordinates->getHash()])), 0, 9);
$imagineOptions = $options->getImagineOptions();
ksort($imagineOptions);

$hash = substr(md5(implode('|', array_merge(
[$path, filemtime($path), $coordinates->getHash()],
array_keys($imagineOptions),
array_values($imagineOptions)
))), 0, 9);

return substr($hash, 0, 1).'/'.$pathinfo['filename'].'-'.substr($hash, 1).'.'.$pathinfo['extension'];
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ResizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ public function testResizeCache()
$this->assertEquals($targetPath, $resizedImage->getPath());
$this->assertFileEquals($imagePath, $targetPath, 'Cache file should have been copied');

// With different imagine options
$resizedImage = $resizer->resize($image, $configuration, (new ResizeOptions())->setImagineOptions(['jpeg_quality' => 10]));

$this->assertNotEquals($imagePath, $resizedImage->getPath());
$this->assertEquals(100, getimagesize($resizedImage->getPath())[0], 'New cache file should have been created');

// Without cache
$resizedImage = $resizer->resize($image, $configuration, (new ResizeOptions())->setBypassCache(true));

Expand Down

0 comments on commit c09bfbf

Please sign in to comment.