简体   繁体   English

如何获取我使用 Laravel 中的存储外观访问的 AWS S3 中图像的尺寸?

[英]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.我有一些图像存储在 AWS S3 中,我使用 Laravel 中的存储外观在网页上显示它们。

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.我想在 PHP 中使用getimagesize函数,但图像不是公开可用的,只能通过 Storage 门面访问它们。

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.编辑:我应该提到这个图像处理都是为了数据库迁移而完成的,所以使用像 JS 这样的 Web 前端解决方案来获取图像尺寸是不切实际的。

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.基本上,我使用 Storage 外观获取文件的二进制文件,然后使用imagecreatefromstringimagesximagesy函数来获取图像的宽度和高度。

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:您还可以使用开源库Intervention\\Image ,它使用 GD 库和 Imagick:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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