PHP code example of fabiomlferreira / yii2-file-manager

1. Go to this page and download the library: Download fabiomlferreira/yii2-file-manager 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/ */

    

fabiomlferreira / yii2-file-manager example snippets


'modules' => [
    'filemanager' => [
        'class' => 'fabiomlferreira\filemanager\Module',
        'rename' => true, //enable upload multiple images with the same name, this will rewrite the images name
        'optimizeOriginalImage' => true, //Optimize the original image
        'maxSideSize' => 1200, //limit the maximum size for the original image only work if 'optimizeOriginalImage' => true
        'originalQuality' => 80, // quality for the original image  only work if 'optimizeOriginalImage' => true
        'thumbnailOnTheFly' => false,  //if is true will generate the thumbnails on the fly, is         ],
            'large' => [
                'name' => 'Large',
                'size' => [500, 400],
            ],
        ],
        // following line will restrict access to admin controller from frontend application
        'as frontend' => 'fabiomlferreira\filemanager\filters\FrontendFilter',
    ],
],

'thumbs' => [
    'small' => [
        'name' => 'Small',
        'size' => [100, 100],
    ],
    'medium' => [
        'name' => 'Regular',
        'size' => [300, 200],
    ],
    'large' => [
        'name' => 'Large',
        'size' => [500, 400],
        'mode' => \Imagine\Image\ImageInterface::THUMBNAIL_INSET
    ],
],

'thumbs' => [
    'small' => [
        'name' => 'Small',
        'size' => [100, 100],
    ],
    'medium' => [
        'name' => 'Regular',
        'size' => [300, 200],
    ],
    'large' => [
        'name' => 'Large',
        'size' => [500, 400],
        'mode' => \Imagine\Image\ImageInterface::THUMBNAIL_INSET,
        'keepAspectRatio' => true,
    ],
],

'thumbs' => [
    'small' => [
        'name' => 'Small',
        'size' => [100, 100],
    ],
    'medium' => [
        'name' => 'Regular',
        'size' => [300, 200],
    ],
    'large' => [
        'name' => 'Large',
        'size' => [500, 400],
        'forceUpscale' => true,
    ],
],

'thumbs' => [
    'small' => [
        'name' => 'Small',
        'size' => [100, 100],
    ],
    'medium' => [
        'name' => 'Regular',
        'size' => [300, 200],
    ],
    'large' => [
        'name' => 'Large',
        'size' => [500, 400],
        'mode' => \Imagine\Image\ImageInterface::THUMBNAIL_INSET,
        'thumbnailBackgroundColor' => 'FFF',
        'thumbnailBackgroundAlpha' => 0
    ],
],

'components' => [
    'thumbnail' => [
        'class' => 'fabiomlferreira\filemanager\Thumbnail',
        'cachePath' => '@webroot/assets/thumbnails', // path for the folder for temporary thumbnails 
        'basePath' => '@webroot',
        'cacheExpire' => 2592000, // time that the thumbnails keeps in cache
        'options' => [
            'placeholder' => [
                'type' => fabiomlferreira\filemanager\Thumbnail::PLACEHOLDER_TYPE_JS,
                'backgroundColor' => '#f5f5f5',
                'textColor' => '#cdcdcd',
                'text' => 'Ooops',
                'random' => true,
                'cache' => false,
            ],
            'quality' => 75
        ]
    ],
]

use fabiomlferreira\filemanager\widgets\FileInput;

echo $form->field($model, 'original_thumbnail')->widget(FileInput::className(), [
    'buttonTag' => 'button',
    'buttonName' => 'Browse',
    'buttonOptions' => ['class' => 'btn btn-default'],
    'options' => ['class' => 'form-control'],
    // Widget template
    'template' => '<div class="input-group">{input}<span class="input-group-btn">{button}</span></div>',
    // Optional, if set, only this image can be selected by user
    'thumb' => 'original',
    // Optional, if set, in container will be inserted selected image
    'imageContainer' => '.img',
    // Default to FileInput::DATA_URL. This data will be inserted in input field
    'pasteData' => FileInput::DATA_URL,
    // JavaScript function, which will be called before insert file data to input.
    // Argument data contains file data.
    // data example: [alt: "some description", description: "123", url: "/uploads/2017/03/vedma-100x100.jpeg", id: "45"]
    'callbackBeforeInsert' => 'function(e, data) {
        console.log( data );
    }',
]);

echo FileInput::widget([
    'name' => 'mediafile',
    'buttonTag' => 'button',
    'buttonName' => 'Browse',
    'buttonOptions' => ['class' => 'btn btn-default'],
    'options' => ['class' => 'form-control'],
    // Widget template
    'template' => '<div class="input-group">{input}<span class="input-group-btn">{button}</span></div>',
    // Optional, if set, only this image can be selected by user
    'thumb' => 'original',
    // Optional, if set, in container will be inserted selected image
    'imageContainer' => '.img',
    // Default to FileInput::DATA_IDL. This data will be inserted in input field
    'pasteData' => FileInput::DATA_ID,
    // JavaScript function, which will be called before insert file data to input.
    // Argument data contains file data.
    // data example: [alt: "Ведьма с кошкой", description: "123", url: "/uploads/2014/12/vedma-100x100.jpeg", id: "45"]
    'callbackBeforeInsert' => 'function(e, data) {
        console.log( data );
    }',
]);

use fabiomlferreira\filemanager\widgets\TinyMCE;

<?= $form->field($model, 'content')->widget(TinyMCE::className(), [
    'clientOptions' => [
           'language' => 'ru',
        'menubar' => false,
        'height' => 500,
        'image_dimensions' => false,
        'plugins' => [
            'advlist autolink lists link image charmap print preview anchor searchreplace visualblocks code contextmenu table',
        ],
        'toolbar' => 'undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code',
    ],
]); 

use fabiomlferreira\filemanager\behaviors\MediafileBehavior;

public function behaviors()
{
    return [
        'mediafile' => [
            'class' => MediafileBehavior::className(),
            'name' => 'post',
            'attributes' => [
                'thumbnail',
            ],
        ]
    ];
}

use fabiomlferreira\filemanager\models\Mediafile;

$model = Post::findOne(1);
$mediafile = Mediafile::loadOneByOwner('post', $model->id, 'thumbnail');

// Ok, we have mediafile object! Let's do something with him:
// return url for small thumbnail, for example: '/uploads/2014/12/flying-cats.jpg'
echo $mediafile->getThumbUrl('small');
// return image tag for thumbnail, for example: '<img src="/uploads/2017/03/flying-dogs.jpg" alt="ypload">'
echo $mediafile->getThumbImage('small'); // return url for small thumbnail

php composer.phar