2017 © Pedro Peláez
 

yii2-extension yii2-gallery-manager

Extension for yii, that allows to manage image galleries

image

zxbodya/yii2-gallery-manager

Extension for yii, that allows to manage image galleries

  • Wednesday, January 3, 2018
  • by zxbodya
  • Repository
  • 13 Watchers
  • 71 Stars
  • 12,230 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 49 Forks
  • 23 Open issues
  • 1 Versions
  • 6 % Grown

The README.md

Gallery Manager usage instructions

Yii2 port of https://github.com/zxbodya/yii-gallery-manager, (*1)

(frontend part mostly without changes, but backend was rewritten almost completely), (*2)

Gallery manager screenshots (yii 1.x version, new one has bootstrap 3 styles):, (*3)

GalleryManager images list, (*4)

Few more screenshots: drag & drop upload, editing image information, upload progress,, (*5)

Features

  1. AJAX image upload
  2. Optional name and description for each image
  3. Possibility to arrange images in gallery
  4. Ability to generate few versions for each image with different configurations
  5. Drag & Drop

Decencies

  1. Yii2
  2. Twitter bootstrap assets (version 3)
  3. Imagine library
  4. JQuery UI (included with Yii)

Installation:

The preferred way to install this extension is through composer., (*6)

Either run, (*7)

php composer.phar require --prefer-dist zxbodya/yii2-gallery-manager "*@dev", (*8)

or add, (*9)

"zxbodya/yii2-gallery-manager": "*@dev", (*10)

to the require section of your composer.json file., (*11)

Usage

Prepare

Add migration to create table for images:, (*12)

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), (*13)

Add configurations for upload and store images

Add GalleryBehavior to your model, and configure it, create folder for uploaded files., (*14)

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., (*15)

Add GalleryManagerAction in controller somewhere in your application. Also on this step you can add some security checks for this action., (*16)

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., (*17)

use zxbodya\yii2\galleryManager\GalleryManager;

/* @var $this yii\web\View */
/* @var $model Product */
?>
...
isNewRecord) {
    echo 'Can not upload images for new record';
} else {
    echo GalleryManager::widget(
        [
            'model' => $model,
            'behaviorName' => 'galleryBehavior',
            'apiRoute' => 'product/galleryApi'
        ]
    );
}
?>

Done!, (*18)

Get uploaded images

Now, you can use uploaded images from gallery like following:, (*19)

foreach($model->getBehavior('galleryBehavior')->getImages() as $image) {
    echo Html::img($image->getUrl('medium'));
}

Options

  1. Add migration that will create table you need
  2. Change tableName property in behavior configuration

The Versions

03/01 2018

dev-master

9999999-dev

Extension for yii, that allows to manage image galleries

  Sources   Download

MIT

The Requires

 

by Bogdan Savluk

extension yii2 gallery image