vladimir-alekseev-go / yii2-image-processor
为 Yii2 提供可配置的图像处理。
v1.1.2
2020-06-24 13:30 UTC
Requires
- imagine/imagine: 0.6.*
- yiisoft/yii2: ~2.0
Requires (Dev)
- phpunit/phpunit: 3.*
This package is auto-updated.
Last update: 2024-09-24 23:26:20 UTC
README
用于 Yii2 的应用程序组件。其主要用途是通过一系列 预配置 的转换处理图像。使用受 WideImage 的智能坐标 启发的简单 DSL 来增加配置的灵活性。本身不使用 WideImage 库,所有实际的图像处理都委托给 Imagine。
也支持临时处理,但如果重度和复杂的图像处理是您应用程序的关键功能,那么直接使用 Imagine 可能更好。
入门
通过 Composer 安装
"require": { "vladimir-alekseev-go/yii2-image-processor": "*" }
在您的应用程序配置的 'components' 部分中配置它
'imageProcessor' => [ 'class' => '\phtamas\yii2\imageprocessor\Component', // Default for all JPEG images 'jpegQuality' => 90, // Default for all PNG images 'pngCompression' => 7, // Create named image categories with their own configuration. // You can refer them by name in application code. 'define' => [ 'userAvatar' => [ // Add transformations. They will be applied in the order they were defined. 'process' => [ // Fix images with embedded orientation metadata ['autorotate'], // Preapre image to crop by resizing it to cover a 160*160 square ['resize', 'width' => 160, 'height' => 160, 'scaleTo' => 'cover'], // Crop it ['crop', 'x' => 'center - 80', 'y' => 'center - 80', 'width' => 160, 'height' => 160], ], ], 'galleryImage' => [ // Override default to save some disk space and bandwidth 'jpegQuality' => 80, 'process' => [ // Resize proportionally to fit a 600*600 square but only if too large ['resize', 'width' => 600, 'height' => 600, 'scaleTo' => 'fit', 'only' => 'down'], // Mark your property ['watermark', 'path' => '@path/to/wmark.png', 'align' => 'top-left', 'margin' => 20], ], ], ], ],
然后在您的应用程序的任何地方使用它
// Process uploaded image and save as a JPEG file $path = '@image/user/avatar/' . uniqid() . '.jpg'; Yii::$app->imageProcessor->save(['file' => $uploadedFile->tempName], $path, 'userAvatar'); // Process image (stored as BLOB in the DB) and send it to the HTTP client Yii::$app->imageProcessor->send(['data' => 'binary string'], 'jpg', 'galleryImage'); // Ad-hoc processing is possible, too Yii::$app->imageProcessor->saveAndSend(['file' => '@images/image.jpg'], $path, 'jpg', [ 'process' => [['resize', 'width' => 300]], // Resize proportionally to 300 px width ]);