7

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?

Zayn Ali
  • 4,765
  • 1
  • 30
  • 40
ThomasPham
  • 109
  • 1
  • 1
  • 3

7 Answers7

32
 $data = getimagesize($filename);
 $width = $data[0];
 $height = $data[1];
Kuldeep Mishra
  • 3,846
  • 1
  • 21
  • 26
14

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();
Daud khan
  • 2,413
  • 2
  • 14
  • 18
  • 1
    What is `Image`? Can you please add `use ...\Image;` – PiTheNumber Mar 14 '22 at 13:21
  • 2
    @PiTheNumber The `Image` being referred to is a library called Intervention Image, a very popular and preferred Image library - you can check it out here [Github](https://github.com/Intervention/image) and more documentation here [Intervention Docs](https://image.intervention.io/v2/introduction/installation) - you need to refer to the installation documentation to implement it properly, unfortunately one can't simply `use ...\Image` - but it's worth it! – Cyfer Jun 09 '22 at 07:43
5
[$width, $height] = getimagesize($filename);
Arash Younesi
  • 1,671
  • 1
  • 14
  • 23
3

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();
Benjamin
  • 133
  • 7
1

You can use

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

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

?>
Aan Faisal
  • 31
  • 1
  • 7
1

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.

Tarek Adam
  • 3,387
  • 3
  • 27
  • 52
0

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,
        ];
    }
sajadsholi
  • 173
  • 1
  • 3
  • 12