简体   繁体   中英

How do I get the dimensions of an image in AWS S3 that I'm accessing with the Storage facade in Laravel?

I have some images stored in AWS S3, and I'm using the Storage facade in Laravel to display them on a web page.

In a few cases, I want to get the dimensions of some images and store those dimensions in a DB.

I was thinking of using the getimagesize function in PHP, but the images are not publicly available, and they can only be accessed via the Storage facade.

How can I get the dimensions of these images? Thank you.

Edit : I should mention that this image processing is all being done for a DB migration, so using a web front-end solution like JS to get the image dimensions is not practical.

I came up with a solution that seems to work. Basically, I get the binary of the file with the Storage facade, then use the imagecreatefromstring , imagesx and imagesy functions to get the width and height of the image.

Here's an example:

$contents = Storage::get($storageFilePath);
$im = imagecreatefromstring($contents);
$width = imagesx($im) ?? null;
$height = imagesy($im) ?? null;

You could also use the open source libary Intervention\\Image , which uses GD Library and Imagick:

$image = \Image::make(Storage::get($storageFilePath));
$width = $image->width();
$height = $image->height();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM