首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel Azure Blob存储缓存图像

问Laravel Azure Blob存储缓存图像
EN

Stack Overflow用户
提问于 2020-06-18 07:57:33
回答 1查看 497关注 0票数 0

这是我的场景:我有一个Laravel6.x应用程序,我使用FlySystemAzureBlobStorage包在Azure上存储图像。然后,我想使用InterventionImageCache包来获取和缓存图像,以便更快地下载不同大小的客户端。我已经这样做了:

代码语言:javascript
复制
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存储中下载映像,然后系统才能检查它是否存在大小调整的缓存版本。

我使用的Azure包没有提供获取图像路径的可能性,因此我无法找到解决问题的另一种解决方案。

那么,是否有一种方法可以实现图像缓存,而无需每次下载文件?

EN

回答 1

Stack Overflow用户

发布于 2020-12-04 10:38:50

我使用单独的路径在刀片中显示/缓存图像。我将图像存储为product_id标识的MYSQL产品表中的BLOB

刀片:

代码语言:javascript
复制
<img src="/dbimage/{{$product->id}}.png" >

路线:

代码语言:javascript
复制
Route::get('/dbimage/{id}',[ProductImageController::class, 'getImage']);

主计长:

代码语言:javascript
复制
class ProductImageController extends Controller
{

    public function getImage($prodcutImageID){
        $productID=explode(".",$prodcutImageID);
        $rendered_buffer= Product::all()->find($productID[0])->image;

        $response = Response::make($rendered_buffer);
        $response->header('Content-Type', 'image/png');
        $response->header('Cache-Control','max-age=2592000');
        return $response;
    }

}

这使得浏览器缓存图像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62445113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档