Skip to content

Commit

Permalink
Merge pull request #18 from pboivin/feat/render-from-array
Browse files Browse the repository at this point in the history
feat: Initialize image and imageset render from arrays
  • Loading branch information
pboivin authored Mar 2, 2023
2 parents 7b1198f + 616210b commit 71ec476
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 10 deletions.
20 changes: 18 additions & 2 deletions src/ImageFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,30 @@ class ImageFile
{
protected $size;

public function __construct(
final public function __construct(
protected string $fileName,
protected string $path,
protected string $url,
protected ImageFileInspector $inspector
protected ?ImageFileInspector $inspector = null
) {
}

public static function fromArray(array $data): static
{
$obj = new static(
$data['fileName'] ?? '',
$data['path'] ?? '',
$data['url'] ?? '',
);

$obj->size = [
'width' => $data['width'] ?? 0,
'height' => $data['height'] ?? 0,
];

return $obj;
}

public function fileName(): string
{
return $this->fileName;
Expand Down
11 changes: 8 additions & 3 deletions src/ImageRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@

class ImageRender extends ImgRenderable
{
public function __construct(protected Image $image, protected array $config = [])
final public function __construct(protected array|Image $image, protected array $config = [])
{
if ($config) {
$this->acceptRenderConfig($config);
}
}

public static function fromArray(array $image, array $config = []): static
{
return new static($image, $config);
}

public function main(): ImageFile
{
return $this->image->source();
return $this->resolveImageFile($this->image);
}

public function lqip(): ImageFile
{
return $this->image->cached();
return $this->resolveImageFile($this->image, cached: true);
}

public function img(array $attributes = []): string
Expand Down
22 changes: 17 additions & 5 deletions src/ImageSetRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,36 @@ class ImageSetRender extends ImgRenderable
{
protected $data;

public function __construct(protected ImageSet $imageSet, protected array $config = [])
final public function __construct(protected array|ImageSet $imageSet, protected array $config = [])
{
$this->data = $this->imageSet->data();
if (is_array($imageSet)) {
$this->data = $imageSet;
} else {
$this->data = $this->imageSet->data();
}

if ($config) {
$this->acceptRenderConfig($config);
}
}

public static function fromArray(array $imageSet, array $config = []): static
{
return new static($imageSet, $config);
}

public function main(): ImageFile
{
$source = end($this->data['sources']);

$item = end($source['srcset']);

return $item['image']->cached();
return $this->resolveImageFile($item['image'], cached: true);
}

public function lqip(): ImageFile
{
return $this->data['lqip']->cached();
return $this->resolveImageFile($this->data['lqip'], cached: true);
}

public function picture(array $attributes = []): string
Expand Down Expand Up @@ -120,7 +129,10 @@ protected function getSrcset($source): string
$srcset = [];

foreach ($source['srcset'] as $item) {
$url = $item['image']->cached()->url();
$cached = $this->resolveImageFile($item['image'], cached: true);

$url = $cached->url();

$width = $item['width'];

$srcset[] = "{$url}" . ($includeWidth ? " {$width}w" : '');
Expand Down
11 changes: 11 additions & 0 deletions src/ImgRenderable.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,15 @@ protected function getPaddingTopValue(float $aspectRatio): string

return "{$padding}%";
}

protected function resolveImageFile(array|Image $item, bool $cached = false): ImageFile
{
$role = $cached ? 'cached' : 'source';

if (is_array($item)) {
return ImageFile::fromArray($item[$role]);
}

return $item->$role();
}
}
34 changes: 34 additions & 0 deletions tests/ImageRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,40 @@ public function test_can_override_noscript_attributes()
);
}

public function test_can_initialize_from_array()
{
$imageArray = [
"source" => [
"fileName" => "01.jpg",
"path" => "/path/to/images/source/01.jpg",
"url" => "/images/source/01.jpg",
"width" => 2932,
"height" => 2000,
"ratio" => 1.466,
],
"cached" => [
"fileName" => "01.jpg/test.gif",
"path" => "/path/to/images/cache/01.jpg/test.gif",
"url" => "/images/cache/01.jpg/test.gif",
"width" => 15,
"height" => 10,
"ratio" => 1.5,
],
];

$imageRender = ImageRender::fromArray($imageArray);

$output = $imageRender->img([
'class' => 'test',
'alt' => 'This is a test',
]);

$this->assertEquals(
'<img class="lazyload test" alt="This is a test" src="/images/cache/01.jpg/test.gif" data-src="/images/source/01.jpg" width="2932" height="2000">',
$output
);
}

protected function prepareImageRender($options = []): object
{
$prepared = $this->prepareImage();
Expand Down
66 changes: 66 additions & 0 deletions tests/ImageSetRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,64 @@ public function test_can_override_noscript_attributes()
);
}

public function test_can_initialize_from_array()
{
$imageSetArray = array(
'sources' => array(
0 => array(
'image' => '01.jpg',
'widths' => array(0 => 500, 1 => 900, 2 => 1300, 3 => 1700),
'srcset' => array(
0 => array(
'image' => array(
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
'cached' => array('fileName' => '01.jpg/one.jpg', 'path' => '/path/to/images/cache/01.jpg/one.jpg', 'url' => '/images/cache/01.jpg/one.jpg', 'width' => 500, 'height' => 341, 'ratio' => 1.466275659824047,),
),
'width' => 500,
),
1 => array(
'image' => array(
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
'cached' => array('fileName' => '01.jpg/two.jpg', 'path' => '/path/to/images/cache/01.jpg/two.jpg', 'url' => '/images/cache/01.jpg/two.jpg', 'width' => 900, 'height' => 614, 'ratio' => 1.4657980456026058,),
),
'width' => 900,
),
2 => array(
'image' => array(
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
'cached' => array('fileName' => '01.jpg/three.jpg', 'path' => '/path/to/images/cache/01.jpg/three.jpg', 'url' => '/images/cache/01.jpg/three.jpg', 'width' => 1300, 'height' => 887, 'ratio' => 1.4656144306651635,),
),
'width' => 1300,
),
3 => array(
'image' => array(
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
'cached' => array('fileName' => '01.jpg/four.jpg', 'path' => '/path/to/images/cache/01.jpg/four.jpg', 'url' => '/images/cache/01.jpg/four.jpg', 'width' => 1700, 'height' => 1160, 'ratio' => 1.4655172413793103,),
),
'width' => 1700,
),
),
),
),
'lqip' => array(
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
'cached' => array('fileName' => '01.jpg/lqip.gif', 'path' => '/path/to/images/cache/01.jpg/lqip.gif', 'url' => '/images/cache/01.jpg/lqip.gif', 'width' => 15, 'height' => 10, 'ratio' => 1.5,),
),
);

$imageRender = ImageSetRender::fromArray($imageSetArray);

$output = $imageRender->img([
'class' => 'test',
'alt' => 'This is a test',
]);

$this->assertEquals(
'<img class="lazyload test" alt="This is a test" src="/images/cache/01.jpg/lqip.gif" width="1700" height="1160" data-src="/images/cache/01.jpg/four.jpg" data-srcset="/images/cache/01.jpg/one.jpg 500w, /images/cache/01.jpg/two.jpg 900w, /images/cache/01.jpg/three.jpg 1300w, /images/cache/01.jpg/four.jpg 1700w" data-sizes="100vw">',
$output
);
}

protected function prepareAllImages(): array
{
$image1 = $this->prepareImage();
Expand Down Expand Up @@ -500,3 +558,11 @@ protected function prepareForPictureFormats($options = []): object
];
}
}



/*
*/

0 comments on commit 71ec476

Please sign in to comment.