gwa / gw-image-editor
简单的PHP图像处理库
v3.3.2
2016-04-26 07:16 UTC
Requires
- php: >=5.4
- ext-exif: *
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^4.8
- squizlabs/php_codesniffer: ^2.3
README
使用内置GD库的PHP图像编辑库。
提供一个简单直观的界面,用于在现有图像上执行典型的编辑、裁剪、调整大小和过滤操作。
典型用例
- 根据约束缩放和裁剪上传的图像
- 创建不同尺寸的图像版本
- 添加水印
安装
通过composer安装
composer require gwa/gw-image-editor
使用方法
use Gwa\Image\ImageEditor; // create an editor using an existing image $editor = new ImageEditor('path/to/image.png'); // resize image to within 640 x 480 px, maintaining aspect ratio $editor->resizeToWithin(640, 480) ->save(); // resize image to exactly 640 x 480 px, cropping any overhang $editor->resizeTo(640, 480) ->save(); // resize image to exactly 640px width, preserving aspect ratio $editor->resizeTo(640) ->save(); // resize image to exactly 480px height, preserving aspect ratio $editor->resizeTo(null, 480) ->save(); // crop the image: x, y, width, height $editor->crop(10, 10, 100, 100) ->save(); // methods can be chained // - create and save medium image with max width and height of 500px // - create and save thumbnail image with exact dimensions of 100px x 100px $editor = new ImageEditor('path/to/original.jpg'); $editor->resizeToWithin(500, 500) ->saveAs('path/to/medium.jpg') ->resizeTo(100, 100) ->saveAs('path/to/thumbnail.jpg') ->saveAs('path/to/thumbnail.png', IMAGETYPE_PNG); // another image can be "pasted" in (e.g. a watermark) // - paste watermark into bottom right corner $editor = new ImageEditor('path/to/original.jpg'); $iw = $editor->getWidth(); $ih = $editor->getHeight(); $watermark = new ImageEditor('path/to/watermark.png'); $ww = $watermark->getWidth(); $wh = $watermark->getHeight(); $padding = 10; $editor->pasteImage($watermark, $iw - $ww - $padding, $ih - $wh - $padding) ->saveAs('path/to/watermarked.jpg'); // there are also filter methods available $editor = new ImageEditor('path/to/original.jpg'); $editor->grayscale() ->coloroverlay(0, 0, 0, 50) // 50% black overlay ->colorize(255, 0, 0, 0) // rgba
测试
PHPUnit
$ ./vendor/bin/phpunit -c tests/phpunit.xml --coverage-html=tests/_report tests
代码风格(PSR-2)
$ ./vendor/bin/phpcs --standard=PSR2 src