PHP code example of shurik2k5 / yii2-upload-behavior
1. Go to this page and download the library: Download shurik2k5/yii2-upload-behavior 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/ */
$model = new Document();
$model->setScenario('update'); //Use scenarion for validation and load file
$model->uploadFromUrl('file', 'https://i.ytimg.com/vi/yfJbKba5xYM/maxresdefault.jpg');
$model->save();
$model = new Document();
$model->setScenario('update'); //Use scenarion for validation and load file
$model->uploadFromFile('file', \Yii::getAlias('@webroot/images/02.jpg'));
$model->save();
$model->getUploadUrl('file');
$model->getUploadPath('file');
class User extends ActiveRecord
{
/**
* @inheritdoc
*/
public function rules()
{
return [
['image', 'image', 'extensions' => 'jpg, jpeg, gif, png', 'on' => ['insert', 'update']],
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => \mohorev\file\UploadImageBehavior::class,
'attribute' => 'image',
'scenarios' => ['insert', 'update'],
'placeholder' => '@app/modules/user/assets/images/userpic.jpg',
'path' => '@webroot/upload/user/{id}',
'url' => '@web/upload/user/{id}',
//if need create all thumbs profiles on image upload
'createThumbsOnSave' => true,
//if need create thumb for one profile only on request by getThumbUploadUrl() method
'createThumbsOnRequest' => true,
//if you want to remove original upload file after images thumbs was generated
'deleteOriginalFile' => true,
'thumbs' => [
'thumb' => ['width' => 400, 'quality' => 90],
'preview' => ['width' => 200, 'height' => 200],
'news_thumb' => ['width' => 200, 'height' => 200, 'bg_color' => '000'],
],
],
];
}
}
//remove image and all thumbs from 'photo' attribute
$model->deleteImage('photo');