簡體   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?

我有一些圖像存儲在 AWS S3 中,我使用 Laravel 中的存儲外觀在網頁上顯示它們。

在某些情況下,我想獲取一些圖像的尺寸並將這些尺寸存儲在數據庫中。

我想在 PHP 中使用getimagesize函數,但圖像不是公開可用的,只能通過 Storage 門面訪問它們。

如何獲得這些圖像的尺寸? 謝謝你。

編輯:我應該提到這個圖像處理都是為了數據庫遷移而完成的,所以使用像 JS 這樣的 Web 前端解決方案來獲取圖像尺寸是不切實際的。

我想出了一個似乎有效的解決方案。 基本上,我使用 Storage 外觀獲取文件的二進制文件,然后使用imagecreatefromstringimagesximagesy函數來獲取圖像的寬度和高度。

下面是一個例子:

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

您還可以使用開源庫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