lassehaslev / image
0.2.1
2017-04-28 12:34 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 5.4.*
This package is not auto-updated.
Last update: 2024-09-23 06:08:01 UTC
README
PHP图像处理库,提供简单易用的图像处理功能
动机
在网页上调整和裁剪图像非常麻烦,我希望有一个图像引擎能帮我完成这些操作。
这个包及其基础概念受到了Croppa的极大启发。
用法
我主要在我的Laravel项目中使用这个包。
在项目文件夹中运行 composer require lassehaslev/image
Laravel
如果你想在Laravel项目中使用这个包,我们将根据文件名自动裁剪和调整图像大小。
打开 config/app.php
并将 LasseHaslev\Image\Providers\LaravelServiceProvider::class
添加到 providers
数组中。
类
你可以原生地在所有PHP项目中使用这个包。
CropHandler
添加基础文件夹和裁剪文件夹,并从图像路径处理图像。
如果没有设置裁剪文件夹,我们将创建与原始文件夹相同的裁剪文件夹。
$baseFolder = '/image'; $cropsFolder = '/image/crops'; $handler = CropHandler::create( $baseFolder, $cropsFolder ); $this->handler ->handle( [ 'name'=>'test-image.jpg', 'width'=>89, 'height'=>89, 'resize'=>true, ] ) ->save( 'test-image-89x89-resize.jpg' );
适配器
你可以使用适配器来处理图像。
class Adaptor implements CropAdaptorInterface { public function transform( $input, $handler = null ) { return [ 'name'=>$input, 'width'=>300, 'height'=>200, 'resize'=>true, ]; } } $baseFolder = '/image'; $cropsFolder = '/image/crops'; $handler = CropHandler::create( $baseFolder, $cropsFolder, new Adaptor ); $this->handler ->handle( 'originalFilename.jpg' ) ->save( 'newFilename' );
ImageModifier
ImageModifier
是用于操作图像的基图像类。
use LasseHaslev\Image\Modifiers\ImageModifier; $modifier = ImageModifier::create( { absolute image path } ); // Crop image function $modifier->crop( $x1, $y1, $x2, $y2 ); // Crop image to width and height based on fucuspoint $modifier->cropToFit( $width, $height, $focusPointX = 0, $focusPointY = 0 ); // Resize width and height $modifier->resize( $width, $height ); // Save the new image $modifier->save( {absolutePath} ); // Example $modifier->cropToFit( 300, 300 ) ->save( '/path/to/image.jpg' );
ImageHandler
ImageHandler
用于处理图像和裁剪。它继承自 ImageModifier
。
use LasseHaslev\Image\Handlers\ImageHandler; $modifier = ImageHandler::create( $filepath ); // Remove the crops $modifier->removeCrops(); // Save $modifier->save( $pathOrFilename, $isFullPath = false );
开发
我的测试出现了问题,但我不确定原因。有时它通过了,有时没有。
# Prepare test images sh prepare.sh # Install php dependencies composer install # Install elixir dependencies npm install # run Test driven development through elixir gulp tdd
``
许可
MIT,狗狗