|
- <?php
-
-
-
- namespace common\helpers;
-
- use yii\web\UploadedFile;
- use Yii ;
-
- class Upload
- {
-
- public static function uploadFile($model, $champs, $filename_old = '')
- {
- $file = UploadedFile::getInstance($model, $champs);
- if ($file) {
- $file_name = $file->baseName . '-' . uniqid() ;
- $file_name_extension = $file_name .'.'.$file->extension ;
- $dir_file = '../../producer/web/uploads/' ;
- $path_file = $dir_file . $file_name_extension ;
- $file->saveAs($path_file);
-
-
- list($width, $height, $type, $attr) = getimagesize($path_file);
- if($width > 500)
- {
- $image = Yii::$app->image->load($path_file);
-
-
- if($width > 1600)
- {
- $image->resize(1600)
- ->save($dir_file . $file_name . '-big.'.$file->extension);
- }
-
-
- if($width > 1024)
- {
- $image->resize(1024)->save($dir_file . $file_name_extension) ;
- $image->save($dir_file . $file_name . '-medium.'.$file->extension);
-
-
- }
-
-
- if($width > 500)
- {
- $image->resize(500)
- ->save($dir_file . $file_name . '-small.'.$file->extension);
- }
- }
-
- $model->$champs = $file_name_extension ;
- } else {
- $model->$champs = $filename_old;
- }
-
- $model->save();
- }
-
- }
|