Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to pass params through to the underlying ImageService #29

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Twill Image is a package designed to work with [Twill](https://twill.io) to disp
- [`sizes`](#sizes)
- [`columns`](#columns)
- [`srcSetWidths`](#srcSetWidths)
- [`params`](#params)
- [`preset`](#preset)
- [`render`](#render)
- [`toArray`](#toArray)
Expand Down Expand Up @@ -100,17 +101,18 @@ The JavaScript module is not required. If you prefer to rely only on the browser
The `Image` model allows you to interact fluently with a media object.

```php
$image = new A17\Twill\Image\Models\Image($object, $role, $media);
$image = new A17\Twill\Image\Models\Image($object, $role, $media, $params);

// or using the Facade
$image = TwillImage::make($object, $role, $media);
$image = TwillImage::make($object, $role, $media, $params);
```

|Argument|Type|Default|Description|
|---|---|---|---|
|`$object` (Required)|`A17\Twill\Models\Media` `A17\Twill\Models\Block` `object`| |Your Twill module or block object|
|`$role` (Required)|`string`| |`Media` role|
|`$media`|`A17\Twill\Models\Media`|`null`|`Media` instance|
|`$params`|`array`|`[]`|Array of extra parameters for the ImageService

#### Available methods

Expand Down Expand Up @@ -154,6 +156,7 @@ $image->crop('desktop')->sources([
'crop' => 'mobile', // required
'width' => 200, // optional
'height' => 200, // optional
'params' => ['q'=> 60], //optional
'srcSetWidths' => [100, 200, 400], // optional
],
[
Expand Down Expand Up @@ -219,6 +222,14 @@ Use this method to give a list a widths to generate the `srcset` attribute. With
$image->srcSetWidths([100, 150, 300, 600, 1200, 2000, 2400, 3600, 5000]);
```

##### `params`

This method allows you to pass an array of extra params for the underlying ImageService (e.g. Imgix,Glide).

```php
$image->params(['q' => 50, 'filt' => 'greyscale']);
```

##### `preset`

With this method you can use an object to pass a value to any of the above methods. You can also add a preset key to the config `config/twill-image.php` and pass the name to this method.
Expand All @@ -232,6 +243,7 @@ return [
'art_directed' => [
'crop' => 'desktop',
'width' => 700,
'params' => ['q' => 75],
'sizes' => '(max-width: 767px) 25vw, (min-width: 767px) and (max-width: 1023px) 50vw, 33vw',
'srcSetWidths' => [350, 700, 1400],
'sources' => [
Expand Down
30 changes: 28 additions & 2 deletions src/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Image implements Arrayable
*/
protected $height;

/**
* @var array The imageservice params
*/
protected $params;

/**
* @var array The media sources
*/
Expand All @@ -62,18 +67,21 @@ class Image implements Arrayable
* @param string $role
* @param null|Media $media
*/
public function __construct($object, $role, $media = null)
public function __construct($object, $role, $media = null, $params = [])
{
$this->object = $object;

$this->role = $role;

$this->media = $media;

$this->params = $params;

$this->mediaSourceService = new MediaSource(
$this->object,
$this->role,
$this->media
$this->media,
$this->params,
);

$columnsServiceClass = config('twill-image.columns_class', ImageColumns::class);
Expand Down Expand Up @@ -183,6 +191,19 @@ public function sizes($sizes)
return $this;
}

/**
* Set extra image service params
*
* @param array $params
* @return $this
*/
public function params($params)
{
$this->params = $params;

return $this;
}

/**
* Set alternative sources for the media.
*
Expand Down Expand Up @@ -211,6 +232,7 @@ public function sources($sources = [])
$source['width'] ?? null,
$source['height'] ?? null,
$source['srcSetWidths'] ?? [],
$source['params'] ?? [],
)->toArray()
];
}
Expand Down Expand Up @@ -262,6 +284,10 @@ protected function applyPreset($preset)
$this->height($preset['height']);
}

if (isset($preset['params'])) {
$this->params($preset['params']);
}

if (isset($preset['sizes'])) {
$this->sizes($preset['sizes']);
}
Expand Down
21 changes: 15 additions & 6 deletions src/Services/MediaSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,39 @@ class MediaSource implements Arrayable

protected $imageArray;

protected $params;

/**
* Create an instance of the service
*
* @param Model|Block|object $object
* @param string $role
* @param array $args
* @param Media|object|null $media
* @param int[] $srcSetWidths
* @param array $params
*/
public function __construct($object, $role, $media = null)
public function __construct($object, $role, $media = null, $params = [])
{
$this->setModel($object);

$this->role = $role;
$this->media = $media;
$this->params = $params;
}

public function generate($crop = null, $width = null, $height = null, $srcSetWidths = [])
public function generate($crop = null, $width = null, $height = null, $srcSetWidths = [], $params=[])
{
$this->setCrop($crop);
$this->setWidth($width);
$this->setHeight($height);
$this->setParams($params);
$this->setImageArray();

$this->srcSetWidths = $srcSetWidths;

return $this;
}

protected function setModel($object)
{
if (!classHasTrait($object, 'A17\Twill\Models\Behaviors\HasMedias')) {
Expand Down Expand Up @@ -112,12 +116,17 @@ protected function setHeight($height)
$this->height = $height ?? null;
}

public function setParams($params)
{
$this->params = $params ?? [];
}

protected function setImageArray()
{
$this->imageArray = $this->model->imageAsArray(
$this->role,
$this->crop,
[],
$this->params,
$this->media,
);

Expand All @@ -144,7 +153,7 @@ protected function params($width, $height = null, $format = null)
$args['fm'] = $format;
}

return $args;
return array_merge($this->params, $args);
}

protected function calcHeightFromWidth($width)
Expand Down
5 changes: 3 additions & 2 deletions src/TwillImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class TwillImage
* @param object|Model|Block $object
* @param string $role
* @param null|Media $media
* @param null|array $params
* @return Image
*/
public function make($object, $role, $media = null)
public function make($object, $role, $media = null, $params = [])
{
return new Image($object, $role, $media);
return new Image($object, $role, $media, $params);
}

/**
Expand Down