ArtboxImage.php
1.78 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace common\components\artboximage;
use kartik\file\FileInput;
use Yii;
use yii\base\Component;
use yii\base\ErrorException;
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');
}
return Image::factory($file, $driver ? $driver : $this->driver);
}
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($modelField) : '',
'overwriteInitial' => !$multiple,
'showRemove' => true,
'showUpload' => false,
'uploadUrl' => $this->uploadUrl,
'uploadExtraData' => [
'fileField' => $modelField,
'multiple' => intval($multiple),
],
],
]);
}
}