Using spatie/laravel-medialibrary
8 in Laravel 8 app I wonder if there is a way to get dimensions(width*height)
of uploaded image?
Thanks in advance!
Using spatie/laravel-medialibrary
8 in Laravel 8 app I wonder if there is a way to get dimensions(width*height)
of uploaded image?
Thanks in advance!
there is no such a feature from the box, but it's possible to get dimensions using the spatie/image component (which is a dependency) like this.
here is the working code from my project:
use Spatie\Image\Image;
class YourController extends Controller
{
public function store(Request $request, string $id)
{
$model = \App\Models\MyModel::find($id);
$image = Image::load($request->file->getPathname());
$width = $image->getWidth();
$height = $image->getHeight();
$media = $model->addMediaFromRequest('file')
->toMediaCollection($collectionName);
// ...
}
}
link to details: https://github.com/spatie/laravel-medialibrary/discussions/2682
I haven't tested the code.
Spatie MediaLibrary uses spatie Image package behind the scenes to generate responsive Images.
Since the image package has a option to get the Image Properties
So you can do Something like this.
$fullPathToImage = 'your-path-here';
$imageInstance = Spatie\MediaLibrary\Support\ImageFactory::load($fullPathToImage);
$imageWidth = $imageInstance->getWidth();
$imageHeight = $imageInstance->getHeight();
since I have't tested the code. But it will provide you some sort of starting point to solve the issue.