diff --git a/core/lib/Thelia/Action/Image.php b/core/lib/Thelia/Action/Image.php index 86003064b..353f9d9c3 100644 --- a/core/lib/Thelia/Action/Image.php +++ b/core/lib/Thelia/Action/Image.php @@ -81,30 +81,33 @@ class Image extends BaseAction implements EventSubscriberInterface // Find cached file path $cacheFilePath = $this->getCacheFilePath($subdir, $source_file, $event); + $originalImagePathInCache = $this->getCacheFilePath($subdir, $source_file, $event, true); + if (! file_exists($cacheFilePath)) { - // If the original image is required, either create a copy of make a symbolic link, - // depending of the current configuration - if ($event->isOriginalImage()) { + // Create a chached version of the original image in the web space, if not exists + if (! file_exists($originalImagePathInCache)) { $mode = ConfigQuery::read('original_image_delivery_mode', 'symlink'); if ($mode == 'symlink') { - if (false == @symlink($source_file, $cacheFilePath)) { + if (false == @symlink($source_file, $originalImagePathInCache)) { $error_message = sprintf("Failed to create symbolic link for %s in %s image cache directory", basename($source_file), $subdir); Tlog::getInstance()->addError($error_message); throw new ImageException($error_message); } } else {// mode = 'copy' - if (false == @copy($source_file, $cacheFilePath)) { + if (false == @copy($source_file, $originalImagePathInCache)) { $error_message = sprintf("Failed to copy %s in %s image cache directory", basename($source_file), $subdir); Tlog::getInstance()->addError($error_message); throw new ImageException($error_message); } } } - else { + + // Process image only if we have some transformations to do. + if (! $event->isOriginalImage()) { // We have to process the image. $imagine = $this->createImagineInstance(); @@ -195,12 +198,17 @@ class Image extends BaseAction implements EventSubscriberInterface } // Compute the image URL - $image_url = $this->getCacheFileURL($subdir, basename($cacheFilePath)); + $processed_image_url = $this->getCacheFileURL($subdir, basename($cacheFilePath)); + + // compute the full resulution image path in cache + $original_image_url = $this->getCacheFileURL($subdir, basename($originalImagePathInCache)); // Update the event with file path and file URL $event->setCacheFilepath($cacheFilePath); + $event->setCacheOriginalFilepath($originalImagePathInCache); - $event->setFileUrl(URL::absoluteUrl($image_url)); + $event->setFileUrl(URL::absoluteUrl($processed_image_url)); + $event->setOriginalFileUrl(URL::absoluteUrl($original_image_url)); } /** @@ -320,16 +328,17 @@ class Image extends BaseAction implements EventSubscriberInterface * * @param string $subdir the subdirectory related to cache base * @param string $filename the filename + * @param boolean $forceOriginalImage if true, the origiunal image path in the cache dir is returned. * @return string the cache directory path relative to Web Root */ - protected function getCacheFilePath($subdir, $filename, ImageEvent $event) + protected function getCacheFilePath($subdir, $filename, ImageEvent $event, $forceOriginalImage = false) { $path = $this->getCachePath($subdir); $safe_filename = preg_replace("[^:alnum:\-\._]", "-", strtolower(basename($filename))); // Keep original safe name if no tranformations are applied - if ($event->isOriginalImage()) + if ($forceOriginalImage || $event->isOriginalImage()) return sprintf("%s/%s", $path, $safe_filename); else return sprintf("%s/%s-%s", $path, $event->getSignature(), $safe_filename); diff --git a/core/lib/Thelia/Core/Event/ImageEvent.php b/core/lib/Thelia/Core/Event/ImageEvent.php index bfca35fb9..f6421eb9f 100644 --- a/core/lib/Thelia/Core/Event/ImageEvent.php +++ b/core/lib/Thelia/Core/Event/ImageEvent.php @@ -46,6 +46,16 @@ class ImageEvent extends ActionEvent */ protected $cache_filepath = null; + /** + * @var string The absolute URL of the cached version of the original image (in the web space) + */ + protected $original_file_url = null; + + /** + * @var string The absolute path of the cached version of the original image file + */ + protected $cache_original_filepath = null; + /** * @var string The image category (i.e. the subdirectory in image cache) */ @@ -89,29 +99,20 @@ class ImageEvent extends ActionEvent /** * @return boolean true if the required image is the original image */ - public function isOriginalImage() { - return - empty($this->width) - && empty($this->height) - && empty($this->resize_mode) - && empty($this->background_color) - && empty($this->effects) - && empty($this->rotation) - ; + public function isOriginalImage() + { + return empty($this->width) && empty($this->height) && empty($this->resize_mode) && empty($this->background_color) + && empty($this->effects) && empty($this->rotation); } /** * @return string a hash identifiying the processing options */ - public function getSignature() { + public function getSignature() + { return md5( - $this->width - . $this->height - . $this->resize_mode - . $this->background_color - . implode(',', $this->effects) - . $this->rotation - ); + $this->width . $this->height . $this->resize_mode . $this->background_color . implode(',', $this->effects) + . $this->rotation); } public function getCategory() @@ -234,4 +235,23 @@ class ImageEvent extends ActionEvent $this->quality = $quality; } + public function getOriginalFileUrl() + { + return $this->original_file_url; + } + + public function setOriginalFileUrl($original_file_url) + { + $this->original_file_url = $original_file_url; + } + + public function getCacheOriginalFilepath() + { + return $this->cache_original_filepath; + } + + public function setCacheOriginalFilepath($cache_original_filepath) + { + $this->cache_original_filepath = $cache_original_filepath; + } } diff --git a/core/lib/Thelia/Tests/Action/ImageTest.php b/core/lib/Thelia/Tests/Action/ImageTest.php index 2b7510449..fa25672d7 100644 --- a/core/lib/Thelia/Tests/Action/ImageTest.php +++ b/core/lib/Thelia/Tests/Action/ImageTest.php @@ -83,7 +83,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase unlink(sprintf("%s/%s", $dir, $file)); } - closedir($dir); + closedir($dh); } } diff --git a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-2.png b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-2.png index 84a6899a6..f6b7f9a53 100644 Binary files a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-2.png and b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-2.png differ diff --git a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-3.png b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-3.png index 6ca361372..51d05ea15 100644 Binary files a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-3.png and b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-3.png differ diff --git a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-4.png b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-4.png index 6ca361372..c6d5d3834 100644 Binary files a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-4.png and b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-4.png differ diff --git a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-5.png b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-5.png index 23c14c7d0..a933abd26 100644 Binary files a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-5.png and b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-5.png differ diff --git a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-6.png b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-6.png index 6ca361372..e7d6b950b 100644 Binary files a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-6.png and b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-6.png differ diff --git a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-7.png b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-7.png index 23c14c7d0..f46514478 100644 Binary files a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-7.png and b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-7.png differ diff --git a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-8.png b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-8.png index 6ca361372..8631b4e78 100644 Binary files a/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-8.png and b/core/lib/Thelia/Tests/Action/assets/images/sources/test-image-8.png differ