From 09e560f1b40b2dedc3de882cbf508bb02ad56a10 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 12 Jul 2018 13:52:15 -0400 Subject: [PATCH 1/2] Smarter appending of variant creator suffixes, to handle URLs that have query strings Signed-off-by: Andrew Welch --- src/imagetransforms/CraftImageTransform.php | 10 ++-- src/imagetransforms/ImageTransform.php | 52 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/imagetransforms/CraftImageTransform.php b/src/imagetransforms/CraftImageTransform.php index e15654a3..17903352 100644 --- a/src/imagetransforms/CraftImageTransform.php +++ b/src/imagetransforms/CraftImageTransform.php @@ -36,9 +36,7 @@ class CraftImageTransform extends ImageTransform implements ImageTransformInterf */ public static function getTransformUrl(Asset $asset, $transform, array $params = []) { - $generateTransformsBeforePageLoad = isset($params['generateTransformsBeforePageLoad']) - ? $params['generateTransformsBeforePageLoad'] - : true; + $generateTransformsBeforePageLoad = $params['generateTransformsBeforePageLoad'] ?? true; // Generate the URLs to the optimized images $assets = Craft::$app->getAssets(); $url = $assets->getAssetUrl($asset, $transform, $generateTransformsBeforePageLoad); @@ -53,7 +51,7 @@ public static function getTransformUrl(Asset $asset, $transform, array $params = */ public static function getWebPUrl(string $url): string { - $url = $url.".webp"; + $url = self::appendExtension($url, '.webp'); return $url; } @@ -65,9 +63,7 @@ public static function getTransformParams(): array { $settings = ImageOptimize::$plugin->getSettings(); // Get our $generateTransformsBeforePageLoad setting - $generateTransformsBeforePageLoad = isset($settings->generateTransformsBeforePageLoad) - ? $settings->generateTransformsBeforePageLoad - : true; + $generateTransformsBeforePageLoad = $settings->generateTransformsBeforePageLoad ?? true; $params = [ 'generateTransformsBeforePageLoad' => $generateTransformsBeforePageLoad, ]; diff --git a/src/imagetransforms/ImageTransform.php b/src/imagetransforms/ImageTransform.php index 34c203a3..d61dcd1d 100644 --- a/src/imagetransforms/ImageTransform.php +++ b/src/imagetransforms/ImageTransform.php @@ -117,4 +117,56 @@ public static function prefetchRemoteFile($url) curl_exec($ch); curl_close($ch); } + + /** + * Append an extension a passed url or path + * + * @param $pathOrUrl + * @param $extension + * + * @return string + */ + public static function appendExtension($pathOrUrl, $extension): string + { + $path = self::decomposeUrl($pathOrUrl); + $path_parts = pathinfo($path['path']); + $new_path = $path_parts['filename'] . '.' . $path_parts['extension'] . $extension; + if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== '.') { + $new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path; + $new_path = preg_replace('#/+#', '/', $new_path); + } + $output = $path['prefix'] . $new_path . $path['suffix']; + + return $output; + } + + // Protected Methods + // ========================================================================= + + /** + * Decompose a url into a prefix, path, and suffix + * + * @param $pathOrUrl + * + * @return array + */ + protected static function decomposeUrl($pathOrUrl): array + { + $result = array(); + + if (filter_var($pathOrUrl, FILTER_VALIDATE_URL)) { + $url_parts = parse_url($pathOrUrl); + $result['prefix'] = $url_parts['scheme'] . '://' . $url_parts['host']; + $result['path'] = $url_parts['path']; + $result['suffix'] = ''; + $result['suffix'] .= empty($url_parts['query']) ? '' : '?' . $url_parts['query']; + $result['suffix'] .= empty($url_parts['fragment']) ? '' : '#' . $url_parts['fragment']; + } else { + $result['prefix'] = ''; + $result['path'] = $pathOrUrl; + $result['suffix'] = ''; + } + + return $result; + } } From 19c5b52616aff32301abf0de34e64250c988f4cf Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 12 Jul 2018 13:52:33 -0400 Subject: [PATCH 2/2] Version 1.4.34 Signed-off-by: Andrew Welch --- CHANGELOG.md | 4 ++++ composer.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26927918..9fe96988 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # ImageOptimize Changelog +## 1.4.34 - 2018.07.12 +### Changed +* Smarter appending of variant creator suffixes, to handle URLs that have query strings + ## 1.4.33 - 2018.05.24 ### Changed * Handle JPEGs coming in as both `jpg` & `jpeg` for the detected file format diff --git a/composer.json b/composer.json index 4c660de2..54186813 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft-imageoptimize", "description": "Automatically create & optimize responsive image transforms, using either native Craft transforms or a service like Imgix, with zero template changes.", "type": "craft-plugin", - "version": "1.4.33", + "version": "1.4.34", "keywords": [ "craft", "cms",