mercator/wn-queuedresize-plugin
Composer 安装命令:
composer require mercator/wn-queuedresize-plugin
包简介
Queued image and PDF resizing plugin for Winter CMS, providing a drop-in replacement for the resize filter with background processing and modern formats.
README 文档
README
This plugin provides an asynchronous and memory-efficient solution for handling image resizing in WinterCMS. By offloading resource-intensive image processing to the Laravel Queue, it prevents synchronous page loads from becoming slow and avoids server exhaustion, making it ideal for large media libraries and high-traffic environments.
Features
| Summary | Category | Feature | Benefit |
|---|---|---|---|
| Performance | Asynchronous Processing | User does not wait for image creation; page loads remain fast. | Instant feedback on the frontend. |
| Optimization | Local Disk Path Handling | Avoids unnecessary memory consumption by reading file paths instead of entire file content into PHP. | Lower RAM overhead per process. |
| Resilience | Memory-Efficient PDF Handling | Optimizes ImageMagick to process PDFs at low DPI and only the first page. | Prevents memory leaks and crashes. |
| Control | CLI Tools (warmup, clear, prune) |
Provides complete cache and maintenance control via the command line. | Automated maintenance workflows. |
| Stability | Concurrency Control | Uses a semaphore lock to cap simultaneous resize jobs. | Protects server CPU/RAM from overload. |
1. Installation and Configuration (.env)
The plugin uses a dedicated configuration file (config.php) which pulls its values from your main .env file. You should place these variables in your environment file to easily manage your setup across different deployments (staging, production).
.env Variables
| Variable | Default Value | Description | Used In |
|---|---|---|---|
IMAGE_RESIZE_QUEUE |
imaging |
The name of the queue the resize jobs will be dispatched to. | ProcessImageResize.php |
IMAGE_RESIZE_DRIVER |
gd |
The Intervention Image driver to use: gd or imagick. |
ImageResizer.php |
IMAGE_RESIZE_DISK |
local |
The default storage disk for reading source files and writing cache files. | All components |
IMAGE_RESIZE_QUALITY |
80 |
The fallback JPEG/WebP output quality (0-100). | ImageResizer.php |
IMAGE_RESIZE_CONCURRENCY |
3 |
(Critical) The max simultaneous resize jobs that can run at once. | ProcessImageResize.php |
IMAGE_RESIZE_BACKOFF |
5 |
Seconds a job waits before re-release if the concurrency limit is reached. | ProcessImageResize.php |
Starting Queue Workers
The plugin is useless without a running queue worker. You should use a process manager (like Supervisor) to ensure this command runs continuously:
# Start a worker process dedicated to the 'imaging' queue
php artisan queue:work --queue=imaging --tries=3 --timeout=3600
2. Twig Filter: qresize - Detailed Parameters
The qresize filter is the primary tool for using the plugin in your templates. It instantly returns a URL, deferring the actual work to the background.
Twig Syntax
{{ image.path | qresize(width, height, options) }}
Parameters
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
src |
string |
Yes | The source path to the original image file. | image.path |
width |
int/null |
No | Desired output width. Required if height is null. |
400 |
height |
int/null |
No | Desired output height. Required if width is null. |
300 |
options |
array |
No | Key/value pairs to control the resizing process. | {'mode': 'crop', 'quality': 90} |
Options Array Keys (opts)
| Key | Type | Description | Default |
|---|---|---|---|
mode |
string |
Resizing mode: auto, crop, or fit. |
'auto' |
quality |
int |
Output quality for JPEG/WebP (0-100). | 80 |
format |
string |
jpg, webp, png, gif, or best (client-aware WebP). |
'best' |
disk |
string |
Overrides the default disk for this specific request. | 'local' |
3. PHP Block Functionality: queuedresize_path()
This function is registered to allow developers to trigger asynchronous directory-based resizing directly from custom PHP code (e.g., in a component's onStart() method or a CMS page's code block).
Function Call Syntax
queuedresize_path($path, $w, $h, $opts = [], $recursive = false, $disk = null);
Function Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$path |
string |
Yes | Path to a directory or file. Resolves to parent folder if a file. |
$w |
int/null |
No | The desired output width in pixels. |
$h |
int/null |
No | The desired output height in pixels. |
$opts |
array |
No | Array of options (mode, quality, etc.) passed to the job. |
$recursive |
bool |
No | Set to true to scan subdirectories within the resolved path. |
$disk |
string/null |
No | Overrides the default disk to use for this batch job. |
Example Usage in a Component:
function onStart() { // When the component initializes, it queues all images in 'media/user-uploads' // to be resized to 1024px wide. $jobsQueued = queuedresize_path( 'user-uploads/latest-image.jpg', // Path resolves to the 'user-uploads' directory 1024, null, ['quality' => 85, 'format' => 'webp'], true, // Recursive 'media' // Uses the 'media' disk ); }
4. Command Line Interface (CLI) - All Parameters
A. queuedresize:warmup (Batch Processing)
Recursively generates resized images for a directory.
| Parameter | Alias | Required | Description | Example Value |
|---|---|---|---|---|
path |
(arg) | Yes | Relative folder path to scan (inside storage/app/media). |
"media/gallery" |
--width |
No | Target width(s) (comma-separated). | 400,800 |
|
--height |
No | Target height(s) (comma-separated). | 200 |
|
--format |
No | Output formats (comma-separated). | jpg,webp |
|
--recursive |
-r |
No | Scans subdirectories within the specified path. |
(flag only) |
--force |
-f |
No | Forces regeneration even if cache is valid. | (flag only) |
--disk |
No | The specific storage disk to operate on. | uploads |
|
--mode |
No | Resize mode (auto, crop, fit). | crop |
|
--quality |
No | Output quality (0-100). | 90 |
Example Usage:
php -d memory_limit=1024M artisan queuedresize:warmup "media/gallery" --width=400,800 --format=jpg,webp --recursive --force
B. queuedresize:clear (Cache Cleanup)
Removes resized images based on filter criteria.
| Parameter | Required | Description | Example Value |
|---|---|---|---|
--disk |
No | The storage disk to clear the cache from. | local |
--width |
No | Clear only images matching this target width. | 400 |
--height |
No | Clear only images matching this target height. | 300 |
--path |
No | Filter by segment of the original source file path. | products/banners |
--dry-run |
No | Displays matching files without deleting them. | (flag only) |
C. queuedresize:prune (Orphan Removal)
Removes cached resized images where the original source file no longer exists.
| Parameter | Required | Description | Example Value |
|---|---|---|---|
--disk |
No | The storage disk to prune the cache on. | media |
--dry-run |
No | Displays which orphans would be deleted. | (flag only) |
Example Usage:
# Delete all orphan files on the default disk
php artisan queuedresize:prune
Would you like me to help you set up a Supervisor configuration file to manage your queue workers?
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-10