Reputation: 119
In this 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
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
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
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
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
Reputation: 41
You can use
<?php
$imagedetails = getimagesize($_FILES['file-vi']['tmp_name']);
$width = $imagedetails[0];
$height = $imagedetails[1];
?>
Upvotes: 2
Reputation: 4040
$data = getimagesize($filename);
$width = $data[0];
$height = $data[1];
Upvotes: 34