Cache any image from any source locally in your Laravel app
- Install using Composer
composer require indent/imgcache --prefer-dist
- Create symbolic link between
public/
directory andstorage/imgcache/
php artisan imgcache:link
- Add your Cloudinary cloud name in
config/services.php
. You can find your cloud name in the Cloudinary dashboard.
return [
'imgcache' => [
'cloudinary' => [
'cloud_name' => env('CLOUDINARY_CLOUD_NAME', '<Your cloud name>'),
],
],
];
Use Imgcache by calling the fascade or use the global helper.
class ProductController
{
public function show()
{
$img = Imgcache::make('https://picsum.photos/id/1/100/100')->get();
return view('product.show', [
'img' => $img,
]);
}
}
The global helper is useful when rendering an image in HTML or Blade views
{{-- When called in a Blade view Imgcache will be stringified --}}
<img src="{{ imgcache($imageUrl) }} alt="...">
{{-- If you want to be explicit, you can call `get()` --}}
<img src="{{ imgcache($imageUrl)->get() }} alt="...">
You can generate very small image hashes to load inline in your Blade templates, then lazy load the actual image later.
<img src="{{ imgcache($imageUrl)->width(150)->blur(2000)->format('webp')->base64() }} alt="...">
Return base64 encoded image string
Apply blur effect to image
Apply brightness / darkness to image
Crops image into specified size
Return relative URL to image
Resize image to specified height with automatic width
Create new instance of Imgcache
Apply pixelate effect to image
Resize image to specified width with automatic height
Currently only Cloudinary is supported through their fetch API.