From 1955dd8831620ea9150585745d69836e228c6e53 Mon Sep 17 00:00:00 2001 From: Tam Date: Mon, 2 Mar 2020 15:04:03 +0000 Subject: [PATCH] Add support for passing URLs to transform methods --- CHANGELOG.md | 4 +++ README.md | 14 +++++--- composer.json | 2 +- src/RemoteAsset.php | 79 +++++++++++++++++++++++++++++++++++++++++++++ src/Service.php | 15 ++++++--- src/Variable.php | 14 +++++--- 6 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 src/RemoteAsset.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 662be2d..4c428ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.0.3 - 2020-03-02 +### Added +- Add support for passing URLs to transform methods + ## 1.0.2 - 2020-02-25 ### Fixed - Fix additional transform options not being passed to picture srcset / placeholder diff --git a/README.md b/README.md index 6bd9f06..9d8d56e 100644 --- a/README.md +++ b/README.md @@ -42,13 +42,16 @@ Thumbor. This is especially useful when working in Docker: ```php return [ - 'imageUrlModifier' => function ($url) { + 'imageUrlModifier' => function ($url, $isRemoteAsset) { // Replace the site's URL with the internal docker container name (web) return str_replace(getenv('DEFAULT_SITE_URL'), 'web', $url); }, ]; ``` +`$isRemoteAsset` will be true if a URL string was passed to one of the transform +methods below. + ### thumborUrlModifier [callable|null] *Default: `null`* @@ -59,10 +62,11 @@ access the file contents in the `picture` method. Works the same as ## Usage -### `img(Asset $asset, array $transforms, array $config = [])` +### `img(Asset|string $asset, array $transforms, array $config = [])` Transform a single image to one or more sizes. The config array will be merged -into every transform. +into every transform. You can pass either an asset or a string URL as the first +argument. ```twig {% set img = craft.assets.one() %} @@ -82,13 +86,15 @@ into every transform. ]) %} ``` -### `picture(Asset $asset, array $transform, array $config = [])` +### `picture(Asset|string $asset, array $transform, array $config = [])` Will output the image in a `picture` tag for dynamic lazy loading with a low quality placeholder. The function signature matches that of the `img` method, with the exception that `$transform` only accepts a single transform object, not an array of multiple objects. +You can pass either an asset or a string URL as the first argument. + This function will inject the necessary JavaScript to dynamically load the image. You can disable this by passing the `noJs` option to the `$config` and setting it to `true`. diff --git a/composer.json b/composer.json index 25fc658..be06442 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "ether/thumbro", "description": "Craft CMS image transformations powered by Thumbor", - "version": "1.0.2", + "version": "1.0.3", "type": "craft-plugin", "license": "proprietary", "minimum-stability": "dev", diff --git a/src/RemoteAsset.php b/src/RemoteAsset.php new file mode 100644 index 0000000..0f28559 --- /dev/null +++ b/src/RemoteAsset.php @@ -0,0 +1,79 @@ +url = $url; + $size = getimagesize($url); + $this->width = $size[0]; + $this->height = $size[1]; + $this->extension = [ + 0 => 'UNKNOWN', + 1 => 'gif', + 2 => 'jpeg', + 3 => 'png', + 4 => 'swf', + 5 => 'psd', + 6 => 'bmp', + 7 => 'tiff', + 8 => 'tiff', + 9 => 'jpc', + 10 => 'jp2', + 11 => 'jpx', + 12 => 'jb2', + 13 => 'swc', + 14 => 'iff', + 15 => 'wbmp', + 16 => 'xbm', + 17 => 'ico', + 18 => 'count', + ][$size[2]]; + } + + public function getUrl () + { + return $this->url; + } + + public function getWidth () + { + return $this->width; + } + + public function getHeight () + { + return $this->height; + } + + public function getExtension () + { + return $this->extension; + } + + public function getFocalPoint () + { + return null; + } + +} diff --git a/src/Service.php b/src/Service.php index 6901e4c..556dc7c 100644 --- a/src/Service.php +++ b/src/Service.php @@ -40,16 +40,16 @@ public function init () } /** - * @param Asset $image + * @param Asset|RemoteAsset $image * @param array $transforms * * @return array|null */ public function transform ($image, $transforms) { - if ($image->getExtension() === 'svg') + if ($image->getExtension() === 'svg' || $image->getExtension() === 'UNKNOWN') { - Craft::error('Thumbor does not support SVG images.'); + Craft::error('Thumbor does not support SVG or unknown image types.'); return null; } @@ -273,13 +273,18 @@ private function _parseFilterArgsValue ($args) return $args; } - private function _getImageUrl (Asset $asset) + /** + * @param Asset|RemoteAsset $asset + * + * @return string + */ + private function _getImageUrl ($asset) { $settings = Thumbro::getInstance()->getSettings(); $url = $asset->getUrl(); if (is_callable($settings->imageUrlModifier)) - $url = call_user_func($settings->imageUrlModifier, $url); + $url = call_user_func($settings->imageUrlModifier, $url, $asset instanceof RemoteAsset); return rawurlencode( preg_replace( diff --git a/src/Variable.php b/src/Variable.php index ab5be73..4e8a7e4 100644 --- a/src/Variable.php +++ b/src/Variable.php @@ -26,14 +26,17 @@ class Variable { /** - * @param Asset $asset + * @param Asset|string $asset * @param array $transforms * @param array $config * * @return ThumbroImage[] */ - public function img (Asset $asset, array $transforms, array $config = []) + public function img ($asset, array $transforms, array $config = []) { + if (is_string($asset)) + $asset = new RemoteAsset($asset); + $thumbro = Thumbro::getInstance(); $settings = $thumbro->getSettings(); $single = false; @@ -58,15 +61,18 @@ public function img (Asset $asset, array $transforms, array $config = []) } /** - * @param Asset $asset + * @param Asset|string $asset * @param array $transform * @param array $config * * @return Markup * @throws Exception */ - public function picture (Asset $asset, array $transform, array $config = []) + public function picture ($asset, array $transform, array $config = []) { + if (is_string($asset)) + $asset = new RemoteAsset($asset); + if (!ArrayHelper::isAssociative($transform, true)) throw new Exception('The `picture` method only supports a single transform!');