Skip to content

Commit

Permalink
Merge branch 'release/1.4.34' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jul 12, 2018
2 parents 09ea9e6 + 19c5b52 commit e4c966a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 3 additions & 7 deletions src/imagetransforms/CraftImageTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -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,
];
Expand Down
52 changes: 52 additions & 0 deletions src/imagetransforms/ImageTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit e4c966a

Please sign in to comment.