thegrommet/image-resizer
Composer 安装命令:
composer require thegrommet/image-resizer
包简介
A PHP library for resizing images
README 文档
README
A PHP library for resizing images
Build status
Usage
Resizing
$resizer = new \Grommet\ImageResizer\Resizer('/path/to/images', '/path/to/save'); $newPath = $resizer->resize('in.jpg', 'out.jpg', ['strategy' => 'fit', 'width' => 100]); // $newPath = '/path/to/save/out.jpg'
Generating URLs to assets
$urlGen = new \Grommet\ImageResizer\UrlGenerator('https://cdn.site.com/media'); $url = $urlGen->imageUrl('i/image.jpg', ['strategy' => 'fit', 'width' => 100]); // $url = 'https://cdn.site.com/media/fit_w-100/i/image.jpg'
Resize presets
$presets = [ 'small' => [ 'width' => 293, 'height' => 219 ], 'large' => [ 'strategy' = 'crop', 'width' => 500, 'height' => 500 ] ]; /* urls */ $urlGen = new \Grommet\ImageResizer\UrlGenerator('https://cdn.site.com/media', $presets); $url = $urlGen->imageUrl('i/image.jpg', ['size' => 'small']); // $url = 'https://cdn.site.com/media/fit_w-293_h-219/i/image.jpg' /* files */ $resizer = new \Grommet\ImageResizer\PresetResizer( '/path/to/images', '/path/to/save', $presets ); $newPath = $resizer->resize('image.jpg', 'large'); // $newPath = '/path/to/save/crop_w-500_h-500_m-c/image.jpg'
Resizing service
$urlResizer = new \Grommet\ImageResizer\UrlResizer( '/path/to/images', '/path/to/save', 'https://cdn.site.com/media' ); // incoming request for a resized image $url = 'https://cdn.site.com/media/fit_w-100/i/image.jpg'; $newPath = $urlResizer->resize($url); // $newPath = '/path/to/save/fit_w-100/i/image.jpg' header('Content-Type: image/jpeg'); $resource = imagecreatefromjpeg($newPath); imagejpeg($resource); // output to browser imagedestroy($resource);
Resize Strategies
| Strategy | Description |
|---|---|
exact |
Resize to exact width and height. Aspect ratio will not be maintained. |
fit |
Resize to fit the specified width and height, maintaining aspect ratio. |
fill |
Resize to fit the specified bounds while maintaining the aspect ratio. Image will be padded with empty space with color in background. |
crop |
Crop an image to the exact size specified. Optionally specify cropMode. |
optimize |
Save the image at the existing dimensions while reducing quality and file size. Optionally specify quality. |
Resize Adapters
By default, the resizer will use PHP's GD2 functions to resize the images. You may specify a different image resize adapter to and offload that work to a 3rd party.
Kraken.io
Use Kraken's image resizing service once you have an account and API access:
$resizer = new \Grommet\ImageResizer\Resizer( '/path/to/images', '/path/to/save', 'kraken', ['api-key', 'api-secret'] ); $newPath = $resizer->resize('in.jpg', 'out.jpg', ['strategy' => 'fit', 'w' => 100, 'h' => 50]); // $newPath = '/path/to/save/out.jpg'
Running tests
composer test
Code style & fix
# sniff src folder composer fmt-check # fix src folder composer fmt
thegrommet/image-resizer 适用场景与选型建议
thegrommet/image-resizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.43k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 thegrommet/image-resizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thegrommet/image-resizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 12.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-30