stkevich / yii2-cropper
YII2 图像裁剪器
dev-master
2018-10-24 16:17 UTC
Requires
This package is auto-updated.
Last update: 2019-08-24 19:33:49 UTC
README
YII2 图像裁剪器。基于 imgAreaSelect jQuery 插件的扩展。更多详细信息请访问作者网站 http://odyniec.net/projects/imgareaselect/usage.html
安装
安装此扩展的首选方式是通过 composer。
运行
composer require --prefer-dist stkevich/yii2-cropper "*"
或添加
"stkevich/yii2-cropper": "*"
到您的 composer.json
文件的 require 部分中。
前端使用
一旦安装了此扩展,只需在您的代码中简单使用即可
use \stkevich\cropper\Cropper; ... $form->field($modelName, 'fieldName')->widget(Cropper::className(), []);
您可以为小部件添加一些选项
$form->field($modelName, 'fieldName')->widget(Cropper::className(), [ 'aspectRatio' => '1', //A string of the form "width:height" which represents the aspect ratio to maintain. Default = 1. 'maxHeight' => '200', //Maximum selection height (in pixels). Default = 9999. 'maxWidth' => '200', //Maximum selection width (in pixels). Default = 9999. 'minHeight' => '200', //Minimum selection height (in pixels). Default = 0. 'minWidth' => '200', //Minimum selection width (in pixels). Default = 0. 'resizable' => 'true', //If set to true, the plugin is completely removed. Default = true. ] );
后端使用
public function behaviors() { return [ 'imageCropper' => [ 'class' => ImageBehavior::class, 'attributeName' => 'image', // Attribute to work in behavior 'removeOldImage' => true, // Remove old image when record is update 'path' => '/upload/product/', // Path to save final image 'savingImages' => [ 'thumbnail' => [ 'fieldName' => 'image', 'method' => function ($img) { /** @var ImageInterface $img */ $cropInfo = CropFactory::getInfo('image'); return $img ->copy() ->crop( new Point($cropInfo->getX(), $cropInfo->getY()), new Box($cropInfo->getWidth(), $cropInfo->getHeight()) )->resize(new Box(235, 300)); } ], 'big' => [ 'fieldName' => 'imageLarge', 'method' => function ($img) { /** @var ImageInterface $img */ $dstSize = $img->getSize(); $maxWidth = 1200; if ($dstSize->getWidth() > $maxWidth) { $dstSize = $dstSize->widen($maxWidth); } return $img ->copy() ->resize($dstSize); } ] ], 'imageFileName' => function($extension, $version) { return $version . '_' . substr(md5(time()), 0, 10) . '.' . $extension; } ], }