pistol88 / yii2-gallery
模块允许使用小部件来上传图片
Requires
- php: >=5.4.0
- abeautifulsite/simpleimage: 2.*
- kartik-v/yii2-widget-fileinput: *
- yiisoft/yii2: *
- yiisoft/yii2-bootstrap: *
This package is auto-updated.
Last update: 2024-08-27 22:08:36 UTC
README
注意!
自2017年4月24日起,模块的开发在这里进行:dvizh/yii2-gallery。建议从Dvizh的仓库安装模块,那里有最新版本。
此模块是为了能够在管理后台快速上传图片,添加标题、描述、替代文本,并设置位置(值越大,图片在列表中的位置越靠前)。
安装
执行以下命令
php composer require pistol88/yii2-gallery "*"
或者添加到 composer.json 中
"pistol88/yii2-gallery": "*",
然后执行
php composer update
迁移
php yii migrate/up --migrationPath=@vendor/pistol88/yii2-gallery/migrations
连接和配置
在应用程序配置文件中添加模块 gallery
'modules' => [ 'gallery' => [ 'class' => 'pistol88\gallery\Module', 'imagesStorePath' => dirname(dirname(__DIR__)).'/frontend/web/images/store', //path to origin images 'imagesCachePath' => dirname(dirname(__DIR__)).'/frontend/web/images/cache', //path to resized copies 'graphicsLibrary' => 'GD', 'placeHolderPath' => '@webroot/images/placeHolder.png', ], //... ]
在需要附加上传图片的模型中添加行为
function behaviors() { return [ 'images' => [ 'class' => 'pistol88\gallery\behaviors\AttachImages', 'mode' => 'gallery', 'quality' => 60, 'galleryId' => 'picture' ], ]; }
*mode - 加载类型。gallery - 批量加载,single - 单个字段,如果需要压缩,则设置 quality (0 - 100),其中 0 - 最大压缩,100 - 最小压缩。galleryId - 相册标识符,如果类名冲突,请使用此标识符
使用
可以直接使用
$images = $model->getImages(); foreach($images as $img) { //retun url to full image echo $img->getUrl(); //return url to proportionally resized image by width echo $img->getUrl('300x'); //return url to proportionally resized image by height echo $img->getUrl('x300'); //return url to resized and cropped (center) image by width and height echo $img->getUrl('200x300'); //return alt text to image $img->alt //return title to image $img->title //return description image $img->description }
小部件
图片通过小部件上传。在模型的 CRUD 表单的 _form.php 中添加。小部件接收以下参数:model => 要绑定图片的模型,默认 null;previewSize => 上传图片的预览大小,默认 '140x140';label => 小部件标签,默认 '图片';fileInputPluginLoading => 是否在输入处显示加载进度指示器,默认 true;fileInputPluginOptions => 小部件属性数组 kartik/file/fileInput,默认 [];
请不要忘记 php<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
用于表单。
<?=\pistol88\gallery\widgets\Gallery::widget( [ 'model' => $model, 'previewSize' => '50x50', 'fileInputPluginLoading' => true, 'fileInputPluginOptions' => [] ] ); ?>