Skip to content

Commit

Permalink
Add support for passing URLs to transform methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Mar 2, 2020
1 parent 1a6adf3 commit 1955dd8
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`*
Expand All @@ -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() %}
Expand All @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
79 changes: 79 additions & 0 deletions src/RemoteAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Thumbro for Craft CMS
*
* @link https://ethercreative.co.uk
* @copyright Copyright (c) 2020 Ether Creative
*/

namespace ether\thumbro;

/**
* Class RemoteAsset
*
* @author Ether Creative
* @package ether\thumbro
*/
class RemoteAsset
{

public $url;
public $width;
public $height;
public $extension;

public function __construct ($url)
{
$this->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;
}

}
15 changes: 10 additions & 5 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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(
Expand Down
14 changes: 10 additions & 4 deletions src/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!');

Expand Down

0 comments on commit 1955dd8

Please sign in to comment.