i want cache a image with intervention/imagecache package(downloaded from github). my problem: after cached image when time image was deleted on host or local cached image not accessible. it seems laravel dont read image from cache, then read from image url...how to solve it? my codes are:
<?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");
}
}
package config/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' => '',
];
i want cache image and image not cached
Create a config/imagecache.php looks like: https://github.com/Intervention/imagecache/blob/master/src/config/config.php
Name route in the config, such as:
An then use it:
If you want create a custom template, make sure it looks like https://github.com/Intervention/imagecache/blob/master/src/Intervention/Image/Templates/Large.php
And then config your custom template in templates array of the config/imagecache.php configuration file.