zxbodya / yii2-gallery-manager
Extension for yii, that allows to manage image galleries
Installs: 50 662
Dependents: 2
Suggesters: 0
Security: 0
Stars: 93
Watchers: 14
Forks: 61
Open Issues: 41
Type:yii2-extension
Requires
This package is not auto-updated.
Last update: 2024-11-09 17:09:09 UTC
README
Yii2 port of https://github.com/zxbodya/yii-gallery-manager
(frontend part mostly without changes, but backend was rewritten almost completely)
Gallery manager screenshots (yii 1.x version, new one has bootstrap 3 styles):
Few more screenshots: drag & drop upload, editing image information, upload progress,
Features
- 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
Decencies
- Yii2
- Twitter bootstrap assets (version 3)
- Imagine library
- JQuery UI (included with Yii)
Installation:
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist zxbodya/yii2-gallery-manager "*@dev"
or add
"zxbodya/yii2-gallery-manager": "*@dev"
to the require section of your composer.json
file.
Usage
Prepare
Add migration to create table for images:
class m150318_154933_gallery_ext extends zxbodya\yii2\galleryManager\migrations\m140930_003227_gallery_manager { }
Or better - copy migration to you application(but be sure to remove namespace from it - it should be in global namespace)
Add configurations for upload and store images
Add GalleryBehavior to your model, and configure it, create folder for uploaded files.
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); }, ] ] ]; }
See also documentations of imagine for image transformations.
Add GalleryManagerAction in controller somewhere in your application. Also on this step you can add some security checks for this action.
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() ] ], ]; }
Add ImageAttachmentWidget somewhere in you application, for example in editing from.
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' ] ); } ?>
Done!
Get uploaded images
Now, you can use uploaded images from gallery like following:
foreach($model->getBehavior('galleryBehavior')->getImages() as $image) { echo Html::img($image->getUrl('medium')); }
Options
Using non default table name for gallery images(default is {{%gallery_image}}
):
- Add migration that will create table you need
- Change
tableName
property in behavior configuration