Skip to content

Commit 581ffc6

Browse files
committed
improve thumbnail transformer svg
1 parent e1f53c1 commit 581ffc6

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/Utils/Transformers/Attributes/Eloquent/SharpUploadModelThumbnailUrlTransformer.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Code16\Sharp\Exceptions\SharpException;
66
use Code16\Sharp\Form\Eloquent\Uploads\SharpUploadModel;
77
use Code16\Sharp\Utils\Transformers\SharpAttributeTransformer;
8+
use Illuminate\Support\Arr;
89

910
/**
1011
* Transforms a SharpUploadModel instance in a thumbnail URL.
@@ -39,18 +40,29 @@ public function renderAsImageTag(bool $renderAsImageTag = true): self
3940
*/
4041
public function apply($value, $instance = null, $attribute = null)
4142
{
42-
if (! $instance->$attribute) {
43+
$upload = $instance->$attribute;
44+
45+
if (! $upload) {
4346
return null;
4447
}
4548

46-
if (! $instance->$attribute instanceof SharpUploadModel) {
49+
if (! $upload instanceof SharpUploadModel) {
4750
throw new SharpException("[$attribute] must be an instance of SharpUploadModel");
4851
}
4952

50-
$url = $instance->$attribute->thumbnail($this->width, $this->height, $this->filters);
53+
$url = $upload->thumbnail($this->width, $this->height, $this->filters);
5154

5255
return $this->renderAsImageTag
53-
? sprintf('<img src="%s" alt="" class="img-fluid">', $url)
56+
? sprintf(
57+
'<img src="%s" alt="" style="object-fit: contain; %s">',
58+
$url,
59+
$upload->mime_type === 'image/svg+xml'
60+
? Arr::toCssStyles([
61+
"width: {$this->width}px" => $this->width,
62+
"height: {$this->height}px" => $this->height,
63+
])
64+
: ''
65+
)
5466
: $url;
5567
}
5668
}

0 commit comments

Comments
 (0)