我想用intervention/imagecache包缓存一张图片(从github下载)。 我的问题: 在主机上删除时间图像或无法访问本地缓存图像时缓存图像之后。似乎 laravel 不从缓存中读取图像,然后从图像 url 中读取...如何解决? 我的代码是:
<?php
namespace App\Http\Services\Image;
use Illuminate\Support\Facades\Config;
use Intervention\Image\Facades\Image;
class ImageCacheService
{
public function cache($imagePath, $size = '')
{
//set image size
$imageSizes = Config::get('image.cache-image-sizes');
if (!isset($imageSizes[$size])) {
$size = Config::get('image.default-current-cache-image');
}
$width = $imageSizes[$size]['width'];
$height = $imageSizes[$size]['height'];
//cache image
if (file_exists($imagePath)) {
$img = Image::cache(function ($image) use ($imagePath, $width, $height) {
return $image->make($imagePath)->fit($width, $height);
}, Config::get('image.image-cache-life-time'), true);
return $img->response();
} else {
$img = Image::canvas($width, $height, '#cdcdcd')->text('تصویر یافت نشد-404', $width / 2, $height / 2, function ($font) {
$font->color('#333333');
$font->align('center');
$font->valign('center');
$font->file(public_path('admin-assets/fonts/IRANSans/IRANSansWeb.woff'));
$font->size(24);
});
return $img->response();
}
}
}
<?php
namespace App\Http\Controllers\Admin\Content;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Content\PostCategoryRequest;
use App\Http\Services\Image\ImageCacheService;
use App\Http\Services\Image\ImageService;
use App\Models\Content\PostCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class CategoryController extends Controller
{
public function create()
{
$imageCache = new ImageCacheService();
return $imageCache->cache('1.png');
return view("admin.content.category.create");
}
}
包配置/image.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Image Driver
|--------------------------------------------------------------------------
|
| Intervention Image supports "GD Library" and "Imagick" to process images
| internally. You may choose one of them according to your PHP
| configuration. By default PHP's "GD Library" implementation is used.
|
| Supported: "gd", "imagick"
|
*/
'driver' => 'gd',
//index size
'index-image-sizes' => [
'large' => [
'width' => 800,
'height' => 600
],
'medium' => [
'width' => 400,
'height' => 300
],
'small' => [
'width' => 80,
'height' => 60
],
],
'default-current-index-image' => 'medium',
//cache size
'cache-image-sizes' => [
'large' => [
'width' => 800,
'height' => 600
],
'medium' => [
'width' => 400,
'height' => 300
],
'small' => [
'width' => 80,
'height' => 60
],
],
'default-current-cache-image' => 'medium',
'image-cache-life-time' => 999999,
'image-not-found' => '',
];
我想要缓存图像和图像不缓存
Intervention/imagecache的src中有一个Intervention\Image\ImageCacheController类https://github.com/Intervention/imagecache/blob/master/src/Intervention/Image/ImageCacheController.php
您可以使用该控制器在 web.php 中定义路由,然后从缓存中获取图像,例如:
Route::get('image/{template}/{filename}', [ImageCacheController::class, 'getResponse'])->name('imageCache.response')
创建一个 config/imagecache.php 看起来像: https://github.com/Intervention/imagecache/blob/master/src/config/config.php
如果你想创建一个自定义模板,确保它看起来像https://github.com/Intervention/imagecache/blob/master/src/Intervention/Image/Templates/Large.php