|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Application\Bundle\DefaultBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
8
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
9
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
|
10
|
|
|
use Symfony\Component\Validator\Constraints\Image; |
|
11
|
|
|
use Symfony\Component\Validator\Constraints\Collection; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class UploadController. |
|
18
|
|
|
*/ |
|
19
|
|
|
class UploadController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Upload image (for markitup plugin). |
|
23
|
|
|
* |
|
24
|
|
|
* @param Request $request |
|
25
|
|
|
* |
|
26
|
|
|
* @return JsonResponse |
|
27
|
|
|
* |
|
28
|
|
|
* @Route("/admin/text-area/uploadImage", name="text_area_upload_image") |
|
29
|
|
|
* |
|
30
|
|
|
* @Method({"POST"}) |
|
31
|
|
|
*/ |
|
32
|
|
|
public function uploadImageAction(Request $request) |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var $file \Symfony\Component\HttpFoundation\File\UploadedFile|null */ |
|
35
|
|
|
$file = $request->files->get('upload_file'); |
|
36
|
|
|
|
|
37
|
|
|
$fileConstraint = new Collection( |
|
38
|
|
|
[ |
|
39
|
|
|
'file' => [ |
|
40
|
|
|
new NotBlank(), |
|
41
|
|
|
new Image(), |
|
42
|
|
|
], |
|
43
|
|
|
] |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
// Validate |
|
47
|
|
|
/** @var $errors \Symfony\Component\Validator\ConstraintViolationList */ |
|
48
|
|
|
$errors = $this->get('validator')->validateValue(array('file' => $file), $fileConstraint); |
|
49
|
|
|
if ($errors->count() > 0) { |
|
50
|
|
|
return new JsonResponse(['msg' => 'Your file is not valid!'], 400); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
list($width, $height) = getimagesize($file); |
|
54
|
|
|
$uploadDir = $this->container->getParameter('upload_dir'); |
|
55
|
|
|
|
|
56
|
|
|
// Move uploaded file |
|
57
|
|
|
$newFileName = uniqid().'.'.$file->guessExtension(); |
|
58
|
|
|
$path = $this->container->getParameter('kernel.root_dir').'/../web/'.$uploadDir; |
|
59
|
|
|
try { |
|
60
|
|
|
$file->move($path, $newFileName); |
|
61
|
|
|
} catch (FileException $e) { |
|
62
|
|
|
return new JsonResponse(['msg' => $e->getMessage()], 400); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$filter = 'upload_image'; |
|
66
|
|
|
$target = $uploadDir.'/'.$newFileName; |
|
67
|
|
|
$cacheManager = $this->get('liip_imagine.cache.manager'); |
|
68
|
|
|
$filterManager = $this->get('liip_imagine.filter.manager'); |
|
69
|
|
|
$dataManager = $this->get('liip_imagine.data.manager'); |
|
70
|
|
|
|
|
71
|
|
|
$cacheManager->store($filterManager->applyFilter($dataManager->find($filter, $target), $filter), $target, $filter); |
|
72
|
|
|
|
|
73
|
|
|
$newFileName = $cacheManager->resolve($target, $filter); |
|
74
|
|
|
|
|
75
|
|
|
return new JsonResponse( |
|
76
|
|
|
$response = [ |
|
77
|
|
|
'status' => 'success', |
|
78
|
|
|
'src' => $newFileName, |
|
79
|
|
|
'width' => $width, |
|
80
|
|
|
'height' => $height, |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|