elgorm / yii2-uploadable-cropable-image
用于上传和裁剪图片的Yii2扩展
该包的官方仓库似乎已消失,因此该包已被冻结。
v2.2.4
2015-10-20 07:25 UTC
Requires
- bower-asset/jcrop: 0.9.*
- yiisoft/yii2: 2.0.*
- yiisoft/yii2-bootstrap: 2.*
- yiisoft/yii2-imagine: 2.*
README
用于上传和裁剪图片的Yii2扩展
安装
安装此扩展的首选方法是通过composer。
运行以下命令之一:
php composer.phar require --prefer-dist elgorm/yii2-uploadable-cropable-image "*"
或者
"elgorm/yii2-uploadable-cropable-image": "*"
将以下内容添加到你的composer.json
文件的require部分。
使用方法
扩展安装完成后,只需在代码中使用它即可
在你的模型中
public function behaviors() { return [ [ 'class' => \elgorm\image\Behavior::className(), 'savePathAlias' => '@web/images/', 'urlPrefix' => '/images/', 'crop' => true, 'attributes' => [ 'avatar' => [ 'savePathAlias' => '@web/images/avatars/', 'urlPrefix' => '/images/avatars/', 'width' => 100, 'height' => 100, ], 'logo' => [ 'crop' => false, 'thumbnails' => [ 'mini' => [ 'width' => 50, ], ], ], ], ], //other behaviors ]; }
使用规则来验证属性。
在你的视图文件中
echo $form->field($model, 'avatar')->widget('elgorm\image\Widget');
然后,在你的视图中
echo Html::img($model->getImageUrl('avatar')); echo Html::img($model->getImageUrl('logo', 'mini')); //get url of thumbnail named 'mini' for 'logo' attribute
如果你使用高级应用程序模板并且这种行为附加在后端模型中,那么在前端模型中添加特质
use \elgorm\image\GetImageUrlTrait
并且在前端模型中也使用getImageUrl()方法。