【问题标题】:Laravel Azure Blob Storage Cache ImagesLaravel Azure Blob 存储缓存图像
【发布时间】: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


    【解决方案1】:

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

    刀片:

    <img src="/dbimage/{{$product->id}}.png" >
    

    路线:

    Route::get('/dbimage/{id}',[ProductImageController::class, 'getImage']);
    

    控制器:

    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;
        }
    
    }
    

    这会让浏览器缓存图片

    【讨论】:

      猜你喜欢
      • 2019-04-03
      • 2018-07-11
      • 2020-10-18
      • 2018-06-26
      • 2018-11-21
      • 2021-06-24
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多