Skip to content
Closed
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
30 changes: 29 additions & 1 deletion src/Illuminate/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
use Illuminate\Contracts\Image\Driver;
use Illuminate\Contracts\Image\Transformation;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Http\UploadedFile;
use Illuminate\Image\Transformations\Blur;
use Illuminate\Image\Transformations\Contain;
Expand All @@ -21,13 +22,14 @@
use Illuminate\Image\Transformations\Rotate;
use Illuminate\Image\Transformations\Scale;
use Illuminate\Image\Transformations\Sharpen;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use Stringable;
use Throwable;

class Image implements Stringable
class Image implements Htmlable, Stringable
{
use Conditionable, Macroable;

Expand Down Expand Up @@ -413,6 +415,32 @@ public function toDataUri(): string
return 'data:'.$this->mimeType().';base64,'.$this->toBase64();
}

/**
* @param array<string, scalar|\Stringable|true>&string[] $attributes
*/
public function toHtml(array $attributes = []): string
{
$attributes = collect([
'src' => $this->toDataUri(),
'width' => $this->width(),
'height' => $this->height(),
])
->merge(Arr::mapWithKeys($attributes, function ($value, $key) {
if (is_int($key) && is_string($value)) {
return [$value => true];
}

if (is_string($key) && (is_scalar($value) || $value instanceof Stringable || $value === true)) {
return [$key => $value];
}

return [];
}))
->map(fn ($value, $key) => $value === true ? e($key) : sprintf('%s="%s"', e($key), e($value)));

return '<img '.$attributes->join(' ').'>';
}

/**
* Get the file extension based on the MIME type.
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/Image/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,39 @@ public function test_optimize_sets_both_format_and_quality()
$this->assertSame(90, $options->quality);
}

public function test_to_html()
{
$image = $this->makeImage();
$html = $image->toHtml();
$this->assertStringStartsWith('<img src="', $html);
$this->assertStringEndsWith('" width="100" height="100">', $html);

$result = $image->scale(400, 400);
$html = $image->toHtml();
$this->assertStringStartsWith('<img src="', $html);
$this->assertStringEndsWith('" width="100" height="100">', $html);
}

public function test_to_html_with_additional_attributes()
{
$image = $this->makeImage();

$result = $image->scale(400, 400);
$html = $image->toHtml(['alt' => 'My avatar', 'hidden']);
$this->assertStringStartsWith('<img src="', $html);
$this->assertStringEndsWith('" width="100" height="100" alt="My avatar" hidden>', $html);
}

public function test_to_html_attributes_are_overwritable()
{
$image = $this->makeImage();

$result = $image->scale(400, 400);
$html = $image->toHtml(['height' => 42]);
$this->assertStringStartsWith('<img src="', $html);
$this->assertStringEndsWith('" width="100" height="42">', $html);
}

public function test_optimize_allows_gif()
{
$result = $this->makeImage()->optimize('gif');
Expand Down