elisevgeniy / yii2-gallery-manager
yii的扩展,允许管理图片画廊
1.2.2
2020-05-19 18:43 UTC
Requires
This package is auto-updated.
Last update: 2024-09-20 04:14:01 UTC
README
Yii2版本的https://github.com/zxbodya/yii-gallery-manager
(前端部分基本未变,但后端几乎全部重写)
画廊管理器截图(yii 1.x版本,新版本具有bootstrap 3样式)
特性
- AJAX图片上传
- 每张图片可选的名称和描述
- 在画廊中排列图片的可能性
- 为每张图片生成具有不同配置的几个版本的能力
- 拖放
- 旋转
优雅
- Yii2
- Twitter bootstrap资源(版本3)
- Imagine库
- JQuery UI(包含在Yii中)
安装
安装此扩展的首选方式是通过composer。
运行以下命令之一:
php composer.phar require --prefer-dist elisevgeniy/yii2-gallery-manager "*@dev"
或在您的composer.json
文件的require部分中添加以下内容:
"elisevgeniy/yii2-gallery-manager": "*@dev"
使用
准备
添加迁移以创建图像表
class m150318_154933_gallery_ext extends zxbodya\yii2\galleryManager\migrations\m140930_003227_gallery_manager { }
或者更好 - 将迁移复制到您的应用程序中(但请确保从其中移除命名空间 - 它应该在全局命名空间中)
添加上传和存储图像的配置
将GalleryBehavior添加到您的模型中,并对其进行配置,创建上传文件的文件夹。
use zxbodya\yii2\galleryManager\GalleryBehavior; class Product extends \yii\db\ActiveRecord { ... public function behaviors() { return [ 'galleryBehavior' => [ 'class' => GalleryBehavior::className(), 'type' => 'product', 'extension' => 'jpg', 'directory' => Yii::getAlias('@webroot') . '/images/product/gallery', 'url' => Yii::getAlias('@web') . '/images/product/gallery', 'versions' => [ 'small' => function ($img) { /** @var \Imagine\Image\ImageInterface $img */ return $img ->copy() ->thumbnail(new \Imagine\Image\Box(200, 200)); }, 'medium' => function ($img) { /** @var \Imagine\Image\ImageInterface $img */ $dstSize = $img->getSize(); $maxWidth = 800; if ($dstSize->getWidth() > $maxWidth) { $dstSize = $dstSize->widen($maxWidth); } return $img ->copy() ->resize($dstSize); }, ] ] ]; }
请参阅imagine的文档以获取图像转换的说明。
在您的应用程序的控制器中添加GalleryManagerAction。在这一步中,您还可以为此操作添加一些安全检查。
use zxbodya\yii2\galleryManager\GalleryManagerAction; class ProductController extends Controller { ... public function actions() { return [ 'galleryApi' => [ 'class' => GalleryManagerAction::className(), // mappings between type names and model classes (should be the same as in behaviour) 'types' => [ 'product' => Product::className() ] ], ]; }
在您的应用程序中添加ImageAttachmentWidget,例如在编辑时。
use zxbodya\yii2\galleryManager\GalleryManager; /* @var $this yii\web\View */ /* @var $model Product */ ?> ... <?php if ($model->isNewRecord) { echo 'Can not upload images for new record'; } else { echo GalleryManager::widget( [ 'model' => $model, 'behaviorName' => 'galleryBehavior', 'apiRoute' => 'product/galleryApi' ] ); } ?>
完成!
获取上传的图片
现在,您可以使用以下方式从画廊中使用上传的图片:
foreach($model->getBehavior('galleryBehavior')->getImages() as $image) { echo Html::img($image->getUrl('medium')); }
选项
使用非默认的画廊图像表名(默认为{{%gallery_image}}
)
- 添加创建所需表的迁移
- 在行为配置中更改
tableName
属性