PHPackages                             wiperawa/yii2-gallery - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. wiperawa/yii2-gallery

ActiveYii2-extension[Image &amp; Media](/categories/media)

wiperawa/yii2-gallery
=====================

multiple Image galleries module

0341PHP

Since Jan 30Pushed 4y ago1 watchersCompare

[ Source](https://github.com/wiperawa/yii2-gallery)[ Packagist](https://packagist.org/packages/wiperawa/yii2-gallery)[ RSS](/packages/wiperawa-yii2-gallery/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (2)Used By (0)

Yii2-gallery
============

[](#yii2-gallery)

This module was done to allow you quickly upload pictures in admin panel or frontend member area, add image props like title, alt, as well as set position, and main image of the gallery. Features:

1. Ajax files upload
2. Server-side resizing
3. Client-side resizing
4. Resizing images by alias, with storing resied images in cache
5. related-model events calling on image insert, image delete, etc.
6. editing image: resize, crop, rotate.

Install
-------

[](#install)

Either run

```
php composer require wiperawa/yii2-gallery "@dev"

```

or add this line to composer.json of your project

```
"wiperawa/yii2-gallery": "@dev",

```

And run

```
php composer update

```

After that run migration to create neccesary table

```
php yii migrate/up --migrationPath=@vendor/wiperawa/yii2-gallery/src/migrations

```

Setup and Usage
---------------

[](#setup-and-usage)

Add module 'gallery' in your config file of app. (usually main.php)

```
    'modules' => [
        'gallery' => [
            'class' => 'wiperawa\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', //Can be GD or Шьфпшсл
            'placeHolderPath' => '@webroot/images/placeHolder.png',
            'access' => ['@'], //roles list who have access to module, remove if dont need it.
        ],
        //...
    ]
```

Attach Behavior to the Model to which you want to attach the downloaded images:

```
    function behaviors()
    {
        return [
            'images' => [
                'class' => 'wiperawa\gallery\behaviors\AttachImages',
                'mode' => 'gallery',
                'quality' => 60,
                'maxWidth' => 1920, //Set if need to resize image by height or width . NOTE that this take action only for server-size resizing, better to use client-side. see widget declaration below.
                'maxHeight' => 1080,
                'galleryId' => 'picture',	//here can be your model name for example
                'allowExtensions' => ['jpg', 'jpeg', 'png', 'gif'],
                'galleryBeforeDelete' => 'galleryBeforeDeleteEvt', // main model method name to fire before Image delete
                'galleryBeforeInsert' => 'galleryBeforeInsertEvt', // main model method name to fire before Image  insert
                'galleryBeforeSetMain' => 'galleryBeforeSetMain', // main model method name to fire before Image setMain event
                'galleryCheckRightsCallback' => 'galleryCheckRightsCallback', // Main Model method that calls from defaultController before any image manipulation. if return false no action performed
            ],
        ];
    }
```

mode - upload type: gallery - mass upload, single - single image quality (0 - 100) - compression quality, 0 - max compression, 100 - max quality. galleryId - identificatior of your gallery. can be model name for example galleryBeforeDelete - name of method of related model galleryBeforeInsert - name of method of related model galleryBeforeSetMain - name of method of related model galleryCheckRightsCallback - if set, will fire on each image operation, like crop, change, delete, etc. if you return false here, operation will cancelled It useful for frontend, for example if member should have ability to manupulate only theirs images, we can check owner id with logged member id

Behavior usage
--------------

[](#behavior-usage)

```
$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
}
```

Widgets
-------

[](#widgets)

Images are downloaded through the widget. Add to the \_form.php inside the CRUD form of your model. The following parameters are passed to the widget: model =&gt; The model to which the pictures will be attached, by default null; previewSize =&gt; size of the preview of the downloaded images, default is '140x140'; label =&gt; label, default 'Image'; fileInputPluginLoading =&gt; whether it is necessary to show the progress loading indicator at the input location, by default true;

fileInputPluginOptions =&gt; properties of widget [kartik/file/fileInput](http://demos.krajee.com/widget-details/fileinput),defaults to \[\];

Dont forget to add multipart/form-data option for form, if you use regular upload

```
