|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
-
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
- 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 = '../../frontend/web/uploads/' ;
- $path_file = $dir_file . $file_name_extension ;
- $file->saveAs($path_file);
-
- // resize image
- list($width, $height, $type, $attr) = getimagesize($path_file);
- if($width > 500)
- {
- $image = Yii::$app->image->load($path_file);
-
- // big
- if($width > 1600)
- {
- $image->resize(1600)
- ->save($dir_file . $file_name . '-big.'.$file->extension);
- }
-
- // medium
- if($width > 1024)
- {
- $image->resize(1024)->save($dir_file . $file_name_extension) ;
- $image->save($dir_file . $file_name . '-medium.'.$file->extension);
-
-
- }
-
- // small
- 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();
- }
-
- }
|