Add in composer.json
:
{
"require": {
"rmrevin/yii2-file": "~1.1"
}
}
In config
<?
// ...
return [
// ...
'modules' => [
// ...
'file' => [
'class' => '\rmrevin\yii\module\File\Module',
'upload_alias' => '@app/web/upload',
'upload_web_alias' => '/upload',
'storage_alias' => '@app/web/storage',
'storage_web_alias' => '/storage',
'max_upload_file_size' => 10, // megabytes
],
],
];
Save file into database
use rmrevin\yii\module\File;
// external resource
$File = File\models\File::push(new File\component\ExternalResource('https://www.google.ru/images/srpr/logo11w.png'));
// internal resource
$File = File\models\File::push(new File\component\InternalResource('/var/www/images/pick.png'));
// uploaded resource
$File = File\models\File::push(new File\component\UploadedResource(UploadedFile::getInstance($model, 'file')));
Manipulation with images
use rmrevin\yii\module\File;
$File = File\models\File::find()->one();
echo Html::img((string)$File->image()
->resizeByWidth(100));
// available methods: resize, resizeByWidth, resizeByHeight, crop, thumbnail, watermark, text
\rmrevin\yii\module\File\component\*Resource
transferred to the own namespace rmrevin\yii\module\File\resources\*Resource
.
Comments