Basics
The Img component simply displays a <img> tag with the ability to add attributes.
make(?string $src)make(?string $src)
use MoonShine\UI\Components\Img;Img::make('path_to_file);
use MoonShine\UI\Components\Img;Img::make('path_to_file);
<x-moonshine::img src="path_to_file" /><x-moonshine::img src="path_to_file" />
Additional attributes
alt(?string $alt)- sets the description for the image (attributealt),size(int $width, ?int $height)- sets the width and height of the image (attributeswidthиheight),width(int $width)- sets the width of the image separately (attributewidth),height(?string $height)- sets the image height separately (attributeheight),rounded()- sets the rounding of the image (appliesstyle="border-radius: 50%;"),eagerLoading()- loads the image immediately, regardless of whether it is currently in the visible viewing area (attributeloading="eager"),lazyLoading()- delayed image loading (attributeloading="lazy"),autoDecoding()- The browser will choose the preferred decoding mode (attributeloading="auto"),syncDecoding()- decode the image synchronously with the rest of the DOM elements (attributeloading="sync"),asyncDecoding()- decode the image after displaying the rest of the DOM elements (attributeloading="async"),srcset(array $sources)- sets additional image sources for different screen resolutions (attributesrcset).
Examples
Examples of using the srcset() method:
Img::make('logo.png')->srcset(['200w' => 'logo-200w.png','400w' => 'logo-400w.png',]);// <img src="logo.png" srcset="logo-200w.png 200w, logo-400w.png 400w">Img::make('logo.png')->srcset(['200w' => 'logo-200w.png','400w' => 'logo-400w.png',]);// <img src="logo.png" srcset="logo-200w.png 200w, logo-400w.png 400w">
Img::make('banner.jpg')->srcset(['2x' => 'banner-2x.jpg','4x' => 'banner-4x.jpg',]);// <img src="banner.jpg" srcset="banner-2x.jpg 2x, banner-4x.jpg 4x">Img::make('banner.jpg')->srcset(['2x' => 'banner-2x.jpg','4x' => 'banner-4x.jpg',]);// <img src="banner.jpg" srcset="banner-2x.jpg 2x, banner-4x.jpg 4x">