2017 © Pedro Peláez
 

library yii2-file-behavior

Yii2 file uploading

image

tpmanc/yii2-file-behavior

Yii2 file uploading

  • Tuesday, November 21, 2017
  • by tpmanc
  • Repository
  • 1 Watchers
  • 1 Stars
  • 224 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 2 % Grown

The README.md

yii2-file-behavior

Yii 2 file uploading, (*1)

Install via Composer

Run the following command, (*2)

$ composer require tpmanc/yii2-file-behavior "*"

or add, (*3)

$ "tpmanc/yii2-file-behavior": "*"

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

Migrations

Create migration by following command, (*5)

$ yii migrate/create images

Open the /path/to/migrations/m_xxxxxx_xxxxxx_images.php file and add following code to up() method, (*6)

        $this->createTable('image', [
            'id' => Schema::TYPE_PK,
            'itemId' => Schema::TYPE_INTEGER . ' NOT NULL',
            'order' => Schema::TYPE_INTEGER . ' NOT NULL',
            'extension' => Schema::TYPE_STRING . '(10) NOT NULL',
            'hash' => Schema::TYPE_STRING . '(32) NOT NULL',
        ]);

        $this->createTable('imageSize', [
            'id' => Schema::TYPE_PK,
            'imageId' => Schema::TYPE_INTEGER . ' NOT NULL',
            'path' => Schema::TYPE_STRING . '(255) NOT NULL',
            'size' => Schema::TYPE_STRING . '(255) NOT NULL',
        ]);

Create model

Generate Active Record model for new image and imageSize tables, (*7)

Configuring

Attach the behavior to your model class:, (*8)

use tpmanc\filebehavior\ImageBehavior;

\\ ...

    public $file;

    public function behaviors()
    {
        return [
            'ImageBehavior' => [
                'class' => ImageBehavior::className(),
                'imageModel' => 'models\Image',
                'imageSizeModel' => 'models\ImageSize',
                'imageVariable' => 'file',
                'imageFolder' => '@upload',
                'webImageFolder' => '@webupload',
                'noImagePath' => '@webupload/no-image.png',
            ],
        ];
    }

    public function rules()
    {
        ['file', 'file', 'extensions' => ['png', 'jpg'], 'maxSize' => 1024*1024*1024, 'maxFiles' => 4],
    }

If file hash will be like "6e3c797abee0ff2803ef1f952f187d2f" the file will be located in @upload/images/6e/3c/{id from image table}.jpg, (*9)

To save several sizes of image add:, (*10)

    public $file;

    public function behaviors()
    {
        return [
            'ImageBehavior' => [
                'class' => ImageBehavior::className(),
                'fileModel' => 'models\Image',
                'fileVar' => 'file',
                'fileFolder' => '@upload/images',
                'imageSizes' => [
                    'original' => [
                        'folder' => 'original',
                    ],
                    'big' => [
                        'width' => 800,
                        'height' => 600,
                        'folder' => 'big',
                    ],
                    'small' => [
                        'width' => 64,
                        'height' => 64,
                        'folder' => 'small',
                    ],
                ],
            ],
        ];
    }

    public function rules()
    {
        ['file', 'file', 'extensions' => ['png', 'jpg'], 'maxSize' => 1024*1024*1024, 'maxFiles' => 1],
    }

If file hash will be like "6e3c797abee0ff2803ef1f952f187d2f" - result 3 images:, (*11)

  • @upload/images/original/6e/3c/{id from image table}.jpg, (*12)

  • @upload/images/big/6e/3c/{id from image table}.jpg, (*13)

  • @upload/images/small/6e/3c/{id from image table}.jpg, (*14)

View file

Example of view file, (*15)

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
    <?= $form->field($model, 'file[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>

Geting images

Get single image:, (*16)

<img src="<?= $model->getImage('original') ?>" alt="">
<img src="<?= $model->getImage('big') ?>" alt="">
<img src="<?= $model->getImage('small') ?>" alt="">

Get all images:, (*17)

<?php foreach ($model->getImages('original') as $image) { ?>
    <img src="<?= $image ?>" alt="">
<?php } ?>

Get count images:, (*18)

<?php $count = 5;?>
...
<?php foreach ($model->getImages('original', $count) as $image) { ?>
    <img src="<?= $image ?>" alt="">
<?php } ?>

Return false when image does not exist:, (*19)

For single image:, (*20)

if ($model->getImage('original', true) === false) {
    ...
}

For all images:, (*21)

$count = false;
foreach ($model->getImages('original', $count, true) as $image) {
    if ($image === false) {
        ...
    }
}

The Versions

21/11 2017

dev-master

9999999-dev https://github.com/tpmanc/yii2-file-behavior

Yii2 file uploading

  Sources   Download

BSD-3-Clause

The Requires

 

file upload yii behavior fileupload

21/07 2016

1.2.1

1.2.1.0 https://github.com/tpmanc/yii2-file-behavior

Yii2 file uploading

  Sources   Download

BSD-3-Clause

The Requires

 

file upload yii behavior fileupload

04/03 2016

1.2.0

1.2.0.0 https://github.com/tpmanc/yii2-file-behavior

Yii2 file uploading

  Sources   Download

BSD-3-Clause

The Requires

 

file upload yii behavior fileupload

09/02 2016

1.1

1.1.0.0 https://github.com/tpmanc/yii2-file-behavior

Yii2 file uploading

  Sources   Download

BSD-3-Clause

The Requires

 

file upload yii behavior fileupload

03/08 2015

v1.0

1.0.0.0 https://github.com/tpmanc/yii2-file-behavior

Yii2 file uploading

  Sources   Download

BSD-3-Clause

The Requires

 

file upload yii behavior fileupload