-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathUploadController.php
More file actions
342 lines (285 loc) · 14.1 KB
/
Copy pathUploadController.php
File metadata and controls
342 lines (285 loc) · 14.1 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
namespace Comur\ImageBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Finder\Finder;
use Comur\ImageBundle\Handler\UploadHandler;
class UploadController extends Controller
{
/**
* Save uploaded image according to comur_image field configuration
*
* @param Request $request
*/
public function uploadImageAction(Request $request
/*, $uploadUrl, $paramName, $webDir, $minWidth=1, $minHeight=1*/
){
$config = json_decode($request->request->get('config'),true);
$uploadUrl = $config['uploadConfig']['uploadUrl'];
$uploadUrl = substr($uploadUrl, -strlen('/')) === '/' ? $uploadUrl : $uploadUrl . '/';
// We must use a streamed response because the UploadHandler echoes directly
$response = new StreamedResponse();
$webDir = $config['uploadConfig']['webDir'];
$webDir = substr($webDir, -strlen('/')) === '/' ? $webDir : $webDir . '/';
$filename = sha1(uniqid(mt_rand(), true));
$thumbsDir = $this->container->getParameter('comur_image.thumbs_dir');
$thumbSize = $this->container->getParameter('comur_image.media_lib_thumb_size');
$galleryDir = $this->container->getParameter('comur_image.gallery_dir');
$gThumbSize = $this->container->getParameter('comur_image.gallery_thumb_size');
$ext = $request->files->get('image_upload_file')->getClientOriginalExtension();//('image_upload_file');
$completeName = $filename.'.'.$ext;
$controller = $this;
$handlerConfig = array(
'upload_dir' => $uploadUrl,
'param_name' => 'image_upload_file',
'file_name' => $filename,
'upload_url' => $config['uploadConfig']['webDir'],
'min_width' => $config['cropConfig']['minWidth'],
'min_height' => $config['cropConfig']['minHeight'],
'image_versions' => array(
'thumbnail' => array(
'upload_dir' => $uploadUrl.$thumbsDir.'/',
'upload_url' => $config['uploadConfig']['webDir'].'/'.$thumbsDir.'/',
'crop' => true,
'max_width' => $thumbSize,
'max_height' => $thumbSize
)
)
);
$transDomain = $this->container->getParameter('comur_image.translation_domain');
$errorMessages = array(
1 => $this->get('translator')->trans('The uploaded file exceeds the upload_max_filesize directive in php.ini', array(), $transDomain),
2 => $this->get('translator')->trans('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', array(), $transDomain),
3 => $this->get('translator')->trans('The uploaded file was only partially uploaded', array(), $transDomain),
4 => $this->get('translator')->trans('No file was uploaded', array(), $transDomain),
6 => $this->get('translator')->trans('Missing a temporary folder', array(), $transDomain),
7 => $this->get('translator')->trans('Failed to write file to disk', array(), $transDomain),
8 => $this->get('translator')->trans('A PHP extension stopped the file upload', array(), $transDomain),
'post_max_size' => $this->get('translator')->trans('The uploaded file exceeds the post_max_size directive in php.ini', array(), $transDomain),
'max_file_size' => $this->get('translator')->trans('File is too big', array(), $transDomain),
'min_file_size' => $this->get('translator')->trans('File is too small', array(), $transDomain),
'accept_file_types' => $this->get('translator')->trans('Filetype not allowed', array(), $transDomain),
'max_number_of_files' => $this->get('translator')->trans('Maximum number of files exceeded', array(), $transDomain),
'max_width' => $this->get('translator')->trans('Image exceeds maximum width', array(), $transDomain),
'min_width' => $this->get('translator')->trans('Image requires a minimum width (%min%)', array('%min%' => $config['cropConfig']['minWidth']), $transDomain),
'max_height' => $this->get('translator')->trans('Image exceeds maximum height', array(), $transDomain),
'min_height' => $this->get('translator')->trans('Image requires a minimum height (%min%)', array('%min%' => $config['cropConfig']['minHeight']), $transDomain),
'abort' => $this->get('translator')->trans('File upload aborted', array(), $transDomain),
'image_resize' => $this->get('translator')->trans('Failed to resize image', array(), $transDomain),
);
$response->setCallback(function () use($handlerConfig, $errorMessages) {
new UploadHandler($handlerConfig, true, $errorMessages);
});
return $response->send();
}
/**
* Crop image using jCrop and upload config parameters and create thumbs if needed
*
* @param Request $request
*/
public function cropImageAction(Request $request
/*, $uploadUrl, $webDir, $imageName, $x, $y, $w, $h, $tarW, $tarH*/
){
$config = json_decode($request->request->get('config'),true);
$params = $request->request->all();
// var_dump($params);exit;
$x = (int) round($params['x']);
$y = (int) round($params['y']);
$w = (int) round($params['w']);
$h = (int) round($params['h']);
$tarW = (int) round($config['cropConfig']['minWidth']);
$tarH = (int) round($config['cropConfig']['minHeight']);
$forceResize = $config['cropConfig']['forceResize'];
$default_jpeg_quality = $this->container->getParameter('comur_image.jpeg_quality');
// $disableCrop = $config['cropConfig']['disableCrop'];
$uploadUrl = urldecode($config['uploadConfig']['uploadUrl']);
$webDir = urldecode($config['uploadConfig']['webDir']);
$imageName = $params['imageName'];
$src = $uploadUrl.'/'.$imageName;
// if($disableCrop){
// list($w, $h) = getimagesize($src);
// if($config['cropConfig']['aspectRatio'])
// {
// list($w, $h) = $this->getMaxCropValues($w, $h, $tarW, $tarH);
// }
// }
if (!is_dir($uploadUrl.'/'.$this->container->getParameter('comur_image.cropped_image_dir').'/')) {
mkdir($uploadUrl.'/'.$this->container->getParameter('comur_image.cropped_image_dir').'/', 0755, true);
}
$ext = pathinfo($imageName, PATHINFO_EXTENSION);
$imageName = sha1(uniqid(mt_rand(), true)).'.'.$ext;
$destSrc = $uploadUrl.'/'.$this->container->getParameter('comur_image.cropped_image_dir').'/'.$imageName;
//$writeFunc($dstR,$src,$imageQuality);
$destW = $w;
$destH = $h;
if($forceResize){
$destW = $tarW;
$destH = $tarH;
if(round($w/$h, 2) != round($tarW/$tarH, 2)){
// var_dump($destW, $destH, $w, $h, $this->getMaxResizeValues($w, $h, $tarW, $tarH));exit;
// $destW = $w;
// $destH = $h;
list($destW, $destH) = $this->getMinResizeValues($w, $h, $tarW, $tarH);
}
}
$jpegQuality = isset($config['cropConfig']['jpeg_quality']) ? $config['cropConfig']['jpeg_quality'] : $defaultJpegQuality;
$this->resizeCropImage($destSrc,$src,0,0,$x,$y,$destW,$destH,$w,$h,$jpegQuality);
$galleryThumbOk = false;
$isGallery = isset($config['uploadConfig']['isGallery']) ? $config['uploadConfig']['isGallery'] : false;
$galleryDir = $this->container->getParameter('comur_image.gallery_dir');
$gThumbSize = $this->container->getParameter('comur_image.gallery_thumb_size');
if($isGallery)
{
if(!isset($config['cropConfig']['thumbs']) || !($thumbs = $config['cropConfig']['thumbs']) || !count($thumbs))
{
$config['cropConfig']['thumbs'] = array();
}
$config['cropConfig']['thumbs'][] = array('maxWidth' => $gThumbSize, 'maxHeight' => $gThumbSize, 'forGallery' => true);
}
//Create thumbs if asked
$previewSrc = '/'.$config['uploadConfig']['webDir'] . '/' . $this->container->getParameter('comur_image.cropped_image_dir') . '/'. $imageName;
if(isset($config['cropConfig']['thumbs']) && ($thumbs = $config['cropConfig']['thumbs']) && count($thumbs))
{
$thumbDir = $uploadUrl.'/'.$this->container->getParameter('comur_image.cropped_image_dir') . '/' . $this->container->getParameter('comur_image.thumbs_dir').'/';
if(!is_dir($thumbDir))
{
mkdir($thumbDir);
}
foreach($thumbs as $thumb){
$maxW = $thumb['maxWidth'];
$maxH = $thumb['maxHeight'];
if(!isset($thumb['forGallery']) && $maxW == $gThumbSize && $maxH == $gThumbSize){
$galleryThumbOk = true;
}
if(isset($thumb['forGallery']) && $galleryThumbOk) continue;
list($w, $h) = $this->getMaxResizeValues($destW, $destH, $maxW, $maxH);
$thumbName = $maxW.'x'.$maxH.'-'.$imageName;
$thumbSrc = $thumbDir . $thumbName;
$thumbJpegQuality = isset($thumb['jpeg_quality']) ? $thumb['jpeg_quality'] : $jpegQuality;
$this->resizeCropImage($thumbSrc, $destSrc, 0, 0, 0, 0, $w, $h, $destW, $destH, $thumbJpegQuality);
if(isset($thumb['useAsFieldImage']) && $thumb['useAsFieldImage']){
$previewSrc = '/'.$config['uploadConfig']['webDir'] . '/' . $this->container->getParameter('comur_image.cropped_image_dir') . '/'. $this->container->getParameter('comur_image.thumbs_dir'). '/' . $thumbName;
}
}
}
return new Response(json_encode(array('success' => true,
'filename'=>$this->container->getParameter('comur_image.cropped_image_dir').'/'.$imageName,
'previewSrc' => $previewSrc,
'galleryThumb' => $this->container->getParameter('comur_image.cropped_image_dir') . '/' . $this->container->getParameter('comur_image.thumbs_dir').'/'.$gThumbSize.'x'.$gThumbSize.'-' .$imageName)));
}
/**
* Calculates and returns maximum size to fit in maxW and maxH for resize
*/
private function getMaxResizeValues($srcW, $srcH, $maxW, $maxH){
if($srcH/$srcW < $maxH/$maxW){
$w = $maxW;
$h = $srcH * ($maxW / $srcW);
}
else{
$h = $maxH;
$w = $srcW * ($maxH / $srcH);
}
return array($w, $h);
}
/**
* Calculates and returns min size to fit in minW and minH for resize
*/
private function getMinResizeValues($srcW, $srcH, $minW, $minH){
if($srcH/$srcW > $minH/$minW){
$w = $minW;
$h = $srcH * ($minW / $srcW);
}
else{
$h = $minH;
$w = $srcW * ($minH / $srcH);
}
return array($w, $h);
}
/**
* Calculates and returns maximum size to fit in maxW and maxH for crop
*/
private function getMaxCropValues($srcW, $srcH, $maxW, $maxH)
{
$x = $y = 0;
if($srcH/$srcW > $maxH/$maxW){
$w = $srcW;
$h = $srcH * ($maxW / $maxH);
$y = round($srcH - $h / 2, 0);
}
else{
$h = $srcH;
$w = $srcW * ($maxtH / $maxW);
$x = round($srcW - $w / 2, 0);
}
return array($w, $h, $x, $y);
}
/**
* Returns files from required directory
*
* @param Request $request
*/
public function getLibraryImagesAction(Request $request){
$finder = new Finder();
$finder->sortByType();
$finder->depth('== 0');
$result = array();
$files = array();
$result['thumbsDir'] = $this->container->getParameter('comur_image.thumbs_dir');
if (!is_dir($request->request->get('dir'))) {
mkdir($request->request->get('dir').'/', 0755, true);
}
foreach ($finder->in($request->request->get('dir'))->files() as $file) {
$files[] = $file->getFilename();
}
$result['files'] = $files;
// var_dump(json_encode($result));exit;
return new Response(json_encode($result));
}
/**
* Crops or resizes image and writes it on disk
*/
private function resizeCropImage($destSrc, $imgSrc, $destX, $destY, $srcX, $srcY, $destW, $destH, $srcW, $srcH, $jpeg_quality = 90)
{
$type = strtolower(pathinfo($imgSrc, PATHINFO_EXTENSION));
switch ($type) {
case 'jpg':
case 'jpeg':
$srcFunc = 'imagecreatefromjpeg';
$writeFunc = 'imagejpeg';
$imageQuality = $jpeg_quality;
break;
case 'gif':
$srcFunc = 'imagecreatefromgif';
$writeFunc = 'imagegif';
$imageQuality = null;
break;
case 'png':
$srcFunc = 'imagecreatefrompng';
$writeFunc = 'imagepng';
$imageQuality = 9;
break;
default:
return false;
}
$imgR = $srcFunc($imgSrc);
if(round($srcW/$srcH, 2) != round($destW/$destH, 2)){
$destW = $srcW;
$destH = $srcH;
}
$dstR = imagecreatetruecolor( $destW, $destH );
imagecopyresampled($dstR,$imgR,$destX,$destY,$srcX,$srcY,$destW,$destH,$srcW,$srcH);
switch ($type) {
case 'gif':
case 'png':
imagecolortransparent($dstR, imagecolorallocate($dstR, 0, 0, 0));
case 'png':
imagealphablending($dstR, false);
imagesavealpha($dstR, true);
break;
}
$writeFunc($dstR,$destSrc,$imageQuality);
}
}