Skip to content

Commit

Permalink
Merge branch 'release/1.4.15' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Feb 14, 2018
2 parents 0210131 + 42ac3f2 commit 201c6a6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 27 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ImageOptimize Changelog

## 1.4.15 - 2018.02.14
### Changed
* Resave all asset volumes after the settings are changed (not just the Transform Method)
* Save the Optimized Image Variants via an async queue when possible
* Detect `gd` installation via `function_exists` instead of `extension_loaded`
* Handle protocol-relative siteUrls better

## 1.4.14 - 2018.02.09
### Changed
* Fixed an issue where certain settings could not be saved via the AdminCP
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.14",
"version": "1.4.15",
"keywords": [
"craft",
"cms",
Expand Down
20 changes: 3 additions & 17 deletions src/ImageOptimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ class ImageOptimize extends Plugin
*/
public static $transformParams;

/**
* @var string
*/
public static $previousTransformMethod = null;

// Public Methods
// =========================================================================

Expand All @@ -99,7 +94,6 @@ public function init()

// Cache some settings
$settings = $this->getSettings();
self::$previousTransformMethod = $settings->transformMethod;
self::$transformClass = ImageTransformInterface::IMAGE_TRANSFORM_MAP[$settings->transformMethod];
self::$transformParams = self::$transformClass::getTransformParams();

Expand Down Expand Up @@ -174,16 +168,8 @@ function (PluginEvent $event) {
'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS',
__METHOD__
);
$settings = self::getSettings();
// If they changed the global transform method, we need to resave all Asset Volumes
if (self::$previousTransformMethod != $settings->transformMethod) {
self::$previousTransformMethod = $settings->transformMethod;
self::$transformClass = ImageTransformInterface::IMAGE_TRANSFORM_MAP[
$settings->transformMethod
];
self::$transformParams = self::$transformClass::getTransformParams();
ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets();
}
// After they have changed the settings, resave all of the assets
ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets();
}
}
);
Expand Down Expand Up @@ -404,7 +390,7 @@ public function settingsHtml()
'settings' => $settings,
'imageProcessors' => $imageProcessors,
'variantCreators' => $variantCreators,
'gdInstalled' => extension_loaded('gd'),
'gdInstalled' => function_exists('imagecreatefromjpeg'),
]
);
}
Expand Down
33 changes: 27 additions & 6 deletions src/fields/OptimizedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,22 @@ public function afterElementSave(ElementInterface $asset, bool $isNew)
{
parent::afterElementSave($asset, $isNew);
// Update our OptimizedImages Field data now that the Asset has been saved
ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($this, $asset);
if ($asset instanceof Asset) {
if ($isNew) {
/**
* If this is a newly uploaded/created Asset, we can save the variants
* via a queue job to prevent it from blocking
*/
ImageOptimize::$plugin->optimizedImages->resaveAsset($asset->id);
} else {
/**
* If it's not a newly uploaded/created Asset, they may have edited
* the image with the ImageEditor, so we need to update the variants
* immediately, so the AssetSelectorHud displays the new images
*/
ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($this, $asset);
}
}
}

/**
Expand All @@ -150,12 +165,18 @@ public function normalizeValue($value, ElementInterface $asset = null)
if (is_string($value) && !empty($value)) {
$value = Json::decodeIfJson($value);
}
// If it's not an array, default it to null
if (!is_array($value)) {
$value = null;
// If we're passed in an array, make a model from it
if (is_array($value)) {
// Create a new OptimizedImage model and populate it
$model = new OptimizedImage($value);
} else {
if ($value instanceof OptimizedImage) {
$model = $value;
} else {
// Just create a new empty model
$model = new OptimizedImage(null);
}
}
// Create a new OptimizedImage model and populate it
$model = new OptimizedImage($value);

return $model;
}
Expand Down
3 changes: 3 additions & 0 deletions src/imagetransforms/ImageTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public static function prefetchRemoteFile($url)
} else {
try {
$url = UrlHelper::siteUrl($url, null, $protocol);
if (UrlHelper::isProtocolRelativeUrl($url)) {
$url = UrlHelper::urlWithScheme($url, $protocol);
}
} catch (Exception $e) {
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/models/OptimizedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ public function getRemoteFileSize($url, $formatSize = true, $useHead = true)
} else {
try {
$url = UrlHelper::siteUrl($url, null, $protocol);
if (UrlHelper::isProtocolRelativeUrl($url)) {
$url = UrlHelper::urlWithScheme($url, $protocol);
}
} catch (Exception $e) {
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/services/OptimizedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ public function resaveAllVolumesAssets()
* OptimizedImages field in the FieldLayout
*
* @param Volume $volume
*
* @throws \yii\base\InvalidConfigException
*/
public function resaveVolumeAssets(Volume $volume)
{
Expand Down Expand Up @@ -337,7 +339,7 @@ public function resaveVolumeAssets(Volume $volume)

$queue = Craft::$app->getQueue();
$jobId = $queue->push(new ResaveOptimizedImages([
'description' => Craft::t('image-optimize', 'Resaving Assets in {name}', ['name' => $volume->name]),
'description' => Craft::t('image-optimize', 'Optimizing images in {name}', ['name' => $volume->name]),
'criteria' => [
'siteId' => $siteId,
'volumeId' => $volume->id,
Expand Down Expand Up @@ -367,7 +369,7 @@ public function resaveAsset(int $id)
{
$queue = Craft::$app->getQueue();
$jobId = $queue->push(new ResaveOptimizedImages([
'description' => Craft::t('image-optimize', 'Resaving new Asset id {id}', ['id' => $id]),
'description' => Craft::t('image-optimize', 'Optimizing image id {id}', ['id' => $id]),
'criteria' => [
'id' => $id,
'status' => null,
Expand Down
2 changes: 1 addition & 1 deletion src/services/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function generatePlaceholderSvg(string $tempPath): string

if (!empty($tempPath)) {
// Potracio depends on `gd` being installed
if (extension_loaded('gd')) {
if (function_exists('imagecreatefromjpeg')) {
$pot = new Potracio();
$pot->loadImageFromFile($tempPath);
$pot->process();
Expand Down

0 comments on commit 201c6a6

Please sign in to comment.