1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fenrizbes\CropBundle\Form\Type; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Form\AbstractType; |
6
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
7
|
|
|
use Symfony\Component\Form\FormInterface; |
8
|
|
|
use Symfony\Component\Form\FormView; |
9
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
10
|
|
|
use Symfony\Component\Validator\Constraints\Image; |
11
|
|
|
|
12
|
|
|
class CroppableType extends AbstractType |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
|
|
public function getName() |
18
|
|
|
{ |
19
|
|
|
return 'croppable'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function setDefaultOptions(OptionsResolverInterface $resolver) |
26
|
|
|
{ |
27
|
|
|
$resolver |
28
|
|
|
->setOptional(array( |
29
|
|
|
'width', |
30
|
|
|
'height', |
31
|
|
|
'instances', |
32
|
|
|
'validate', |
33
|
|
|
'max_canvas_width', |
34
|
|
|
'max_canvas_height' |
35
|
|
|
)) |
36
|
|
|
->setDefaults(array( |
37
|
|
|
'required' => false, |
38
|
|
|
'validate' => true, |
39
|
|
|
'max_canvas_width' => 640, |
40
|
|
|
'max_canvas_height' => 480 |
41
|
|
|
)) |
42
|
|
|
; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
49
|
|
|
{ |
50
|
|
|
unset($options['max_canvas_width']); |
51
|
|
|
unset($options['max_canvas_height']); |
52
|
|
|
|
53
|
|
|
$builder |
54
|
|
|
->add('image', 'uploadable', array( |
55
|
|
|
'label' => $options['label'], |
56
|
|
|
'file_constraints' => $this->getConstraints($options) |
57
|
|
|
)) |
58
|
|
|
->add('coordinates', 'crop_coordinates', $options) |
59
|
|
|
; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Creates and returns constraints for a file field |
64
|
|
|
* |
65
|
|
|
* @param $options |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getConstraints($options) |
69
|
|
|
{ |
70
|
|
|
$constraints = array(); |
71
|
|
|
|
72
|
|
|
if ($options['validate']) { |
73
|
|
|
$constraints[] = new Image(array( |
74
|
|
|
'mimeTypes' => array( |
75
|
|
|
'image/gif', |
76
|
|
|
'image/png', |
77
|
|
|
'image/jpg', |
78
|
|
|
'image/jpeg' |
79
|
|
|
), |
80
|
|
|
'minWidth' => $this->calcMin('width', $options), |
81
|
|
|
'minHeight' => $this->calcMin('height', $options) |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $constraints; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Calculates and returns values for the size constraint |
90
|
|
|
* |
91
|
|
|
* @param $option |
92
|
|
|
* @param array $options |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
|
public function calcMin($option, array $options) |
96
|
|
|
{ |
97
|
|
|
$min = (isset($options[$option]) ? (int)$options[$option] : 0); |
98
|
|
|
|
99
|
|
|
if (isset($options['instances']) && is_array($options['instances'])) { |
100
|
|
|
foreach ($options['instances'] as $instance) { |
101
|
|
|
$value = (isset($instance[$option]) ? (int)$instance[$option] : 0); |
102
|
|
|
|
103
|
|
|
if ($value > $min) { |
104
|
|
|
$min = $value; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $min; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options) |
116
|
|
|
{ |
117
|
|
|
$view->vars['max_canvas_width'] = (int)$options['max_canvas_width']; |
118
|
|
|
$view->vars['max_canvas_height'] = (int)$options['max_canvas_height']; |
119
|
|
|
|
120
|
|
|
$view->vars['validate'] = $options['validate']; |
121
|
|
|
$view->vars['minimums'] = array( |
122
|
|
|
'width' => $this->calcMin('width', $options), |
123
|
|
|
'height' => $this->calcMin('height', $options) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |