2

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!

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43
Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91

2 Answers2

2

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

jangaraev
  • 21
  • 4
1

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.

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43