jalloulkhaled/yii2-cropper-bootstrap5
Composer 安装命令:
composer require jalloulkhaled/yii2-cropper-bootstrap5
包简介
Yii2 Bootstrap Cropper Input Widget using bootstrap 5
关键字:
README 文档
README
Yii2 Image Cropper InputWidget
Cropper.js - Bootstrap Cropper (recommended) (already included).
Features
- Crop
- Image Rotate
- Image Flip
- Image Zoom
- Coordinates
- Image Sizes Info
- Base64 Data
- Set Image.Url Directly
- Set Image.Src With Javascript
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist bilginnet/yii2-cropper "dev-master"
or add
"bilginnet/yii2-cropper": "dev-master"
to the require section of your composer.json file.
Usage
Let's add into config in your main-local config file before return[]
$baseUrl = str_replace('/backend/web', '', (new Request)->getBaseUrl()); $baseUrl = str_replace('/frontend/web', '', $baseUrl); Yii::setAlias('@uploadUrl', $baseUrl.'/uploads/'); Yii::setAlias('@uploadPath', realpath(dirname(__FILE__).'/../../uploads/')); // image file will upload in //root/uploads folder return [ .... ]
Let's add in your model file
public $_image public function rules() { return [ ['_image', 'safe'], ]; } public function beforeSave($insert) { if (is_string($this->_image) && strstr($this->_image, 'data:image')) { // creating image file as png $data = $this->_image; $data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data)); $fileName = time() . '-' . rand(100000, 999999) . '.png'; file_put_contents(Yii::getAlias('@uploadPath') . '/' . $fileName, $data); // deleting old image // $this->image is real attribute for filename in table // customize your code for your attribute if (!$this->isNewRecord && !empty($this->image)) { unlink(Yii::getAlias('@uploadPath/'.$this->image)); } // set new filename $this->image = $fileName; } return parent::beforeSave($insert); }
Advanced usage in _form file
echo $form->field($model, '_image')->widget(\bilginnet\cropper\Cropper::className(), [ /* * elements of this widget * * buttonId = #cropper-select-button-$uniqueId * previewId = #cropper-result-$uniqueId * modalId = #cropper-modal-$uniqueId * imageId = #cropper-image-$uniqueId * inputChangeUrlId = #cropper-url-change-input-$uniqueId * closeButtonId = #close-button-$uniqueId * cropButtonId = #crop-button-$uniqueId * browseInputId = #cropper-input-$uniqueId // fileinput in modal */ 'uniqueId' => 'image_cropper', // will create automaticaly if not set // you can set image url directly // you will see this image in the crop area if is set // default null 'imageUrl' => Yii::getAlias('@web/image.jpg'), 'cropperOptions' => [ 'width' => 100, // must be specified 'height' => 100, // must be specified // optional // url must be set in update action 'preview' => [ 'url' => '', // (!empty($model->image)) ? Yii::getAlias('@uploadUrl/'.$model->image) : null 'width' => 100, // must be specified // you can set as string '100%' 'height' => 100, // must be specified // you can set as string '100px' ], // optional // default following code // you can customize 'buttonCssClass' => 'btn btn-primary', // optional // defaults following code // you can customize 'icons' => [ 'browse' => '<i class="fa fa-image"></i>', 'crop' => '<i class="fa fa-crop"></i>', 'close' => '<i class="fa fa-crop"></i>', 'zoom-in' => '<i class="fa fa-search-plus"></i>', 'zoom-out' => '<i class="fa fa-search-minus"></i>', 'rotate-left' => '<i class="fa fa-rotate-left"></i>', 'rotate-right' => '<i class="fa fa-rotate-right"></i>', 'flip-horizontal' => '<i class="fa fa-arrows-h"></i>', 'flip-vertical' => '<i class="fa fa-arrows-v"></i>', 'move-left' => '<i class="fa fa-arrow-left"></i>', 'move-right' => '<i class="fa fa-arrow-right"></i>', 'move-up' => '<i class="fa fa-arrow-up"></i>', 'move-down' => '<i class="fa fa-arrow-down"></i>', ] ], // optional // defaults following code // you can customize 'label' => '$model->attribute->label', // optional // default following code // you can customize 'template' => '{button}{preview}', ]);
Simple usage in _form file
echo $form->field($model, '_image')->widget(\bilginnet\cropper\Cropper::className(), [ 'cropperOptions' => [ 'width' => 100, // must be specified 'height' => 100, // must be specified ] ]);
jsOptions[]
echo $form->field($model, '_image')->widget(\bilginnet\cropper\Cropper::className(), [ 'cropperOptions' => [ 'width' => 100, // must be specified 'height' => 100, // must be specified ], 'jsOptions' => [ 'pos' => View::POS_END, // default is POS_END if not specified 'onClick' => 'function(event){ // when click crop or close button // do something }' ], ]);
Notes
Don't forget to add this line into root in .htaccess file
RewriteRule ^uploads/(.*)$ uploads/$1 [L]
You can set crop image directly with javascript
Sample:
$('button').click(function() {
// #cropper-modal-$unique will show automatically when click the button
// you must set uniqueId on widget
$('#cropper-url-change-input-' + uniqueId).val('image.jpeg').trigger('change');
});
jalloulkhaled/yii2-cropper-bootstrap5 适用场景与选型建议
jalloulkhaled/yii2-cropper-bootstrap5 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 3.59k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「extension」 「widget」 「yii2」 「cropper」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jalloulkhaled/yii2-cropper-bootstrap5 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jalloulkhaled/yii2-cropper-bootstrap5 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jalloulkhaled/yii2-cropper-bootstrap5 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
Adds more BBCode
统计信息
- 总下载量: 3.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-05