2017 © Pedro Peláez
 

yii2-extension yii2-file-behavior

File store and image thumbs behavior for Yii2

image

paulzi/yii2-file-behavior

File store and image thumbs behavior for Yii2

  • Saturday, May 26, 2018
  • by PaulZi
  • Repository
  • 3 Watchers
  • 6 Stars
  • 167 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 15 Versions
  • 7 % Grown

The README.md

Yii2 file and image helper

File store and image thumbs behavior for Yii2., (*1)

Packagist Version Total Downloads, (*2)

Features

  • single and multiple file store for ActiveRecord
  • multiple image thumbs without extra db fields
  • user-friendly API
  • flexible class inheritance
  • support yii alias and different location of web/real path

Install

Install via Composer:, (*3)

composer require paulzi/yii2-file-behavior:~1.2.0

or add, (*4)

"paulzi/yii2-file-behavior" : "~1.2.0"

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

Usage

Use FileBehavior in model and fill attributes option:, (*6)

class Sample extends \yii\db\ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => 'paulzi\fileBehavior\FileBehavior',
                'path'  => '@webroot/files',
                'url'   => '@web/files',
                'attributes' => [
                    'file'  => [],
                    'files' => [
                        'class' => 'paulzi\fileBehavior\FileMultiple',
                    ],
                    'image' => [
                        'class' => 'paulzi\fileBehavior\Image',
                        'types' => [
                            'original' => [1200, 1200],
                            'mid'      => [400, 400],
                            'thm'      => [120, 120],
                        ],
                    ],
                    'images' => [
                        'class' => 'paulzi\fileBehavior\FileMultiple',
                        'item'  => [
                            'class' => 'paulzi\fileBehavior\Image',
                            'types' => [
                                'thm'  => [120, 120],
                            ],
                        ]
                    ],
                ],
            ],
        ];
    }
}

Set files

$model = Sample::findOne(1);
$file  = UploadedFile::getInstance($model, 'file');
$model->file->value = $file->tempName;
$model->save();

$model = Sample::findOne(2);
$files = UploadedFile::getInstances($model, 'images');
foreach ($files as $file) {
    $model->images[] = $file->tempName;
}
$model->save();

Get files

$model = Sample::findOne(1);
$url   = $model->file->url;
$path  = $model->file->path;

$model = Sample::findOne(2);
foreach ($model->images as $image) {
    echo $image->url;      // original image url
    echo $image->thm->url; // thm image url
}

Remove files

$model = Sample::findOne(1);
$model->file->value = null;
$model->save();

$model = Sample::findOne(1);
$model->files[2]->value = null;
$model->save();

$model = Sample::findOne(2);
$model->images->value = null;
$model->save();

Image salt

To generate a thumbnail file name is using a hash of the file name and type of thumbnail. If you need to protect the possibility of obtaining different types of thumbnail, set options salt by secret:, (*7)

    public function behaviors() {
        return [
            [
                'class' => 'paulzi\fileBehavior\FileBehavior',
                'attributes' => [
                    'image' => [
                        'class' => 'paulzi\fileBehavior\Image',
                        'salt'  => 'secret',
                        'types' => [
                            'mid'      => [400, 400],
                            'thm'      => [120, 120],
                        ],
                    ],
                ],
            ],
        ];
    }

Set options globally

You can set salt, path and url options globally by using Dependency Injection:, (*8)

config\main.php:, (*9)

    'aliases' => [
        '@cdnWeb' => 'http://s.example.com',
    ],

    'on beforeRequest' => function () {
        \Yii::$container->set('paulzi\fileBehavior\FileBehavior', [
            'path' => '@cdn\web\files',
            'url'  => '@cdnWeb\web\files',
        ]);
        \Yii::$container->set('paulzi\fileBehavior\Image', [
            'salt' => Yii::$app->params['salt'],
        ]);
    },

config\params-local.php:, (*10)

    'salt' => 'secret salt',

Extending

You can extend classes for change path building function or change file storing., (*11)

By default, files are stores in {path}/{folder}/{12}/{12}/{1234567890abcdef1234567890ab}.{extension}, (*12)

The Versions

26/05 2018

dev-master

9999999-dev

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

26/05 2018

v1.2.3

1.2.3.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

14/05 2018

v1.2.2

1.2.2.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

14/05 2018

v1.2.1

1.2.1.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

14/02 2018

v1.2.0

1.2.0.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

12/01 2017

v1.1.2

1.1.2.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

30/08 2016

v1.1.1

1.1.1.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

30/08 2016

v1.1.0

1.1.0.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

30/08 2016

v1.0.6

1.0.6.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

26/08 2016

v1.0.5

1.0.5.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

26/08 2016

v1.0.4

1.0.4.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

25/08 2016

v1.0.3

1.0.3.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

14/07 2016

v1.0.2

1.0.2.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

01/04 2016

v1.0.1

1.0.1.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior

29/03 2016

v1.0.0

1.0.0.0

File store and image thumbs behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar PaulZi

yii2 file behavior image behavior