phplaravellaravel-medialibrary

Get Image Width Spatie Libary for Laravel


I would like to have the image width output so that I can use certain classes at a certain width.

With the current code snippet it throws me an error: Unable to init from given url

Can someone help me?

@foreach($imgs as $img)
                @php

                        $imageInstance = Spatie\Image\Image::load($img->getFullUrl());
                        $imageWidth = $imageInstance->getWidth();

                @endphp
            <div id="grid-item" class="grid-item grid-item--width2"><img src="{{$img->getFullUrl()}}" class="img-fluid " {{$imageWidth}}></div>
            @endforeach

Solution

  • @foreach($imgs as $img)
                @php
    
                        $imageInstance = Spatie\Image\Image::load($img->getPath());
                        $imageWidth = $imageInstance->getWidth();
    
                @endphp
            <div id="grid-item" class="grid-item grid-item--width2"><img src="{{$img->getFullUrl()}}" class="img-fluid " {{$imageWidth}}></div>
            @endforeach