这是我的场景:我有一个Laravel6.x应用程序,我使用FlySystemAzureBlobStorage包在Azure上存储图像。然后,我想使用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存储中下载映像,然后系统才能检查它是否存在大小调整的缓存版本。
我使用的Azure包没有提供获取图像路径的可能性,因此我无法找到解决问题的另一种解决方案。
那么,是否有一种方法可以实现图像缓存,而无需每次下载文件?
发布于 2020-12-04 10:38:50
我使用单独的路径在刀片中显示/缓存图像。我将图像存储为product_id标识的MYSQL产品表中的BLOB
刀片:
<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;
}
}这使得浏览器缓存图像。
https://stackoverflow.com/questions/62445113
复制相似问题