Patch AVIF 3370258
The snippet can be accessed without any authentication.
Authored by
Pierre
Patch ajusté pour s'appliquer après 3370253
avif-3370258.diff 4.24 KiB
diff --git a/src/Avif.php b/src/Avif.php
index efb6704..808d708 100644
--- a/src/Avif.php
+++ b/src/Avif.php
@@ -113,8 +113,7 @@ class Avif {
* The location of the Avif image if successful, FALSE if not successful.
*/
public function getAvifCopy($uri, $quality = NULL) {
- $pathInfo = pathinfo($uri);
- $destination = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.avif';
+ $destination = $this->getAvifDestination($uri, '@directory@filename.@extension.avif');
// Don't start generating the webp copy if it already exists or if
// generation is in progress in another thread.
@@ -185,7 +184,7 @@ class Avif {
* Avif version of srcset
*/
public function getAvifSrcset($srcset) {
- return preg_replace('/\.(png|jpg|jpeg)(\\?.*?)?(,| |$)/i', '.avif\\2\\3', $srcset);
+ return preg_replace('/\.(png|jpg|jpeg)(\\?.*?)?(,| |$)/i', '.\\1.avif\\2\\3', $srcset);
}
/**
@@ -212,8 +211,7 @@ class Avif {
$plugin = $type->createInstance($this->processor);
- $pathInfo = pathinfo($uri);
- $destination = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.avif';
+ $destination = $this->getAvifDestination($uri, '@directory@filename.@extension.avif');
if ($plugin->convert($uri, $quality, $destination)) {
// load image
@@ -226,4 +224,26 @@ class Avif {
return $avifGenerated;
}
+ /**
+ * Return source destination for given uri.
+ * @param $uri
+ * @param $template
+ *
+ * @return string
+ */
+ public function getAvifDestination($uri, $template) {
+ $path_info = pathinfo($uri);
+ $path = $path_info['dirname'];
+ // If path is just the stream wrapper scheme, e.g. "public:" or
+ // "temporary:" append two slashes, otherwise just one.
+ $path .= (substr($path, (strlen($path) - 1)) === ':') ? '//' : '/';
+ $destination = strtr($template, [
+ '@directory' => $path,
+ '@filename' => $path_info['filename'],
+ '@extension' => $path_info['extension'],
+ ]);
+
+ return $destination;
+ }
+
}
diff --git a/src/Controller/ImageStyleDownloadController.php b/src/Controller/ImageStyleDownloadController.php
index 65af087..7dcb2eb 100644
--- a/src/Controller/ImageStyleDownloadController.php
+++ b/src/Controller/ImageStyleDownloadController.php
@@ -114,34 +114,7 @@ class ImageStyleDownloadController extends FileDownloadController {
$image_uri = $scheme . '://' . $target;
if ($avifWanted = preg_match('/\.avif$/', $image_uri)) {
-
- $path_info = pathinfo($image_uri);
-
- $possible_image_uris = [
- // If the image style converted the extension, it has been added to the
- // original file, resulting in filenames like image.png.jpeg. So to find
- // the actual source image, we remove the extension and check if that
- // image exists.
- $path_info['dirname'] . DIRECTORY_SEPARATOR . $path_info['filename'],
- ];
-
- // Try out the different possible sources for a avif image.
- $extensions = [
- '.jpg',
- '.jpeg',
- '.png',
- ];
- foreach ($extensions as $extension) {
- $possible_image_uris[] = str_replace('.avif', mb_strtoupper($extension), $image_uri);
- $possible_image_uris[] = str_replace('.avif', $extension, $image_uri);
- }
-
- foreach ($possible_image_uris as $possible_image_uri) {
- if (file_exists($possible_image_uri)) {
- $image_uri = $possible_image_uri;
- break;
- }
- }
+ $image_uri = $this->avif->getAvifDestination($image_uri, '@directory@filename');
}
// Don't try to generate file if source is missing.
diff --git a/src/Plugin/AvifProcessor/ImageMagick.php b/src/Plugin/AvifProcessor/ImageMagick.php
index 6fabf0d..cf5170c 100644
--- a/src/Plugin/AvifProcessor/ImageMagick.php
+++ b/src/Plugin/AvifProcessor/ImageMagick.php
@@ -100,9 +100,7 @@ class ImageMagick extends AvifProcessorBase implements ContainerFactoryPluginInt
'quality' => $quality
]);
- $pathInfo = pathinfo($image_uri);
- $destination = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.avif';
- if ($ImageMagickImg->save($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.avif')) {
+ if ($ImageMagickImg->save($destination)) {
$avif = $destination;
}
else {
Please register or sign in to comment