Blame view

common/components/artboximage/ArtboxImage.php 3.08 KB
d8c1a2e0   Yarik   Big commit artbox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  <?php
  
  namespace common\components\artboximage;
  
  use kartik\file\FileInput;
  use Yii;
  use yii\base\Component;
  use yii\base\ErrorException;
  use yii\helpers\Html;
  use yii\image\drivers\Image;
  
  //use common\components\artboximage\drivers\Image;
  
  class ArtboxImage extends Component {
  
      public $driver;
  
      public $presets = [];
  
      public $rootPath;
      public $rootUrl;
  
      public $extensions = [
          'jpg' => 'jpeg',
          'jpeg' => 'jpeg',
          'png' => 'png',
          'gif' => 'gif',
          'bmp' => 'bmp',
      ];
  
      public $uploadUrl = '/admin/artboxfile/action/upload';
  
      public function load($file = null, $driver = null) {
          if(empty($file) || !realpath($file)) {
              throw new ErrorException('File name can not be empty and exists');
          }
cb0d7e74   Yarik   Image fixes
37
38
39
40
41
          try {
              return Image::factory($file, $driver ? $driver : $this->driver);
          } catch (ErrorException $e) {
              return Image::factory(\Yii::getAlias('@storage') . '/no-image.png', $driver ? $driver : $this->driver);
          }
d8c1a2e0   Yarik   Big commit artbox
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
      }
  
      public function fileinputWidget($model, $modelField, $formField = 'fileUpload', $multiple = false, $imageOnly = true) {
          $options = [
              'multiple' => $multiple,
          ];
          if ($imageOnly) {
              $options['accept'] = 'image/*';
          }
          return FileInput::widget([
              'name' => $formField,
              'options' => $options,
              'pluginOptions' => [
                  'allowedFileExtensions' => array_keys($this->extensions),
                  // @todo set for multiple
                  'initialPreview' => $model->{$modelField} ? Html::img($model->{$modelField}) : '',
                  'overwriteInitial' => !$multiple,
                  'showRemove' => true,
                  'showUpload' => false,
                  'uploadUrl' => $this->uploadUrl,
                  'uploadExtraData' => [
                      'fileField' => $modelField,
                      'multiple' => intval($multiple),
                  ],
              ],
              'pluginEvents' => [
                  "change" => "function() { console.log('change'); }",
                  "open" => "function() { console.log('open'); }",
                  "save" => "function() { console.log('save'); }",
                  "upload" => "function() { console.log('upload'); }",
                  "uploaded" => "function() { console.log('uploaded'); }",
                  "filepreupload" => "function() { console.log('filepreupload'); }",
                  "fileuploaded" => "function(event, files, extra) { console.log(event, files, extra); }",
                  "fileuploaderror" => "function() { console.log('fileuploaderror'); }",
                  "filebatchuploaderror" => "function() { console.log('filebatchuploaderror'); }",
                  "filebatchuploadsuccess" => "function() { console.log('filebatchuploadsuccess'); }",
                  "filebatchuploadcomplete" => "function() { console.log('filebatchuploadcomplete'); }",
                  'filebatchuploadsuccess' => "function(event, files, extra) {
                          console.log(event, files, extra);
                  }",
              ],
          ]);
      }
  }