/
ImageController.php
131 lines (107 loc) · 3.62 KB
/
ImageController.php
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
<?php
/*
* This file is part of the GenemuFormBundle package.
*
* (c) Olivier Chauvel <olivier@generation-multiple.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Genemu\Bundle\FormBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Genemu\Bundle\FormBundle\Gd\File\Image;
use Symfony\Component\HttpFoundation\Request;
/**
* Class ImageController
*
* @author Olivier Chauvel <olivier@generation-multiple.com>
*/
class ImageController implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* {@inheritdoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function changeAction(Request $request)
{
$rootDir = rtrim($this->container->getParameter('genemu.form.file.root_dir'), '/\\') . DIRECTORY_SEPARATOR;
$folder = rtrim($this->container->getParameter('genemu.form.file.folder'), '/\\');
$file = $request->get('image');
$handle = new Image($rootDir . $this->stripQueryString($file));
switch ($request->get('filter')) {
case 'rotate':
$handle->addFilterRotate(90);
break;
case 'negative':
$handle->addFilterNegative();
break;
case 'bw':
$handle->addFilterBw();
break;
case 'sepia':
$handle->addFilterSepia('#C68039');
break;
case 'crop':
$x = $request->get('x');
$y = $request->get('y');
$w = $request->get('w');
$h = $request->get('h');
$handle->addFilterCrop($x, $y, $w, $h);
break;
case 'blur':
$handle->addFilterBlur();
default:
break;
}
$handle->save();
$thumbnail = $handle;
if ($this->container->hasParameter('genemu.form.image.thumbnails')) {
$thumbnails = $this->container->getParameter('genemu.form.image.thumbnails');
foreach ($thumbnails as $name => $thumbnail) {
$handle->createThumbnail($name, $thumbnail[0], $thumbnail[1]);
}
$selected = key(reset($thumbnails));
if ($this->container->hasParameter('genemu.form.image.selected')) {
$selected = $this->container->getParameter('genemu.form.image.selected');
}
$thumbnail = $handle->getThumbnail($selected);
}
$json = array(
'result' => '1',
'file' => $folder . '/' . $handle->getFilename() . '?' . time(),
'thumbnail' => array(
'file' => $folder . '/' . $thumbnail->getFilename() . '?' . time(),
'width' => $thumbnail->getWidth(),
'height' => $thumbnail->getHeight()
),
'image' => array(
'width' => $handle->getWidth(),
'height' => $handle->getHeight()
)
);
return new JsonResponse($json);
}
/**
* Delete info after `?`
*
* @param string $file
*
* @return string
*/
private function stripQueryString($file)
{
if (false !== ($pos = strpos($file, '?'))) {
$file = substr($file, 0, $pos);
}
return $file;
}
}