|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\Imagine\Variation; |
|
8
|
|
|
|
|
9
|
|
|
use eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver; |
|
10
|
|
|
use eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator; |
|
11
|
|
|
use eZ\Publish\API\Repository\Values\Content\Field; |
|
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\VersionInfo; |
|
13
|
|
|
use eZ\Publish\Core\IO\IOServiceInterface; |
|
14
|
|
|
use eZ\Publish\SPI\Variation\Values\ImageVariation; |
|
15
|
|
|
use eZ\Publish\SPI\Variation\VariationHandler; |
|
16
|
|
|
use Imagine\Image\ImagineInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Alias Generator Decorator which ensures (using Imagine if needed) that ImageVariation has proper |
|
20
|
|
|
* dimensions. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ImagineAwareAliasGenerator implements VariationHandler |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var \eZ\Publish\SPI\Variation\VariationHandler |
|
26
|
|
|
*/ |
|
27
|
|
|
private $aliasGenerator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator |
|
31
|
|
|
*/ |
|
32
|
|
|
private $variationPathGenerator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \eZ\Publish\Core\IO\IOServiceInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $ioService; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $configResolver; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var \Imagine\Image\ImagineInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
private $imagine; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct( |
|
50
|
|
|
VariationHandler $aliasGenerator, |
|
51
|
|
|
VariationPathGenerator $variationPathGenerator, |
|
52
|
|
|
IOServiceInterface $ioService, |
|
53
|
|
|
ImagineInterface $imagine |
|
54
|
|
|
) { |
|
55
|
|
|
$this->aliasGenerator = $aliasGenerator; |
|
56
|
|
|
$this->variationPathGenerator = $variationPathGenerator; |
|
57
|
|
|
$this->ioService = $ioService; |
|
58
|
|
|
$this->imagine = $imagine; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns a Variation object, ensuring proper image dimensions. |
|
63
|
|
|
* |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
|
67
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getVariation( |
|
70
|
|
|
Field $field, |
|
71
|
|
|
VersionInfo $versionInfo, |
|
72
|
|
|
$variationName, |
|
73
|
|
|
array $parameters = [] |
|
74
|
|
|
) { |
|
75
|
|
|
/** @var \eZ\Publish\SPI\Variation\Values\ImageVariation $variation */ |
|
76
|
|
|
$variation = $this->aliasGenerator->getVariation( |
|
77
|
|
|
$field, |
|
78
|
|
|
$versionInfo, |
|
79
|
|
|
$variationName, |
|
80
|
|
|
$parameters |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
if (null === $variation->width || null === $variation->height) { |
|
84
|
|
|
$variationBinaryFile = $this->getVariationBinaryFile($field->value->id, $variationName); |
|
85
|
|
|
$image = $this->imagine->load($this->ioService->getFileContents($variationBinaryFile)); |
|
86
|
|
|
$dimensions = $image->getSize(); |
|
87
|
|
|
|
|
88
|
|
|
return new ImageVariation( |
|
89
|
|
|
[ |
|
90
|
|
|
'name' => $variation->name, |
|
91
|
|
|
'fileName' => $variation->fileName, |
|
92
|
|
|
'dirPath' => $variation->dirPath, |
|
93
|
|
|
'uri' => $variation->uri, |
|
94
|
|
|
'imageId' => $variation->imageId, |
|
95
|
|
|
'width' => $dimensions->getWidth(), |
|
96
|
|
|
'height' => $dimensions->getHeight(), |
|
97
|
|
|
'fileSize' => $variationBinaryFile->size, |
|
98
|
|
|
'mimeType' => $this->ioService->getMimeType($variationBinaryFile->id), |
|
99
|
|
|
'lastModified' => $variationBinaryFile->mtime, |
|
100
|
|
|
] |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $variation; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get image variation filesystem path. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $originalPath |
|
111
|
|
|
* @param string $variationName |
|
112
|
|
|
* |
|
113
|
|
|
* @return \eZ\Publish\Core\IO\Values\BinaryFile |
|
114
|
|
|
* |
|
115
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
|
116
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
|
117
|
|
|
*/ |
|
118
|
|
|
private function getVariationBinaryFile($originalPath, $variationName) |
|
119
|
|
|
{ |
|
120
|
|
|
if ($variationName !== IORepositoryResolver::VARIATION_ORIGINAL) { |
|
121
|
|
|
$variationPath = $this->variationPathGenerator->getVariationPath( |
|
122
|
|
|
$originalPath, |
|
123
|
|
|
$variationName |
|
124
|
|
|
); |
|
125
|
|
|
} else { |
|
126
|
|
|
$variationPath = $originalPath; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $this->ioService->loadBinaryFile($variationPath); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|