Skip to content

Commit

Permalink
Merge branch 'release/1.5.1' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Dec 28, 2018
2 parents 5688dba + 96ad0f7 commit 40928d2
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 701 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# ImageOptimize Changelog

## 1.5.0 - 2018.12.28
## 1.5.1 - 2018.12.28
### Changed
* Refactored the Imgix and Thumbor Image Transforms out to external packages

## 1.5.0 - 2018.12.27
### Changed
* Refactored out the `ImageTransform` interface to use Yii2's DI to allow them to be stand-alone components
* Respond to `Assets::EVENT_GET_ASSET_THUMB_URL` for direct thumbnail generation from third party image transform services
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ use vendor\package\MyImageTransform;
],
```

No module or plugin bootstrapping code needed to get it working.
No module or plugin bootstrapping code needed to get it working. For an example of how this works, check out [craft-imageoptimize-imgix](https://github.com/nystudio107/craft-imageoptimize-imgix) & [craft-imageoptimize-thumbor](https://github.com/nystudio107/craft-imageoptimize-thumbor).

If you want to wrap your `ImageTransform` into a plugin or module,
simply listen for the `EVENT_REGISTER_IMAGE_TRANSFORM_TYPES` event to add your `ImageTransform` to the types that ImageOptimize knows about.
Expand All @@ -727,6 +727,8 @@ Event::on(Optimize::class,

Although ImageOptimize itself uses the same mechanisms internally for `ImageTranform` implementations, this allows you to update & maintain an `ImageTransform` entirely independent of ImageOptimize.

This technique is discussed in depth in the [Writing Craft Plugins with Extensible Components](https://nystudio107.com/blog/writing-craft-plugins-with-extensible-components) article.

## ImageOptimize Roadmap

Some things to do, and ideas for potential features:
Expand Down
8 changes: 4 additions & 4 deletions 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.5.0",
"version": "1.5.1",
"keywords": [
"craft",
"cms",
Expand All @@ -29,10 +29,10 @@
"prefer-stable": true,
"require": {
"craftcms/cms": "^3.0.0",
"imgix/imgix-php": "^2.1.0",
"nystudio107/craft-imageoptimize-imgix": "^1.0.0",
"nystudio107/craft-imageoptimize-thumbor": "^1.0.0",
"ksubileau/color-thief-php": "^1.3",
"mikehaertl/php-shellcommand": "~1.2",
"99designs/phumbor": "^1.2"
"mikehaertl/php-shellcommand": "~1.2"
},
"autoload": {
"psr-4": {
Expand Down
36 changes: 36 additions & 0 deletions src/ImageOptimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use craft\events\GenerateTransformEvent;
use craft\events\PluginEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterTemplateRootsEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\events\ReplaceAssetEvent;
use craft\events\VolumeEvent;
Expand All @@ -47,6 +48,7 @@
use craft\web\twig\variables\CraftVariable;
use craft\web\Controller;
use craft\web\UrlManager;
use craft\web\View;

use markhuot\CraftQL\CraftQL;

Expand Down Expand Up @@ -264,6 +266,10 @@ protected function installEventHandlers()
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
$this->installSiteEventListeners();
}
// Install only for non-console cp requests
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
$this->installCpEventListeners();
}
}

/**
Expand Down Expand Up @@ -541,6 +547,36 @@ function (RegisterUrlRulesEvent $event) {
);
}

/**
* Install site event listeners for cp requests only
*/
protected function installCpEventListeners()
{
// Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_LOAD_PLUGINS,
function () {
// Install these only after all other plugins have loaded
Event::on(
View::class,
View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
function (RegisterTemplateRootsEvent $e) {
// Register the root directodies
$allImageTransformTypes = ImageOptimize::$plugin->optimize->getAllImageTransformTypes();
/** @var ImageTransformInterface $imageTransformType */
foreach ($allImageTransformTypes as $imageTransformType) {
list($id, $baseDir) = $imageTransformType::getTemplatesRoot();
if (is_dir($baseDir)) {
$e->roots[$id] = $baseDir;
}
}
}
);
}
);
}

/**
* Return the custom frontend routes
*
Expand Down
2 changes: 1 addition & 1 deletion src/imagetransforms/CraftImageTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getSettingsHtml()
{
$imageProcessors = ImageOptimize::$plugin->optimize->getActiveImageProcessors();
$variantCreators = ImageOptimize::$plugin->optimize->getActiveVariantCreators();
return Craft::$app->getView()->renderTemplate('image-optimize/settings/image-transforms/craft.twig', [
return Craft::$app->getView()->renderTemplate('craft-image-transform/settings/image-transforms/craft.twig', [
'imageTransform' => $this,
'imageProcessors' => $imageProcessors,
'variantCreators' => $variantCreators,
Expand Down
79 changes: 45 additions & 34 deletions src/imagetransforms/ImageTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use craft\base\SavableComponent;
use craft\elements\Asset;
use craft\helpers\FileHelper;
use craft\helpers\StringHelper;
use craft\models\AssetTransform;

/**
Expand All @@ -28,15 +30,38 @@ abstract class ImageTransform extends SavableComponent implements ImageTransform

use ImageTransformTrait;

// Static Methods
// =========================================================================

/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('image-optimize', 'Generic Transform');
}

/**
* @inheritdoc
*/
public static function getTemplatesRoot(): array
{
$reflect = new \ReflectionClass(static::class);
$classPath = FileHelper::normalizePath(
dirname($reflect->getFileName())
. '/../templates'
)
. DIRECTORY_SEPARATOR;
$id = StringHelper::toKebabCase($reflect->getShortName());

return [$id, $classPath];
}

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

/**
* @param Asset $asset
* @param AssetTransform|null $transform
* @param array $params
*
* @return string|null
* @inheritdoc
*/
public function getTransformUrl(Asset $asset, $transform, array $params = [])
{
Expand All @@ -46,23 +71,15 @@ public function getTransformUrl(Asset $asset, $transform, array $params = [])
}

/**
* @param string $url
* @param Asset $asset
* @param AssetTransform|null $transform
* @param array $params
*
* @return string
* @inheritdoc
*/
public function getWebPUrl(string $url, Asset $asset, $transform, array $params = []): string
{
return $url;
}

/**
* @param Asset $asset
* @param array $params
*
* @return null|string
* @inheritdoc
*/
public function getPurgeUrl(Asset $asset, array $params = [])
{
Expand All @@ -72,32 +89,15 @@ public function getPurgeUrl(Asset $asset, array $params = [])
}

/**
* @param string $url
* @param array $params
*
* @return bool
* @inheritdoc
*/
public function purgeUrl(string $url, array $params = []): bool
{
return true;
}

/**
* @return array
*/
public function getTransformParams(): array
{
$params = [
];

return $params;
}

/**
* @param Asset $asset
*
* @return mixed
* @throws \yii\base\InvalidConfigException
* @inheritdoc
*/
public function getAssetUri(Asset $asset)
{
Expand Down Expand Up @@ -131,6 +131,17 @@ public function prefetchRemoteFile($url)
curl_close($ch);
}

/**
* @inheritdoc
*/
public function getTransformParams(): array
{
$params = [
];

return $params;
}

/**
* Append an extension a passed url or path
*
Expand Down
27 changes: 26 additions & 1 deletion src/imagetransforms/ImageTransformInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,24 @@
*/
interface ImageTransformInterface extends SavableComponentInterface
{
// Static Methods
// =========================================================================

/**
* Return an array that contains the template root and corresponding file
* system directory for the Image Transform's templates
*
* @return array
* @throws \ReflectionException
*/
public static function getTemplatesRoot(): array;

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

/**
* Return a URL to a transformed images
*
* @param Asset $asset
* @param AssetTransform|null $transform
* @param array $params
Expand All @@ -34,6 +48,8 @@ interface ImageTransformInterface extends SavableComponentInterface
public function getTransformUrl(Asset $asset, $transform, array $params = []);

/**
* Return a URL to the webp version of the transformed image
*
* @param string $url
* @param Asset $asset
* @param AssetTransform|null $transform
Expand All @@ -44,6 +60,8 @@ public function getTransformUrl(Asset $asset, $transform, array $params = []);
public function getWebPUrl(string $url, Asset $asset, $transform, array $params = []): string;

/**
* Return the URL that should be used to purge the Asset
*
* @param Asset $asset
* @param array $params
*
Expand All @@ -52,27 +70,34 @@ public function getWebPUrl(string $url, Asset $asset, $transform, array $params
public function getPurgeUrl(Asset $asset, array $params = []);

/**
* Purge the URL from the service's cache
*
* @param string $url
* @param array $params
*
* @return bool
*/
public function purgeUrl(string $url, array $params = []): bool;


/**
* Return the URI to the asset
*
* @param Asset $asset
*
* @return mixed
*/
public function getAssetUri(Asset $asset);

/**
* Prefetch the remote file to prime the cache
*
* @param string $url
*/
public function prefetchRemoteFile($url);

/**
* Get the parameters needed for this transform
*
* @return array
*/
public function getTransformParams(): array;
Expand Down
Loading

0 comments on commit 40928d2

Please sign in to comment.