Skip to content

Commit

Permalink
Take sources and sizes as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrckvzn committed Oct 1, 2021
1 parent 91f7f96 commit 03e3d7f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 85 deletions.
22 changes: 0 additions & 22 deletions src/Models/Interfaces/ImageSource.php

This file was deleted.

149 changes: 86 additions & 63 deletions src/Models/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use A17\Twill\Image\Exceptions\ImageException;
use A17\Twill\Image\Models\Interfaces\ImageSource;

class Source implements ImageSource, Arrayable
class Source implements Arrayable
{
public const AUTO_WIDTHS = [
250,
Expand Down Expand Up @@ -48,6 +48,10 @@ class Source implements ImageSource, Arrayable

protected $imageArray;

protected $sources;

protected $sizes;

/**
* Build a Source to be used with A17\Twill\Image\Models\Image
*
Expand All @@ -71,6 +75,22 @@ public function __construct(
$this->setPreset($preset);
$this->setCrop($args['crop'] ?? null);
$this->setImageArray();
$this->setSources($args['sources'] ?? null);
$this->setSizes($args['sizes'] ?? null);
}

protected function setSources($sources)
{
$this->sources = $sources ?? $this->preset['sources'] ?? [
[
'crop' => $this->crop,
]
];
}

protected function setSizes($sizes)
{
$this->sizes = $sizes ?? $this->preset['sizes'] ?? null;
}

/**
Expand Down Expand Up @@ -113,33 +133,6 @@ public function caption(): string
return $this->model->imageCaption($this->role, $this->media);
}

public function srcSets()
{
$srcSets = [];

foreach ($this->sources() ?? [] as $sources) {
$srcset = [];
foreach ($sources['sources'] as $source) {
if (isset($source['src']) && isset($source['width'])) {
$srcset[] = sprintf(
'%s %dw',
$source['src'],
$source['width'],
);
}
}
$srcSets[] = [
'srcset' => join(',', $srcset),
'type' => $sources['type'],
'mediaQuery' => $sources['mediaQuery'],
'crop' => $sources['crop'],
'ratio' => $this->ratio($sources['crop']),
];
}

return $srcSets;
}

public function defaultSrc()
{
$defaultWidth = $this->defaultWidth();
Expand All @@ -154,16 +147,12 @@ public function defaultSrc()
);
}

public function sizesAttr()
{
return $this->preset['sizes'] ?? null;
}

public function lqip()
{
$sources = [];

foreach ($this->defaultSources() as $source) {
foreach ($this->sources as $source) {
$crop = $source['crop'] ?? $this->crop;

$sources[] = [
Expand Down Expand Up @@ -195,13 +184,42 @@ public function lqip()
];
}

public function srcSets()
{
$srcSets = [];

foreach ($this->sources() ?? [] as $sources) {
$srcset = [];

foreach ($sources['sources'] as $source) {
if (isset($source['src']) && isset($source['width'])) {
$srcset[] = sprintf(
'%s %dw',
$source['src'],
$source['width'],
);
}
}

$srcSets[] = [
'srcset' => join(',', $srcset),
'type' => $sources['type'],
'mediaQuery' => $sources['mediaQuery'],
'crop' => $sources['crop'],
'ratio' => $this->ratio($sources['crop']),
];
}

return $srcSets;
}

protected function sources()
{
$sources = [];

// webp
if (config('twill-image.webp_support')) {
foreach ($this->defaultSources() as $source) {
foreach ($this->sources as $source) {
$sources[] = [
'mediaQuery' => $source['media_query'] ?? null,
'type' => self::TYPE_WEBP,
Expand All @@ -212,7 +230,7 @@ protected function sources()
}

// jpeg
foreach ($this->defaultSources() as $source) {
foreach ($this->sources as $source) {
$sources[] = [
'mediaQuery' => $source['media_query'] ?? null,
'type' => self::TYPE_JPEG,
Expand All @@ -224,31 +242,6 @@ protected function sources()
return $sources;
}

protected function defaultSources()
{
return $this->preset['sources'] ?? [
[
'crop' => $this->crop,
],
];
}

protected function defaultWidth()
{
return $this->preset['width'] ?? self::DEFAULT_WIDTH;
}

protected function defaultWidths()
{
$defaultWidth = $this->defaultWidth();

return array_filter(self::AUTO_WIDTHS, function ($width) use (
$defaultWidth
) {
return $width <= $defaultWidth * self::AUTO_WIDTHS_RATIO;
});
}

protected function imageSources($mediaQueryConfig, $sourceParams = [])
{
$widths = $mediaQueryConfig['widths'] ?? $this->defaultWidths();
Expand All @@ -271,12 +264,30 @@ protected function imageSources($mediaQueryConfig, $sourceParams = [])
return $sourcesList;
}

protected function defaultWidth()
{
return $this->preset['width'] ?? self::DEFAULT_WIDTH;
}

protected function defaultWidths()
{
$defaultWidth = $this->defaultWidth();

return array_filter(self::AUTO_WIDTHS, function ($width) use (
$defaultWidth
) {
return $width <= $defaultWidth * self::AUTO_WIDTHS_RATIO;
});
}



/**
* Build crops list
*
* @return array
*/
protected function crops(): array
protected function getAllCrops(): array
{
$role = $this->role;

Expand All @@ -294,6 +305,12 @@ protected function crops(): array
return $crops;
}

/**
* Set model crop
*
* @param null|string $crop
* @return void
*/
protected function setCrop($crop)
{
if (isset($crop)) {
Expand All @@ -306,7 +323,7 @@ protected function setCrop($crop)
return;
}

$crops = $this->crops();
$crops = $this->getAllCrops();

if ($index = array_search('default', $crops)) {
return $crops[$index];
Expand Down Expand Up @@ -356,6 +373,12 @@ private function mimeType($extension)
return null;
}

/**
* Set model
*
* @param object $model
* @return void
*/
protected function setModel($model)
{
if (!classHasTrait($model, HasMedias::class)) {
Expand Down Expand Up @@ -418,7 +441,7 @@ public function toArray()
'src' => $this->defaultSrc(),
'width' => $this->width(),
'height' => $this->height(),
'sizes' => $this->sizesAttr(),
'sizes' => $this->sizes,
'alt' => $this->alt(),
];
}
Expand Down

0 comments on commit 03e3d7f

Please sign in to comment.