Added original file data

This commit is contained in:
franck
2013-08-15 12:27:30 +02:00
parent 56034e1184
commit 5762711146
10 changed files with 57 additions and 28 deletions

View File

@@ -81,30 +81,33 @@ class Image extends BaseAction implements EventSubscriberInterface
// Find cached file path // Find cached file path
$cacheFilePath = $this->getCacheFilePath($subdir, $source_file, $event); $cacheFilePath = $this->getCacheFilePath($subdir, $source_file, $event);
$originalImagePathInCache = $this->getCacheFilePath($subdir, $source_file, $event, true);
if (! file_exists($cacheFilePath)) { if (! file_exists($cacheFilePath)) {
// If the original image is required, either create a copy of make a symbolic link, // Create a chached version of the original image in the web space, if not exists
// depending of the current configuration
if ($event->isOriginalImage()) {
if (! file_exists($originalImagePathInCache)) {
$mode = ConfigQuery::read('original_image_delivery_mode', 'symlink'); $mode = ConfigQuery::read('original_image_delivery_mode', 'symlink');
if ($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); $error_message = sprintf("Failed to create symbolic link for %s in %s image cache directory", basename($source_file), $subdir);
Tlog::getInstance()->addError($error_message); Tlog::getInstance()->addError($error_message);
throw new ImageException($error_message); throw new ImageException($error_message);
} }
} }
else {// mode = 'copy' 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); $error_message = sprintf("Failed to copy %s in %s image cache directory", basename($source_file), $subdir);
Tlog::getInstance()->addError($error_message); Tlog::getInstance()->addError($error_message);
throw new ImageException($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. // We have to process the image.
$imagine = $this->createImagineInstance(); $imagine = $this->createImagineInstance();
@@ -195,12 +198,17 @@ class Image extends BaseAction implements EventSubscriberInterface
} }
// Compute the image URL // 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 // Update the event with file path and file URL
$event->setCacheFilepath($cacheFilePath); $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 $subdir the subdirectory related to cache base
* @param string $filename the filename * @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 * @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); $path = $this->getCachePath($subdir);
$safe_filename = preg_replace("[^:alnum:\-\._]", "-", strtolower(basename($filename))); $safe_filename = preg_replace("[^:alnum:\-\._]", "-", strtolower(basename($filename)));
// Keep original safe name if no tranformations are applied // Keep original safe name if no tranformations are applied
if ($event->isOriginalImage()) if ($forceOriginalImage || $event->isOriginalImage())
return sprintf("%s/%s", $path, $safe_filename); return sprintf("%s/%s", $path, $safe_filename);
else else
return sprintf("%s/%s-%s", $path, $event->getSignature(), $safe_filename); return sprintf("%s/%s-%s", $path, $event->getSignature(), $safe_filename);

View File

@@ -46,6 +46,16 @@ class ImageEvent extends ActionEvent
*/ */
protected $cache_filepath = null; 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) * @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 * @return boolean true if the required image is the original image
*/ */
public function isOriginalImage() { public function isOriginalImage()
return {
empty($this->width) return empty($this->width) && empty($this->height) && empty($this->resize_mode) && empty($this->background_color)
&& empty($this->height) && empty($this->effects) && empty($this->rotation);
&& empty($this->resize_mode)
&& empty($this->background_color)
&& empty($this->effects)
&& empty($this->rotation)
;
} }
/** /**
* @return string a hash identifiying the processing options * @return string a hash identifiying the processing options
*/ */
public function getSignature() { public function getSignature()
{
return md5( return md5(
$this->width $this->width . $this->height . $this->resize_mode . $this->background_color . implode(',', $this->effects)
. $this->height . $this->rotation);
. $this->resize_mode
. $this->background_color
. implode(',', $this->effects)
. $this->rotation
);
} }
public function getCategory() public function getCategory()
@@ -234,4 +235,23 @@ class ImageEvent extends ActionEvent
$this->quality = $quality; $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;
}
} }

View File

@@ -83,7 +83,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase
unlink(sprintf("%s/%s", $dir, $file)); unlink(sprintf("%s/%s", $dir, $file));
} }
closedir($dir); closedir($dh);
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB