Skip to content
Snippets Groups Projects
Commit 3b643e98 authored by Steven Jones's avatar Steven Jones Committed by Steven Jones
Browse files

Issue #2891103 by Steven Jones, bucefal91, jcisio, mbrc, kala4ek: Image style...

Issue #2891103 by Steven Jones, bucefal91, jcisio, mbrc, kala4ek: Image style generation failes when filename has special characters
parent 96069bcd
No related branches found
No related tags found
No related merge requests found
...@@ -75,6 +75,11 @@ class ImageAPIOptimizePipeline extends ConfigEntityBase implements ImageAPIOptim ...@@ -75,6 +75,11 @@ class ImageAPIOptimizePipeline extends ConfigEntityBase implements ImageAPIOptim
*/ */
protected $processorsCollection; protected $processorsCollection;
/**
* An array of temporary files that can be deleted on destruction.
*/
protected $temporaryFiles = [];
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -166,13 +171,40 @@ class ImageAPIOptimizePipeline extends ConfigEntityBase implements ImageAPIOptim ...@@ -166,13 +171,40 @@ class ImageAPIOptimizePipeline extends ConfigEntityBase implements ImageAPIOptim
return FALSE; return FALSE;
} }
foreach ($this->getProcessors() as $processor) { if (count($this->getProcessors())) {
$processor->applyToImage($image_uri); /*
// The file may have changed on disk after each processor has been Copy image to optimize to a temp location so that:
// applied, and PHP has a cache of file size information etc. so clear 1. It's always a local image.
// it here so that later calls to filesize() etc. get the correct 2. The filename is only ascii characters.
// information. 3. Multiple pipelines can be applied to the same image at the same time.
clearstatcache(); */
$file_extension = substr(strrchr($image_uri,'.'),1);
$temp_image_uri_base = \Drupal::service('file_system')->tempnam('temporary://', 'image_api_optimize_' . $this->id());
// Ensure this file is cleaned up at the end of the request.
$this->temporaryFiles[] = $temp_image_uri_base;
// Copy the file we are trying to optimize to the temporary location.
file_unmanaged_copy($image_uri, $temp_image_uri_base, FILE_EXISTS_REPLACE);
// Some processors require the correct file extension, add that on.
$temp_image_uri = $temp_image_uri_base . '.' .$file_extension;
file_unmanaged_move($temp_image_uri_base, $temp_image_uri);
// Ensure this file is cleaned up at the end of the request.
$this->temporaryFiles[] = $temp_image_uri;
$image_changed = FALSE;
foreach ($this->getProcessors() as $processor) {
$image_changed = $processor->applyToImage($temp_image_uri) || $image_changed;
// The file may have changed on disk after each processor has been
// applied, and PHP has a cache of file size information etc. so clear
// it here so that later calls to filesize() etc. get the correct
// information.
clearstatcache();
}
if ($image_changed) {
// Copy the temporary file back over the original image.
file_unmanaged_move($temp_image_uri, $image_uri, FILE_EXISTS_REPLACE);
}
} }
} }
...@@ -253,4 +285,15 @@ class ImageAPIOptimizePipeline extends ConfigEntityBase implements ImageAPIOptim ...@@ -253,4 +285,15 @@ class ImageAPIOptimizePipeline extends ConfigEntityBase implements ImageAPIOptim
return \Drupal::service('plugin.manager.imageapi_optimize.processor'); return \Drupal::service('plugin.manager.imageapi_optimize.processor');
} }
/**
* Clean up any temporary files created in optimization.
*/
public function __destruct() {
foreach ($this->temporaryFiles as $file) {
if (file_exists($file)) {
file_unmanaged_delete($file);
}
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment