Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Upload.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. namespace common\helpers;
  8. use yii\web\UploadedFile;
  9. use Yii ;
  10. class Upload {
  11. public static function uploadFile($model, $champs, $filename_old = '') {
  12. $file = UploadedFile::getInstance($model, $champs);
  13. if ($file) {
  14. $file_name = $file->baseName . '-' . uniqid() ;
  15. $file_name_extension = $file_name .'.'.$file->extension ;
  16. $dir_file = '../../frontend/web/uploads/' ;
  17. $path_file = $dir_file . $file_name_extension ;
  18. $file->saveAs($path_file);
  19. // resize image
  20. list($width, $height, $type, $attr) = getimagesize($path_file);
  21. if($width > 500)
  22. {
  23. $image = Yii::$app->image->load($path_file);
  24. // big
  25. if($width > 1600)
  26. {
  27. $image->resize(1600)
  28. ->save($dir_file . $file_name . '-big.'.$file->extension);
  29. }
  30. // medium
  31. if($width > 1024)
  32. {
  33. $image->resize(1024)->save($dir_file . $file_name_extension) ;
  34. $image->save($dir_file . $file_name . '-medium.'.$file->extension);
  35. }
  36. // small
  37. if($width > 500)
  38. {
  39. $image->resize(500)
  40. ->save($dir_file . $file_name . '-small.'.$file->extension);
  41. }
  42. }
  43. $model->$champs = $file_name_extension ;
  44. } else {
  45. $model->$champs = $filename_old;
  46. }
  47. $model->save();
  48. }
  49. }