diff --git a/src/Resizer.php b/src/Resizer.php index 4414dc4..9a17315 100644 --- a/src/Resizer.php +++ b/src/Resizer.php @@ -12,6 +12,7 @@ use Imagine\Exception\RuntimeException as ImagineRuntimeException; use Symfony\Component\Filesystem\Filesystem; +use Webmozart\PathUtil\Path; /** * Resizer class. @@ -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) { @@ -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); diff --git a/tests/ResizerTest.php b/tests/ResizerTest.php index 888dfe9..adad8b1 100644 --- a/tests/ResizerTest.php +++ b/tests/ResizerTest.php @@ -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));