geofftech / laravel-imagestyle
Installs: 231
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/geofftech/laravel-imagestyle
Requires
- php: *
- illuminate/console: ^11.0 || ^12.0
- illuminate/support: ^11.0 || ^12.0
- intervention/image: ^3.8
Requires (Dev)
- laravel/pint: ^1.21
README
- resizes and stores images such as thumbnails or the original
- resized images are stored in a dedicated 'cache' disk to separate from real data
- works by generating a URL to a resized file
- if that URL is viewed, it initially hits a route which generates and saves that resized image
- this uses the concept that real files will always be served before the routes are processed
Agent Instructions
This Laravel package provides image resizing and caching functionality. When working with this codebase:
Key Components
- ImageStyle.php: Main class that handles image processing and URL generation
- ImageStyleController.php: Controller that handles dynamic image generation requests
- ServiceProvider.php: Registers the package services and routes
- helpers.php: Contains the
img()helper function for easy access - Commands: Clean and purge commands for cache management
Helper Function Usage
Use the img() helper function to generate image style URLs:
// Basic usage img($imagePath)->thumbnail() img($imagePath)->resize(300, 200) // In Eloquent models public function thumbnailUrl(): Attribute { return Attribute::make( get: fn () => img($this->image)->thumbnail() ); }
Configuration
- Configuration file:
config/imagestyle.php - Cache disk must be configured in
config/filesystems.php - Routes are automatically registered via the ServiceProvider
Image Processing Flow
- Helper function generates URL pointing to cache location
- If cached file doesn't exist, route intercepts request
- Controller processes original image and saves to cache
- Subsequent requests serve the cached file directly
Development Notes
- Uses Intervention Image v3 for image processing
- Requires properly configured cache disk and storage links
- Images are stored in
/storage/app/cacheand served from/cacheURL
Uses Intervention Images
Sample Attribute
public function thumbnailUrl(): Attribute { return Attribute::make( get: fn () => img($this->image)->thumbnail() ); }
Setup
1. Setup Cache Disk
- create and link a new local disk
- edit
/config/filesystem.php - add a cache disk
'disks' => [ ... 'cache' => [ 'driver' => 'local', 'root' => storage_path('app/cache'), 'url' => env('APP_URL') . '/cache', 'visibility' => 'public', 'throw' => false, ], ],
2. Add a Storage link entry
'links' => [ public_path('storage') => storage_path('app/public'), public_path('cache') => storage_path('app/cache'), ],
3. Run Storage Link
a storage:link
- now files in the
/storage/app/cachefolder are accessible from the/cachefolder in the domain name