ThomasPham
ThomasPham

Reputation: 119

How i get width and height of a image in Laravel Framework?

In this image,

image

I can get the width and height of the image in the directory.

But i want to get the width and height of a picture before i upload the image.

How can i achieve this?

Upvotes: 8

Views: 43242

Answers (7)

sajadsholi
sajadsholi

Reputation: 341

If you don't want to install intervention/image package use this function:


    /**
     * Get file dimension
     * @param  \Illuminate\Http\UploadedFile $file
     * @return array
     */
    public function getFileDimension(UploadedFile $file): array
    {
        $size = getimagesize($file->getRealPath());

        return [
            'width'     => $size[0] ?? 0,
            'height'    => $size[1] ?? 0,
        ];
    }

Upvotes: 1

Benjamin
Benjamin

Reputation: 173

run

composer require intervention/image

Then add this to your config/app.php

 return [
       ......
       $providers => [
          ......,
          'Intervention\Image\ImageServiceProvider'
       ],
       $aliases => [
          ......,
          'Image' => 'Intervention\Image\Facades\Image'
       ]
 ];

then use like this.

$upload_file = $request->file('gallery_image');
$height = Image::make($upload_file)->height();
$width = Image::make($upload_file)->width();

Upvotes: 6

Arash Younesi
Arash Younesi

Reputation: 1760

[$width, $height] = getimagesize($filename);

Upvotes: 5

Tarek Adam
Tarek Adam

Reputation: 3525

If you are using an s3 or some file system other than local, you can use getimagesize($url). The laravel Storage::disk('s3')->url($file_path) can provide what the url, however your s3 must be configured as public. Any url/route to the file will work.

Upvotes: 1

Daud khan
Daud khan

Reputation: 2509

Through Intervention Image you can do it as

$upload_file = $request->file('gallery_image');
$height = Image::make($upload_file)->height();
$width = Image::make($upload_file)->width();

Upvotes: 15

Aan Faisal
Aan Faisal

Reputation: 41

You can use

 <?php
$imagedetails = getimagesize($_FILES['file-vi']['tmp_name']);

$width = $imagedetails[0];
$height = $imagedetails[1];

?>

Upvotes: 2

Kuldeep Mishra
Kuldeep Mishra

Reputation: 4040

 $data = getimagesize($filename);
 $width = $data[0];
 $height = $data[1];

Upvotes: 34

Related Questions