PHP code example of sergmoro1 / yii2-uploader

1. Go to this page and download the library: Download sergmoro1/yii2-uploader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sergmoro1 / yii2-uploader example snippets


use sergmoro1\uploader\behaviors\HaveFileBehavior;

class User extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return array_merge(parent::behaviors(), [
            [
                'class' => HaveFileBehavior::className(),
                'file_path' => '/user/',
                //'check_wh' => false, // not checking min width and height of image
                'sizes' => [
                    //'original' => ['width' => 0, 'height' => 0, 'catalog' => 'original'], // zero values mean not resize
                    'original' => ['width' => 1200, 'height' => 1200, 'catalog' => 'original'],
                    'main'     => ['width' => 400,  'height' => 400,  'catalog' => ''],
                    'thumb'    => ['width' => 90,   'height' => 90,   'catalog' => 'thumb'],
                ],
            ],
        ]);
    }
}

return [
    'aliases' => [
        '@absolute' => '/home/my/site',
        '@host'     => 'http://example.ru',
        '@uploader' => '/uploads',
    ],

    'modules' => [
        'uploader' => [
            'class' => 'sergmoro1\uploader\Module',
        ],

return [
    'aliases' => [
        '@absolute' => '/home/my/site',
        '@host'     => '',
        '@uploader' => '/uploads',
    ],

$model = User::findOne(2);

// get top thumb image of the model with image description
echo Html::img($model->getImage('thumb'), ['alt' => $model->getFileDescription()) ]);

// get top image of the model from main catalog
echo Html::img($model->getImage());

// get all images of the model from original catalog with image description
$image = $model->getImage('original');
while ($image) {
    echo Html::img($image, ['title' => $model->getFileDescription()]);
    $image = $model->getNextImage('original');
}

  <?= Uploader::widget([
    'model'        => $model,
    'appendixView' => '/user/appendix',
    'cropAllowed'  => true,
  ]) 

    <?= \sergmoro1\uploader\widgets\Uploader::widget([
        'model'       => $model,
        'draggable'   => true,
        'cropAllowed' => true,
        'limit'       => 5,
    ]) 
html
<span id='description'>
    <?= isset($file->vars->description) ? $file->vars->description : '';