|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2014 Jonathan Bouzekri. All rights reserved. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright 2014 Jonathan Bouzekri <[email protected]> |
|
7
|
|
|
* @license https://github.com/jbouzekri/FileUploaderBundle/blob/master/LICENSE |
|
8
|
|
|
* @link https://github.com/jbouzekri/FileUploaderBundle |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Jb\Bundle\FileUploaderBundle\Service; |
|
12
|
|
|
|
|
13
|
|
|
use Jb\Bundle\FileUploaderBundle\Service\ResolverChain; |
|
14
|
|
|
use Jb\Bundle\FileUploaderBundle\Service\EndpointConfiguration; |
|
15
|
|
|
use Jb\Bundle\FileUploaderBundle\Exception\JbFileUploaderException; |
|
16
|
|
|
use Jb\Bundle\FileUploaderBundle\Service\ValidatorManager; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Croper |
|
20
|
|
|
* |
|
21
|
|
|
* @author jobou |
|
22
|
|
|
*/ |
|
23
|
|
|
class Croper |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var \Jb\Bundle\FileUploaderBundle\Service\CropFileManager |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $cropManager; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Jb\Bundle\FileUploaderBundle\Service\ResolverChain |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $resolvers; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \Jb\Bundle\FileUploaderBundle\Service\EndpointConfiguration |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $configuration; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \Jb\Bundle\FileUploaderBundle\Service\ValidatorManager |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $validator; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Jb\Bundle\FileUploaderBundle\Service\CropFileManager $cropManager |
|
50
|
|
|
* @param \Jb\Bundle\FileUploaderBundle\Service\ResolverChain $resolvers |
|
51
|
|
|
* @param \Jb\Bundle\FileUploaderBundle\Service\EndpointConfiguration $configuration |
|
52
|
|
|
* @param \Jb\Bundle\FileUploaderBundle\Service\ValidatorManager $validator |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct( |
|
55
|
|
|
CropFileManager $cropManager, |
|
56
|
|
|
ResolverChain $resolvers, |
|
57
|
|
|
EndpointConfiguration $configuration, |
|
58
|
|
|
ValidatorManager $validator |
|
59
|
|
|
) { |
|
60
|
|
|
$this->cropManager = $cropManager; |
|
61
|
|
|
$this->resolvers = $resolvers; |
|
62
|
|
|
$this->configuration = $configuration; |
|
63
|
|
|
$this->validator = $validator; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Crop an image |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $endpoint |
|
70
|
|
|
* @param array $data |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function crop($endpoint, array $data) |
|
75
|
|
|
{ |
|
76
|
|
|
// Throw ValidationException if there is an error |
|
77
|
|
|
$this->validator->validate($endpoint, $data, 'crop_validators'); |
|
78
|
|
|
|
|
79
|
|
|
// Generate croped image |
|
80
|
|
|
$cropedFile = $this->cropManager->transformFile($data); |
|
81
|
|
|
|
|
82
|
|
|
// Save it to filesystem using gaufrette |
|
83
|
|
|
$this->cropManager->saveTransformedFile($endpoint, $cropedFile, $data); |
|
84
|
|
|
|
|
85
|
|
|
// Return data |
|
86
|
|
|
return array( |
|
87
|
|
|
'filepath' => $this->resolvers->getResolver($this->getCropResolver($endpoint))->getUrl($data['filename']), |
|
88
|
|
|
'filename' => $data['filename'] |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get crop resolver configuration |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $endpoint |
|
96
|
|
|
* @return string |
|
97
|
|
|
* |
|
98
|
|
|
* @throws JbFileUploaderException |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getCropResolver($endpoint) |
|
101
|
|
|
{ |
|
102
|
|
|
$cropedResolver = $this->configuration->getValue($endpoint, 'croped_resolver'); |
|
103
|
|
|
if (!$cropedResolver) { |
|
104
|
|
|
throw new JbFileUploaderException('No croped_resolver configuration for endpoint '.$endpoint); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $cropedResolver; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|