1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Generator; |
4
|
|
|
|
5
|
|
|
use League\Glide\Filesystem\FilesystemException; |
6
|
|
|
use League\Glide\Server; |
7
|
|
|
use MediaMonks\SonataMediaBundle\Model\MediaInterface; |
8
|
|
|
|
9
|
|
|
class ImageGenerator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Server |
13
|
|
|
*/ |
14
|
|
|
private $server; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var FilenameGeneratorInterface |
18
|
|
|
*/ |
19
|
|
|
private $filenameGenerator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $tmpPath; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $tmpPrefix; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Server $server |
33
|
|
|
* @param FilenameGeneratorInterface $filenameGenerator |
34
|
|
|
* @param string $tmpPath |
35
|
|
|
* @param string $tmpPrefix |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct( |
38
|
|
|
Server $server, |
39
|
|
|
FilenameGeneratorInterface $filenameGenerator, |
40
|
|
|
$tmpPath = null, |
41
|
|
|
$tmpPrefix = null |
42
|
|
|
) { |
43
|
1 |
|
$this->server = $server; |
44
|
1 |
|
$this->filenameGenerator = $filenameGenerator; |
45
|
1 |
|
$this->tmpPath = $tmpPath; |
46
|
1 |
|
$this->tmpPrefix = $tmpPrefix; |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param MediaInterface $media |
51
|
|
|
* @param array $parameters |
52
|
|
|
* @return string |
53
|
|
|
* @throws FilesystemException |
54
|
|
|
*/ |
55
|
1 |
|
public function generate(MediaInterface $media, array $parameters) |
56
|
|
|
{ |
57
|
1 |
|
$filename = $this->filenameGenerator->generate($media, $parameters); |
58
|
|
|
|
59
|
1 |
|
if (!$this->server->getSource()->has($filename)) { |
60
|
1 |
|
$this->generateImage($media, $parameters, $filename); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
return $filename; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param MediaInterface $media |
68
|
|
|
* @param array $parameters |
69
|
|
|
* @param $filename |
70
|
|
|
* @throws FilesystemException |
71
|
|
|
* @throws \Exception |
72
|
|
|
*/ |
73
|
1 |
|
private function generateImage(MediaInterface $media, array $parameters, $filename) |
74
|
|
|
{ |
75
|
1 |
|
$tmp = $this->getTemporaryFile(); |
76
|
1 |
|
if (@file_put_contents($tmp, $this->server->getSource()->read($media->getImage())) === false) { |
77
|
|
|
throw new FilesystemException('Unable to write temporary file'); |
78
|
|
|
} |
79
|
|
|
try { |
80
|
1 |
|
$this->server->getCache()->write($filename, $this->server->getApi()->run($tmp, $parameters)); |
81
|
1 |
|
} catch (\Exception $e) { |
82
|
|
|
throw new \Exception('Could not generate image', 0, $e); |
83
|
1 |
|
} finally { |
84
|
1 |
|
@unlink($tmp); |
85
|
|
|
} |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
1 |
|
private function getTemporaryFile() |
92
|
|
|
{ |
93
|
1 |
|
if (empty($this->tmpPath)) { |
94
|
1 |
|
$this->tmpPath = sys_get_temp_dir(); |
95
|
1 |
|
} |
96
|
1 |
|
if (empty($this->tmpPrefix)) { |
97
|
1 |
|
$this->tmpPrefix = 'media'; |
98
|
1 |
|
} |
99
|
|
|
|
100
|
1 |
|
return tempnam($this->tmpPath, $this->tmpPrefix); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|