an easy way for make folders and upload images with one simple code line.
The preferred way to install this extension is through composer.
Either run
php composer.phar require gomonkey/yii2-easy-uploader "*"
or add
"gomonkey/yii2-easy-uploader": "*"
to the require section of your composer.json
file.
Add in your config file ( common/config/main.php ) for using in frontend and backend
'components' => [
'uploaders' => [
'class' => 'gomonkey\uploader\Uploader',
'baseFrontendUrl' => dirname(dirname(__DIR__)) . '/frontend/web/images',
'baseBackendUrl' => dirname(dirname(__DIR__)) . '/backend/web/images',
'rename' => true, // Rename file
'random' => 12, // random alphanumeric name
'remove' => true, // Remove original file after upload
'folders' => [
[
'name' => '1200',
'quality' => 70,
'width' => 1200
],
[
'name' => '800',
'quality' => 70,
'width' => 800
],
[
'name' => '600',
'quality' => 70,
'width' => 600
],
[
'name' => 'avatars',
'quality' => 70,
'width' => 200
]
]
],
],
if you use basic template, you can still use the same code above, just put the code in you config file and change baseFrontendUrl. You can remove or comment baseBackendUrl
add UploadFile in your controller
use yii\web\UploadedFile;
In your controller action :
$upload = new Yii::$app->uploaders();
/**
If you want to use backend path:
$upload = new Yii::$app->uploaders("backend");
**/
$model->image = $upload->upload(UploadedFile::getInstance($model, 'image'), "avatars");
$model->image now have the name of the uploaded image, ready to save it in database.
foreach (UploadedFile::getInstances($model, 'image') as $file) {
$model->image = (new Yii::$app->uploaders())->upload($file, "avatars");
}
Now it is possible to upload images from other website, using dynamic or static urls. the same code can be used in a loop
$model->image = $upload->uploadFromUrl("https://www.website.it/url/to/image/image.jpg", "myFolder");
N.B. no need upload instance
You can make infinite folders. For example with user id: images/user/3/1200/imagename.jpg
$model->image = $upload->upload(UploadedFile::getInstance($model, 'image'), "users/".Yii::$app->user->id);
do not forget to use multipart/form-data to your form
<?php $form = ActiveForm::begin([
'options' => ['enctype'=>'multipart/form-data']
]); ?>
If you need to delete one or more images from all folders:
( new Yii::$app->uploaders() )->delete( file, folder );
for example:
(new Yii::$app->uploaders())->delete($model->name, "/products/".Yii::$app->user->id);
It remove all images in your path/products/user id/others setted folder/file
Will rename your uploaded file, set to false if you don't want to change the file name
Remove the original file
Random is the length of the alphanumeric image name
Your frontend ( or web path for basic template ) path to image folder
Your backend path to image folder
The folders are not the primary, these must be set in controller
- name[] = is the name of the folder
- quality[] = is the quality of the uploaded image
- width[] = is the width of the image, the height will be scaled