|
5 | 5 | use Code16\Sharp\Exceptions\SharpException; |
6 | 6 | use Code16\Sharp\Form\Eloquent\Uploads\SharpUploadModel; |
7 | 7 | use Code16\Sharp\Utils\Transformers\SharpAttributeTransformer; |
| 8 | +use Illuminate\Support\Arr; |
8 | 9 |
|
9 | 10 | /** |
10 | 11 | * Transforms a SharpUploadModel instance in a thumbnail URL. |
@@ -39,18 +40,29 @@ public function renderAsImageTag(bool $renderAsImageTag = true): self |
39 | 40 | */ |
40 | 41 | public function apply($value, $instance = null, $attribute = null) |
41 | 42 | { |
42 | | - if (! $instance->$attribute) { |
| 43 | + $upload = $instance->$attribute; |
| 44 | + |
| 45 | + if (! $upload) { |
43 | 46 | return null; |
44 | 47 | } |
45 | 48 |
|
46 | | - if (! $instance->$attribute instanceof SharpUploadModel) { |
| 49 | + if (! $upload instanceof SharpUploadModel) { |
47 | 50 | throw new SharpException("[$attribute] must be an instance of SharpUploadModel"); |
48 | 51 | } |
49 | 52 |
|
50 | | - $url = $instance->$attribute->thumbnail($this->width, $this->height, $this->filters); |
| 53 | + $url = $upload->thumbnail($this->width, $this->height, $this->filters); |
51 | 54 |
|
52 | 55 | 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 | + ) |
54 | 66 | : $url; |
55 | 67 | } |
56 | 68 | } |
0 commit comments