Skip to content

Commit 832b191

Browse files
Merge pull request #8 from fuelviews/patch-0.0.3
Chore: Refactor DX, add support for attributes, add basic defaults.
2 parents bca21c1 + 07c39f0 commit 832b191

File tree

4 files changed

+26
-42
lines changed

4 files changed

+26
-42
lines changed

README.md

+8-13
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,17 @@ php artisan vendor:publish --provider="Fuelviews\ResponsiveImages\ResponsiveImag
3636

3737
## Basic usage
3838

39-
Process images using [Process Responsive Images Script](https://github.com/fuelviews/responsiveimages)
40-
41-
Copy processed images into the public/images/ directory
42-
43-
Do not forget double and single quotes `"'$value'"`
44-
4539
```
4640
<x-fuelviews::responsive-images
47-
:imageName="'logo'"
48-
:imageExt="'png'"
49-
:imageWidth="'350'"
50-
:imageHeight="'167'"
51-
:loading="''"
52-
:altText="''"
53-
:class="''"
41+
image="logo.png"
42+
class="h-96 w-auto"
5443
/>
5544
```
5645

46+
or
47+
48+
```
49+
<x-fuelviews::responsive-images image="logo.png" class="h-96 w-auto" />
50+
```
51+
5752
### [Fuelviews](https://feulviews.com)
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<img
2-
src="{{ $imgsrc }}"
3-
srcset="{{ $srcset }}"
4-
alt="{{ $altText }}"
5-
class="{{ $class }}"
6-
width="{{ $imageWidth }}"
7-
height="{{ $imageHeight }}"
8-
loading="{{ $loading }}"
2+
src="{{ $imgsrc }}"
3+
srcset="{{ $srcset }}"
4+
{{ $attributes->merge([
5+
'alt' => $altText,
6+
'loading' => $loading,
7+
'width' => $imageWidth,
8+
'height' => $imageHeight
9+
]) }}
910
>
1011
{{ $slot }}

resources/views/welcome.blade.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
<x-fuelviews::responsive-images
2-
:imageName="'logo'"
3-
:imageExt="'png'"
4-
:imageWidth="'350'"
5-
:imageHeight="'167'"
6-
:loading="''"
7-
:altText="''"
8-
:class="''"
9-
/>
1+
<x-fuelviews::responsive-images image="logo.png" class="h-96 w-auto" />

src/View/Components/ResponsiveImages.php

+9-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ResponsiveImages extends Component
1010
{
1111
public $imageName;
1212

13-
public $imageExt;
13+
private $imageExt;
1414

1515
public $imageWidth;
1616

@@ -27,21 +27,18 @@ class ResponsiveImages extends Component
2727
private $imagePath;
2828

2929
public function __construct(
30-
$imageName,
31-
$imageExt,
32-
$imageWidth,
33-
$imageHeight,
34-
$altText,
30+
$image,
31+
$imageWidth = '300',
32+
$imageHeight = '200',
33+
$altText = 'Alt text',
3534
$loading = 'lazy',
36-
$class = 'w-full'
3735
) {
38-
$this->imageName = $imageName;
39-
$this->imageExt = $imageExt;
36+
$this->imageName = $image;
37+
$this->imageExt = pathinfo($this->imagePath . $this->imageName, PATHINFO_EXTENSION);
4038
$this->imageWidth = $imageWidth;
4139
$this->imageHeight = $imageHeight;
4240
$this->altText = $altText;
4341
$this->loading = $loading;
44-
$this->class = $class;
4542
$this->imagePath = config('responsive-images.image_path');
4643
$this->dimensions = config('responsive-images.dimensions');
4744
}
@@ -51,18 +48,17 @@ public function render(): View|Closure|string
5148
$imageSet = [];
5249

5350
foreach ($this->dimensions as $dimension) {
54-
$src = asset($this->imagePath.$this->imageName.'-'.$dimension[0].'.'.'webp');
51+
$src = asset($this->imagePath . pathinfo($this->imageName, PATHINFO_FILENAME) . '-' . $dimension[0] . '.webp');
5552
$descriptor = $dimension[1].'w';
5653
$imageSet[] = "{$src} {$descriptor}";
5754
}
5855

5956
$srcset = implode(', ', $imageSet);
6057

6158
return view('fuelviews::responsive-images', [
62-
'imgsrc' => asset($this->imagePath.$this->imageName.'-'.$this->dimensions[1][0].'.'.$this->imageExt),
59+
'imgsrc' => asset($this->imagePath . pathinfo($this->imageName, PATHINFO_FILENAME) . '-' . $this->dimensions[1][0] . '.' . $this->imageExt),
6360
'srcset' => $srcset,
6461
'altText' => $this->altText,
65-
'class' => $this->class,
6662
'imageWidth' => $this->imageWidth,
6763
'imageHeight' => $this->imageHeight,
6864
'loading' => $this->loading,

0 commit comments

Comments
 (0)