【发布时间】:2020-10-08 05:21:24
【问题描述】:
这是我的场景: 我有一个 Laravel 6.x 应用程序,我正在使用 FlySystemAzureBlobStorage 包将图像存储在 Azure Blob 存储上。 然后我想使用InterventionImageCache 包来获取和缓存图像,以便更快地下载不同大小的客户端。 我已经这样做了:
public static function getImageResponseForApi($storageDiskName, $imagePath, $width = null, $height = null)
{
//check if the image exists on disk
$exists = empty($storageDiskName) ?
Storage::exists($imagePath) :
Storage::disk($storageDiskName)->exists($imagePath);
if ($exists) {
$image = empty($storageDiskName) ?
Storage::get($imagePath) :
Storage::disk($storageDiskName)->get($imagePath);
if (!empty($width) || !empty($height)) {
//use the image cache function to get the cached image if exists
$image = \Image::cache(function ($ima) use ($image, $width, $height) {
//check if height and with are higher than original or not
$ima->make($image)->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
});
}
$sizes = getimagesizefromstring($image);
$headers = [
'Content-Length' => strlen($image),
'Content-Type' => $sizes["mime"]
];
return \Response::make($image, 200, $headers);
} else {
return null;
}
}
以这种方式工作有一个问题:我必须先从 azure blob 存储下载图像,然后系统才能检查是否存在调整大小的缓存版本。
我正在使用的 Azure 包不提供获取图像路径的可能性,所以我找不到解决我的问题的其他方法。
那么,有没有一种方法可以实现图像缓存而无需我每次都下载文件?
【问题讨论】:
标签: php laravel image azure caching