|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibraryModule\Controllers\Traits\Async; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Common\Records\Record; |
|
6
|
|
|
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait; |
|
7
|
|
|
use ImgPicker; |
|
8
|
|
|
use Nip\Http\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Gallery |
|
13
|
|
|
* @package ByTIC\MediaLibraryModule\Controllers\Traits\Async |
|
14
|
|
|
* |
|
15
|
|
|
* @method Request getRequest() |
|
16
|
|
|
* @method HasMediaTrait|Record getModelFromRequest() |
|
17
|
|
|
*/ |
|
18
|
|
|
trait Gallery |
|
19
|
|
|
{ |
|
20
|
|
|
public function uploadGallery() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->uploadGalleryImgPicker(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function uploadGalleryImgPicker() |
|
26
|
|
|
{ |
|
27
|
|
|
$item = $this->getModelFromRequest(); |
|
28
|
|
|
$newMedia = $item->getMedia('images')->newMedia(); |
|
|
|
|
|
|
29
|
|
|
$minWidth = $item->getMedia('images')->getConstraint()->minWidth; |
|
|
|
|
|
|
30
|
|
|
$minHeight = $item->getMedia('images')->getConstraint()->minHeight; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$options = [ |
|
33
|
|
|
|
|
34
|
|
|
// Upload directory path |
|
35
|
|
|
'upload_dir' => TMP_PATH, |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
// Upload directory url: |
|
38
|
|
|
'upload_url' => TMP_URL, |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
// Accepted file types: |
|
41
|
|
|
'accept_file_types' => 'png|jpg|jpeg|gif', |
|
42
|
|
|
|
|
43
|
|
|
// Directory mode: |
|
44
|
|
|
'mkdir_mode' => 0777, |
|
45
|
|
|
|
|
46
|
|
|
// File size restrictions (in bytes): |
|
47
|
|
|
'max_file_size' => UploadedFile::getMaxFilesize(), |
|
48
|
|
|
'min_file_size' => 1, |
|
49
|
|
|
|
|
50
|
|
|
// Image resolution restrictions (in px): |
|
51
|
|
|
'max_width' => null, |
|
52
|
|
|
'max_height' => null, |
|
53
|
|
|
'min_width' => $minWidth, |
|
54
|
|
|
'min_height' => $minHeight, |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Load callback |
|
58
|
|
|
* |
|
59
|
|
|
* @param ImgPicker $instance |
|
60
|
|
|
* @return string|array |
|
61
|
|
|
*/ |
|
62
|
|
|
'load' => function ($instance) { |
|
|
|
|
|
|
63
|
|
|
//return 'avatar.jpg'; |
|
64
|
|
|
}, |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Delete callback |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $filename |
|
70
|
|
|
* @param ImgPicker $instance |
|
71
|
|
|
* @return boolean |
|
72
|
|
|
*/ |
|
73
|
|
|
'delete' => function ($filename, $instance) { |
|
|
|
|
|
|
74
|
|
|
return true; |
|
75
|
|
|
}, |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Upload start callback |
|
79
|
|
|
* |
|
80
|
|
|
* @param stdClass $image |
|
81
|
|
|
* @param ImgPicker $instance |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
'upload_start' => function ($image, $instance) { |
|
|
|
|
|
|
85
|
|
|
$image->name = '~toCropFP' . $item->ID . '-' . rand(1000, 4000) . '.' . $image->type; |
|
|
|
|
|
|
86
|
|
|
}, |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Upload complete callback |
|
90
|
|
|
* |
|
91
|
|
|
* @param stdClass $image |
|
92
|
|
|
* @param ImgPicker $instance |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
'upload_complete' => function ($image, $instance) { |
|
|
|
|
|
|
96
|
|
|
}, |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Crop start callback |
|
100
|
|
|
* |
|
101
|
|
|
* @param stdClass $image |
|
102
|
|
|
* @param ImgPicker $instance |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
'crop_start' => function ($image, $instance) { |
|
|
|
|
|
|
106
|
|
|
$image->name = sha1(microtime()) . '.' . $image->type; |
|
107
|
|
|
}, |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Crop complete callback |
|
111
|
|
|
* |
|
112
|
|
|
* @param stdClass $image |
|
113
|
|
|
* @param ImgPicker $instance |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
'crop_complete' => function ($image, $instance) { |
|
117
|
|
|
foreach (['full', 'default'] as $version) { |
|
118
|
|
|
$filename = $instance->getVersionFilename($image->name, $version); |
|
119
|
|
|
$filepath = $instance->getUploadPath($filename, $version); |
|
120
|
|
|
rename($filepath, str_replace('-' . $version . '.', '.', $filepath)); |
|
121
|
|
|
} |
|
122
|
|
|
}, |
|
123
|
|
|
]; |
|
124
|
|
|
if (@$_POST['action'] == 'crop') { |
|
125
|
|
|
$filesystem = $item->getMedia('images')->getFilesystem(); |
|
|
|
|
|
|
126
|
|
|
$rootPath = $filesystem->getAdapter()->getPathPrefix(); |
|
|
|
|
|
|
127
|
|
|
// Image versions: |
|
128
|
|
|
$options['versions'] = [ |
|
129
|
|
|
'full' => [ |
|
130
|
|
|
'upload_dir' => $rootPath . $newMedia->getBasePath('full') . DIRECTORY_SEPARATOR, |
|
|
|
|
|
|
131
|
|
|
'upload_url' => $filesystem->getUrl($newMedia->getBasePath('full')), |
|
|
|
|
|
|
132
|
|
|
'max_width' => 1600, |
|
133
|
|
|
'max_height' => 1600, |
|
134
|
|
|
], |
|
135
|
|
|
'default' => [ |
|
136
|
|
|
'upload_dir' => $rootPath . $newMedia->getBasePath('default') . DIRECTORY_SEPARATOR, |
|
137
|
|
|
'upload_url' => $filesystem->getUrl($newMedia->getBasePath('default')), |
|
138
|
|
|
'max_width' => $minWidth, |
|
139
|
|
|
'crop' => $minHeight, |
|
140
|
|
|
], |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (intval($_SERVER['CONTENT_LENGTH']) > 0 && count($_POST) === 0) { |
|
145
|
|
|
$maxSize = round(($this->getRequest()->server->getMaxFileSize() / 1048576), 2) . 'MB'; |
|
|
|
|
|
|
146
|
|
|
$this->response['error'] = 'File to big. Max size [' . $maxSize . ']'; |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
return $this->_output(); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// Create new ImgPicker instance |
|
152
|
|
|
new ImgPicker($options); |
|
153
|
|
|
die(''); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.