Skip to content

Commit

Permalink
Merge branch 'release/1.4.31' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Apr 22, 2018
2 parents 4ac642b + 83abb8d commit b6e1556
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 106 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.31 - 2018.04.22
### Added
* Added CraftQL support

## 1.4.30 - 2018.04.09
### Added
* Added additional profiling information
Expand Down
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,71 @@ Re-saving many images at a time can be intensive, and on certain setups may requ

All you need to do is install the plugin, and any queue jobs in Craft CMS 3 will now run entirely in the background via the CLI php, which isn't subject to the same restrictions that the web php is.

### GraphQL via CraftQL Plugin

ImageOptimize has built-in support for accessing the OptimizedImages field via GraphQL using the [CraftQL plugin](https://github.com/markhuot/craftql).

You can access all of the primary OptimizeImages methods:

```
{
entries(section:[homepage], limit:1) {
...on Homepage {
title
url
someAsset {
...on AssetsVolume {
title
optimizedImages {
...on OptimizedImagesData {
src,
srcset,
srcWebp,
srcsetWebp,
maxSrcsetWidth,
placeholderImage,
placeholderBox,
placeholderSilhouette
}
}
}
}
}
}
}
```

...as well as all of the object properties:

```
entries(section:[homepage], limit:1) {
...on Homepage {
title
url
someAsset {
...on AssetsVolume {
title
optimizedImages {
...on OptimizedImagesData {
optimizedImageUrls,
optimizedWebPImageUrls,
variantSourceWidths,
originalImageWidth,
originalImageHeight
placeholder,
placeholderSvg,
colorPalette,
placeholderWidth,
placeholderHeight
}
}
}
}
}
}
}
```

## Using Optimized Image Transforms

Once ImageOptimize is set up and configured, there's nothing left to do for optimizing your image transforms. It just works.
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.30",
"version": "1.4.31",
"keywords": [
"craft",
"cms",
Expand Down
216 changes: 125 additions & 91 deletions src/ImageOptimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use nystudio107\imageoptimize\fields\OptimizedImages;
use nystudio107\imageoptimize\imagetransforms\ImageTransformInterface;
use nystudio107\imageoptimize\listeners\GetCraftQLSchema;
use nystudio107\imageoptimize\models\Settings;
use nystudio107\imageoptimize\services\Optimize as OptimizeService;
use nystudio107\imageoptimize\services\OptimizedImages as OptimizedImagesService;
Expand All @@ -22,7 +23,6 @@
use craft\base\Field;
use craft\base\Plugin;
use craft\base\Volume;
use craft\console\Application as ConsoleApplication;
use craft\elements\Asset;
use craft\events\AssetTransformImageEvent;
use craft\events\ElementEvent;
Expand All @@ -44,7 +44,10 @@
use craft\web\twig\variables\CraftVariable;
use craft\web\Controller;

use markhuot\CraftQL\CraftQL;

use yii\base\Event;
use yii\base\Exception;

/** @noinspection MissingPropertyAnnotationsInspection */

Expand All @@ -63,6 +66,12 @@
*/
class ImageOptimize extends Plugin
{

// Constants
// =========================================================================

const CRAFTQL_PLUGIN_HANDLE = 'craftql';

// Static Properties
// =========================================================================

Expand Down Expand Up @@ -150,15 +159,23 @@ public function settingsHtml()
$settings = $this->getSettings();

// Render the settings template
return Craft::$app->getView()->renderTemplate(
'image-optimize/settings',
[
'settings' => $settings,
'imageProcessors' => $imageProcessors,
'variantCreators' => $variantCreators,
'gdInstalled' => function_exists('imagecreatefromjpeg'),
]
);
try {
return Craft::$app->getView()->renderTemplate(
'image-optimize/settings',
[
'settings' => $settings,
'imageProcessors' => $imageProcessors,
'variantCreators' => $variantCreators,
'gdInstalled' => \function_exists('imagecreatefromjpeg'),
]
);
} catch (\Twig_Error_Loader $e) {
Craft::error($e->getMessage(), __METHOD__);
} catch (Exception $e) {
Craft::error($e->getMessage(), __METHOD__);
}

return '';
}

// Protected Methods
Expand Down Expand Up @@ -210,83 +227,7 @@ protected function installEventHandlers()
$this->installAssetEventHandlers();
$this->installElementEventHandlers();
$this->installMiscEventHandlers();
}

/**
* Install our miscellaneous event handlers
*/
protected function installMiscEventHandlers()
{
// Handler: Fields::EVENT_AFTER_SAVE_FIELD
Event::on(
Fields::class,
Fields::EVENT_AFTER_SAVE_FIELD,
function (FieldEvent $event) {
Craft::debug(
'Fields::EVENT_AFTER_SAVE_FIELD',
__METHOD__
);
$settings = $this->getSettings();
/** @var Field $field */
if (!$event->isNew && $settings->automaticallyResaveImageVariants) {
$this->checkForOptimizedImagesField($event);
}
}
);

// Handler: Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS,
function (PluginEvent $event) {
if ($event->plugin === $this) {
Craft::debug(
'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS',
__METHOD__
);
$settings = $this->getSettings();
if ($settings->automaticallyResaveImageVariants) {
// After they have changed the settings, resave all of the assets
ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets();
}
}
}
);

// Handler: Volumes::EVENT_AFTER_SAVE_VOLUME
Event::on(
Volumes::class,
Volumes::EVENT_AFTER_SAVE_VOLUME,
function (VolumeEvent $event) {
Craft::debug(
'Volumes::EVENT_AFTER_SAVE_VOLUME',
__METHOD__
);
$settings = $this->getSettings();
// Only worry about this volume if it's not new
if (!$event->isNew && $settings->automaticallyResaveImageVariants) {
/** @var Volume $volume */
$volume = $event->volume;
if (!empty($volume)) {
ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume);
}
}
}
);

// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
function (PluginEvent $event) {
if ($event->plugin === $this) {
$request = Craft::$app->getRequest();
if ($request->isCpRequest) {
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('image-optimize/welcome'))->send();
}
}
}
);
$this->installCraftQLEventHandlers();
}

/**
Expand Down Expand Up @@ -375,7 +316,7 @@ function (ReplaceAssetEvent $event) {
);
/** @var Asset $element */
$element = $event->asset;
if (!empty($element->id)) {
if ($element->id !== null) {
ImageOptimize::$plugin->optimizedImages->resaveAsset($element->id);
}
}
Expand Down Expand Up @@ -434,9 +375,101 @@ function (ElementEvent $event) {
);
}


/**
* Install our miscellaneous event handlers
*/
protected function installMiscEventHandlers()
{
// Handler: Fields::EVENT_AFTER_SAVE_FIELD
Event::on(
Fields::class,
Fields::EVENT_AFTER_SAVE_FIELD,
function (FieldEvent $event) {
Craft::debug(
'Fields::EVENT_AFTER_SAVE_FIELD',
__METHOD__
);
$settings = $this->getSettings();
/** @var Field $field */
if (!$event->isNew && $settings->automaticallyResaveImageVariants) {
$this->checkForOptimizedImagesField($event);
}
}
);

// Handler: Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS,
function (PluginEvent $event) {
if ($event->plugin === $this) {
Craft::debug(
'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS',
__METHOD__
);
$settings = $this->getSettings();
if ($settings->automaticallyResaveImageVariants) {
// After they have changed the settings, resave all of the assets
ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets();
}
}
}
);

// Handler: Volumes::EVENT_AFTER_SAVE_VOLUME
Event::on(
Volumes::class,
Volumes::EVENT_AFTER_SAVE_VOLUME,
function (VolumeEvent $event) {
Craft::debug(
'Volumes::EVENT_AFTER_SAVE_VOLUME',
__METHOD__
);
$settings = $this->getSettings();
// Only worry about this volume if it's not new
if (!$event->isNew && $settings->automaticallyResaveImageVariants) {
/** @var Volume $volume */
$volume = $event->volume;
if ($volume !== null) {
ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume);
}
}
}
);

// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
function (PluginEvent $event) {
if ($event->plugin === $this) {
$request = Craft::$app->getRequest();
if ($request->isCpRequest) {
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('image-optimize/welcome'))->send();
}
}
}
);
}

/**
* Install our CraftQL event handlers
*/
protected function installCraftQLEventHandlers()
{
if (class_exists(CraftQL::class)) {
Event::on(
OptimizedImages::class,
GetCraftQLSchema::EVENT_GET_FIELD_SCHEMA,
[new GetCraftQLSchema, 'handle']
);
}
}

/**
* If the Field being saved is an OptimizedImages field, re-save the responsive
* image variants automatically
* If the Field being saved is an OptimizedImages field, re-save the
* responsive image variants automatically
*
* @param FieldEvent $event
*
Expand All @@ -456,7 +489,8 @@ protected function checkForOptimizedImagesField(FieldEvent $event)
if ($fieldLayout) {
$fields = $fieldLayout->getFields();
foreach ($fields as $field) {
if ($thisField->handle == $field->handle) {
/** @var Field $field */
if ($thisField->handle === $field->handle) {
$needToReSave = true;
}
}
Expand Down
Loading

0 comments on commit b6e1556

Please sign in to comment.