Skip to content

Commit

Permalink
Make resizer path relative to cache dir (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp authored and ausi committed Mar 22, 2017
1 parent 0df2971 commit 47e1646
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Resizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Imagine\Exception\RuntimeException as ImagineRuntimeException;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;

/**
* Resizer class.
Expand Down Expand Up @@ -165,7 +166,7 @@ protected function createImage(ImageInterface $image, $path)
* @param ResizeCoordinatesInterface $coordinates
* @param ResizeOptionsInterface $options
*
* @return string The realtive target path
* @return string The relative target path
*/
private function createCachePath($path, ResizeCoordinatesInterface $coordinates, ResizeOptionsInterface $options)
{
Expand All @@ -174,7 +175,11 @@ private function createCachePath($path, ResizeCoordinatesInterface $coordinates,
ksort($imagineOptions);

$hash = substr(md5(implode('|', array_merge(
[$path, filemtime($path), $coordinates->getHash()],
[
Path::makeRelative($path, $this->cacheDir),
filemtime($path),
$coordinates->getHash(),
],
array_keys($imagineOptions),
array_values($imagineOptions)
))), 0, 9);
Expand Down
38 changes: 38 additions & 0 deletions tests/ResizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,44 @@ public function testResizeCache()
$this->assertNotEquals($imagePath, $resizedImage->getPath());
$this->assertEquals(100, getimagesize($resizedImage->getPath())[0], 'New cache file should have been created');

// With different paths, but same relative path
$subDir = $this->rootDir.'/sub/dir';

mkdir($subDir, 0777, true);
copy($this->rootDir.'/dummy.jpg', $subDir.'/dummy.jpg');
touch($subDir.'/dummy.jpg', filemtime($this->rootDir.'/dummy.jpg'));

$subResizer = $this->createResizer($subDir, $calculator);

$subImage = $this
->getMockBuilder('Contao\Image\Image')
->disableOriginalConstructor()
->getMock()
;

$subImage
->method('getDimensions')
->willReturn(new ImageDimensions(new Box(200, 200)))
;

$subImage
->method('getPath')
->willReturn($subDir.'/dummy.jpg')
;

$subImage
->method('getImagine')
->willReturn(new GdImagine())
;

$resizedImage = $subResizer->resize($subImage, $configuration, (new ResizeOptions())->setBypassCache(true));

$this->assertEquals(
substr($imagePath, strlen($this->rootDir)),
substr($resizedImage->getPath(), strlen($subDir)),
'The hash should be the same if the image path relative to the cacheDir is the same'
);

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

Expand Down

0 comments on commit 47e1646

Please sign in to comment.