Yii2 port of https://github.com/dlds/yii-gallery-manager
(frontend part mostly without changes, but backend was rewritten almost completely)
warning: preview version
- AJAX image upload
- Optional name and description for each image
- Possibility to arrange images in gallery
- Ability to generate few versions for each image with different configurations
- Drag & Drop
- Yii2
- Twitter bootstrap assets (version 3)
- Imagine library
- JQuery UI (included with Yii)
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist dlds/yii2-gallery-manager "*@dev"
or add
"dlds/yii2-gallery-manager": "*@dev"
to the require section of your composer.json
file.
Add migration to create table for images:
class m150318_154933_gallery_ext
extends dlds\galleryManager\migrations\m140930_003227_gallery_manager
{
}
Add GalleryBehavior to your model, and configure it, create folder for uploaded files.
public function behaviors()
{
return [
'galleryBehavior' => [
'class' => GalleryBehavior::className(),
'type' => 'product',
'extension' => 'jpg',
'directory' => Yii::getAlias('@contentRoot') . '/images/product/gallery',
'url' => Yii::getAlias('@web') . '/images/product/gallery',
'versions' => [
'small' => function ($img) {
/** @var ImageInterface $img */
return $img
->copy()
->thumbnail(new Box(200, 200));
},
'medium' => function ($img) {
/** @var ImageInterface $img */
$dstSize = $img->getSize();
$maxWidth = 800;
if ($dstSize->getWidth() > $maxWidth) {
$dstSize = $dstSize->widen($maxWidth);
}
return $img
->copy()
->resize($dstSize);
},
]
]
];
}
Add GalleryManagerAction in controller somewhere in your application. Also on this step you can add some security checks for this action.
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()
]
],
];
}
Add ImageAttachmentWidget somewhere in you application, for example in editing from.
if ($model->isNewRecord) {
echo 'Can not upload images for new record';
} else {
echo GalleryManager::widget(
[
'model' => $model,
'behaviorName' => 'galleryBehavior',
'apiRoute' => 'product/galleryApi'
]
);
}
Done!
Now, you can use uploaded images from gallery like following:
foreach($model->getBehavior('galleryBehavior')->getImages() as $image) {
echo Html::img($image->getUrl('medium'));
}