You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -733,6 +737,73 @@ This package comes with 3 formatters out of the box:
733
737
734
738
Feel free to implement your own formatter by extending `\Elegantly\Media\UrlFormatters\AbstractUrlFormatter`.
735
739
740
+
## Conversions Presets
741
+
742
+
### Image Placeholder
743
+
744
+
When loading images on the web, it’s common to show a low-resolution blurred placeholder first, then swap it with the full-resolution image once it’s ready. This improves perceived performance, reduces layout shifts, and gives a polished feel to your UI.
745
+
746
+
This package includes a ready-to-use preset for generating these placeholders in Base64-encoded format. The placeholder is tiny in size, quick to load, and designed to be displayed while the actual image is being fetched.
747
+
748
+
#### Defining the Placeholder Conversion
749
+
750
+
To create a placeholder, simply define a media conversion using the `MediaImagePlaceholderConverter`:
751
+
752
+
```php
753
+
namespace App\Models;
754
+
755
+
use Illuminate\Database\Eloquent\Model;
756
+
use Elegantly\Media\Concerns\HasMedia;
757
+
use Elegantly\Media\Contracts\InteractWithMedia;
758
+
use Elegantly\Media\MediaCollection;
759
+
use Elegantly\Media\MediaConversionDefinition;
760
+
use Elegantly\Media\Converters\MediaImagePlaceholderConverter;
761
+
762
+
class User extends Model implements InteractWithMedia
763
+
{
764
+
use HasMedia;
765
+
766
+
public function registerMediaCollections(): array;
767
+
{
768
+
return [
769
+
new MediaCollection(
770
+
name: 'avatar',
771
+
single: true,
772
+
conversions: [
773
+
new MediaConversionDefinition(
774
+
name: 'placeholder',
775
+
converter: fn ($media) => new MediaImagePlaceholderConverter($media),
776
+
),
777
+
]
778
+
)
779
+
];
780
+
}
781
+
}
782
+
```
783
+
784
+
#### Using the Placeholder in Your Views
785
+
786
+
If you’re using the built-in <x-media::img> Blade component, you can directly specify the placeholder conversion name:
787
+
788
+
```html
789
+
<x-media::img
790
+
:media="$user->getFirstMedia('avatar')"
791
+
placeholder="placeholder"
792
+
/>
793
+
```
794
+
795
+
This will automatically load the blurred placeholder as a background-image.
796
+
797
+
#### Retrieving the Base64 Value Directly
798
+
799
+
If you need to manually work with the placeholder (e.g., for API responses or inline styles), you can retrieve it like this:
0 commit comments