Skip to content

Commit

Permalink
Add option to set a custom ImageService
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrckvzn committed Dec 21, 2021
1 parent 996de31 commit 0fa7799
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 41 deletions.
76 changes: 59 additions & 17 deletions src/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use A17\Twill\Image\Services\ImageColumns;
use Illuminate\Contracts\Support\Arrayable;
use A17\Twill\Image\Exceptions\ImageException;
use A17\Twill\Services\MediaLibrary\ImageServiceInterface;

class Image implements Arrayable
{
Expand Down Expand Up @@ -57,6 +58,11 @@ class Image implements Arrayable
*/
protected $srcSetWidths = [];

/**
* @var string|ImageServiceInterface ImageService instance or class name
*/
protected $service;

/**
* @param object|Model $object
* @param string $role
Expand All @@ -70,19 +76,28 @@ public function __construct($object, $role, $media = null)

$this->media = $media;

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

$columnsServiceClass = config('twill-image.columns_class', ImageColumns::class);

if ($columnsServiceClass::shouldInstantiateService()) {
$this->columnsService = new $columnsServiceClass();
}
}

/**
* @return MediaSource
*/
private function mediaSourceService()
{
return isset($this->mediaSourceService)
? $this->mediaSourceService
: $this->mediaSourceService = new MediaSource(
$this->object,
$this->role,
$this->media,
$this->service,
);
}

/**
* Pick a preset from the configuration file or pass an array with the image configuration
*
Expand Down Expand Up @@ -191,9 +206,36 @@ public function sizes($sizes)
*/
public function sources($sources = [])
{
$this->sources = [];
$this->sources = $sources;

return $this;
}

/**
* Set the ImageService to use instead of the one provided
* by the service container
*
* @param array $service
* @return $this
*/
public function service($service)
{
$this->service = $service;

foreach ($sources as $source) {
return $this;
}

/**
* Set alternative sources for the media.
*
* @param array $sources
* @return $this
*/
public function generateSources()
{
$sources = [];

foreach ($this->sources ?? [] as $source) {
if (!isset($source['media_query']) && !isset($source['mediaQuery']) && !isset($source['columns'])) {
throw new ImageException("Media query is mandatory in sources.");
}
Expand All @@ -202,11 +244,11 @@ public function sources($sources = [])
throw new ImageException("Crop name is mandatory in sources.");
}

$this->sources[] = [
$sources[] = [
"mediaQuery" => isset($source['columns'])
? $this->mediaQueryColumns($source['columns'])
: $source['media_query'] ?? $source['mediaQuery'],
"image" => $this->mediaSourceService->generate(
"image" => $this->mediaSourceService()->generate(
$source['crop'],
$source['width'] ?? null,
$source['height'] ?? null,
Expand All @@ -215,7 +257,7 @@ public function sources($sources = [])
];
}

return $this;
return $sources;
}

/**
Expand All @@ -231,14 +273,14 @@ public function render($overrides = [])
public function toArray()
{
$arr = [
"image" => $this->mediaSourceService->generate(
"image" => $this->mediaSourceService()->generate(
$this->crop,
$this->width,
$this->height,
$this->srcSetWidths
)->toArray(),
"sizes" => $this->sizes,
"sources" => $this->sources,
"sources" => $this->generateSources(),
];

return array_filter($arr);
Expand Down Expand Up @@ -270,16 +312,16 @@ protected function applyPreset($preset)
$this->columns($preset['columns']);
}

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

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

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

if (isset($preset['service'])) {
$this->service($preset['service']);
}
}
}
58 changes: 34 additions & 24 deletions src/Services/MediaSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use A17\Twill\Models\Block;
use A17\Twill\Models\Media;
use A17\Twill\Models\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Contracts\Support\Arrayable;
use A17\Twill\Image\Exceptions\ImageException;
use A17\Twill\Services\MediaLibrary\ImageService;
use A17\Twill\Services\MediaLibrary\ImageServiceInterface;

class MediaSource implements Arrayable
{
Expand All @@ -17,6 +19,8 @@ class MediaSource implements Arrayable

public const FORMAT_WEBP = 'webp';

public const CROP_KEYS = ['crop_x', 'crop_y', 'crop_w', 'crop_h'];

protected $model;

protected $role;
Expand All @@ -40,14 +44,15 @@ class MediaSource implements Arrayable
* @param string $role
* @param array $args
* @param Media|object|null $media
* @param int[] $srcSetWidths
* @param ImageServiceInterface|string $service
*/
public function __construct($object, $role, $media = null)
public function __construct($object, $role, $media = null, $service = null)
{
$this->setModel($object);

$this->role = $role;
$this->media = $media;
$this->service = $this->setService($service);
}

public function generate($crop = null, $width = null, $height = null, $srcSetWidths = [])
Expand All @@ -71,6 +76,16 @@ protected function setModel($object)
$this->model = $object;
}

protected function setService($service)
{
if (isset($service) && is_string($service) && class_exists($service)) {
return App::make($service);
} elseif (isset($service) && is_object($service)) {
return $service;
}
return app('imageService');
}

/**
* Set model crop
*
Expand Down Expand Up @@ -114,12 +129,10 @@ protected function setHeight($height)

protected function setImageArray()
{
$this->imageArray = $this->model->imageAsArray(
$this->role,
$this->crop,
[],
$this->media,
);
$this->imageArray = [
'width' => $this->media()->pivot->crop_w ?? $this->media()->width,
'height' => $this->media()->pivot->crop_h ?? $this->media()->height,
];

if (empty($this->imageArray)) {
throw new ImageException(
Expand Down Expand Up @@ -157,6 +170,15 @@ protected function calcHeightFromWidth($width)
return (int) $height;
}

protected function image($media, $params)
{
return $this->service->getUrlWithCrop(
$media->uuid,
Arr::only($media->pivot->toArray(), self::CROP_KEYS),
$params
);
}

public function src()
{
return $this->getSrc($this->width, $this->height);
Expand All @@ -171,25 +193,13 @@ protected function getSrc($width, $height, $format = null)
{
$params = $this->params($width, $height, $format);

return $this->model->image(
$this->role,
$this->crop,
$params,
false,
false,
$this->media,
);
return $this->image($this->media(), $params);
}

public function lqipBase64()
{
return $this->media
? $this->media->pivot->lqip_data ??
ImageService::getTransparentFallbackUrl()
: $this->model->lowQualityImagePlaceholder(
$this->role,
$this->crop,
);
return $this->media()->pivot->lqip_data ??
$this->service->getTransparentFallbackUrl();
}

public function srcSet()
Expand Down

0 comments on commit 0fa7799

Please sign in to comment.