Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

ImageConverter.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
8
9 use eZ\Publish\Core\IO\IOServiceInterface;
10 use eZ\Publish\Core\IO\UrlRedecoratorInterface;
11 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
12 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
13 use eZ\Publish\SPI\Persistence\Content\FieldTypeConstraints;
14 use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
15 use eZ\Publish\SPI\Persistence\Content\FieldValue;
16
17 class ImageConverter extends BinaryFileConverter
18 {
20 private $imageIoService;
21
23 private $urlRedecorator;
24
25 public function __construct(IOServiceInterface $imageIoService, UrlRedecoratorInterface $urlRedecorator)
26 {
27 $this->imageIoService = $imageIoService;
28 $this->urlRedecorator = $urlRedecorator;
29 }
30
37 public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
38 {
39 if (isset($value->data)) {
40 // Determine what needs to be stored
41 if (isset($value->data['width']) && isset($value->data['fieldId'])) {
42 // width + field id set means that something really needs to be stored
43 $storageFieldValue->dataText = $this->createLegacyXml($value->data);
44 } elseif (isset($value->data['fieldId'])) {
45 // $fieldId without width mleans an empty field
46 $storageFieldValue->dataText = $this->createEmptyLegacyXml($value->data);
47 }
48 // otherwise the image is unprocessed and the DB field stays empty
49 // there will be a subsequent call to this method, after the image
50 // has been stored
51 }
52 }
53
61 protected function createEmptyLegacyXml($contentMetaData)
62 {
63 return $this->fillXml(
64 array_merge(
65 [
66 'uri' => '',
67 'path' => '',
68 'width' => '',
69 'height' => '',
70 'mime' => '',
71 'alternativeText' => '',
72 ],
73 $contentMetaData
74 ),
75 [
76 'basename' => '',
77 'extension' => '',
78 'dirname' => '',
79 'filename' => '',
80 ],
81 time()
82 );
83 }
84
92 protected function createLegacyXml(array $data)
93 {
94 $data['uri'] = $this->urlRedecorator->redecorateFromSource($data['uri']);
95 $pathInfo = pathinfo($data['uri']);
96
97 return $this->fillXml($data, $pathInfo, time());
98 }
99
109 protected function fillXml($imageData, $pathInfo, $timestamp)
110 {
111 // <?xml version="1.0" encoding="utf-8"
112 // <ezimage serial_number="1" is_valid="1" filename="River-Boat.jpg" suffix="jpg" basename="River-Boat" dirpath="var/ezdemo_site/storage/images/travel/peruvian-amazon/river-boat/322-1-eng-US" url="var/ezdemo_site/storage/images/travel/peruvian-amazon/river-boat/322-1-eng-US/River-Boat.jpg" original_filename="bbbbc2fe.jpg" mime_type="image/jpeg" width="770" height="512" alternative_text="Old River Boat" alias_key="1293033771" timestamp="1342530101">
113 // <original attribute_id="322" attribute_version="1" attribute_language="eng-US"/>
114 // <information Height="512" Width="770" IsColor="1"/>
115 // </ezimage>
116 $xml = <<<EOT
117 <?xml version="1.0" encoding="utf-8"?>
118 <ezimage serial_number="1" is_valid="%s" filename="%s"
119 suffix="%s" basename="%s" dirpath="%s" url="%s"
120 original_filename="%s" mime_type="%s" width="%s"
121 height="%s" alternative_text="%s" alias_key="%s" timestamp="%s">
122 <original attribute_id="%s" attribute_version="%s" attribute_language="%s"/>
123 <information Height="%s" Width="%s" IsColor="%s"/>
124 </ezimage>
125 EOT;
126
127 return sprintf(
128 $xml,
129 // <ezimage>
130 ($pathInfo['basename'] !== '' ? '1' : ''), // is_valid="%s"
131 htmlspecialchars($pathInfo['basename']), // filename="%s"
132 htmlspecialchars($pathInfo['extension']), // suffix="%s"
133 htmlspecialchars($pathInfo['filename']), // basename="%s"
134 htmlspecialchars($pathInfo['dirname']), // dirpath
135 htmlspecialchars($imageData['uri']), // url
136 htmlspecialchars($pathInfo['basename']), // @todo: Needs original file name, for whatever reason?
137 htmlspecialchars($imageData['mime']), // mime_type
138 htmlspecialchars($imageData['width']), // width
139 htmlspecialchars($imageData['height']), // height
140 htmlspecialchars($imageData['alternativeText']), // alternative_text
141 htmlspecialchars(1293033771), // alias_key, fixed for the original image
142 htmlspecialchars($timestamp), // timestamp
143 // <original>
144 $imageData['fieldId'],
145 $imageData['versionNo'],
146 $imageData['languageCode'],
147 // <information>
148 $imageData['height'], // Height
149 $imageData['width'], // Width
150 1 // IsColor @todo Do we need to fix that here?
151 );
152 }
153
160 public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
161 {
162 if (empty($value->dataText)) {
163 // Special case for anonymous user
164 return;
165 }
166 $fieldValue->data = $this->parseLegacyXml($value->dataText);
167 }
168
169 public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef): void
170 {
171 $validators = $fieldDef->fieldTypeConstraints->validators;
172
173 $storageDef->dataInt1 = $validators['FileSizeValidator']['maxFileSize'] ?? 0;
174 $storageDef->dataInt2 = (int)($validators['AlternativeTextValidator']['required'] ?? 0);
175 }
176
177 public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef): void
178 {
179 $fieldDef->fieldTypeConstraints = new FieldTypeConstraints(
180 [
181 'validators' => [
182 'FileSizeValidator' => [
183 'maxFileSize' => $storageDef->dataInt1 !== 0 ? $storageDef->dataInt1 : null,
184 ],
185 'AlternativeTextValidator' => [
186 'required' => (bool)$storageDef->dataInt2,
187 ],
188 ],
189 ]
190 );
191 }
192
202 protected function parseLegacyXml($xml)
203 {
204 $extractedData = [];
205
206 $dom = new \DOMDocument();
207 $dom->loadXml($xml);
208
209 $ezimageTag = $dom->documentElement;
210
211 if (!$ezimageTag->hasAttribute('url')) {
212 throw new \RuntimeException('Missing attribute "url" in <ezimage/> tag.');
213 }
214
215 if (($legacyUrl = $ezimageTag->getAttribute('url')) === '') {
216 // Detected XML considered "empty" by the legacy storage
217 return null;
218 }
219
220 $url = $this->urlRedecorator->redecorateFromTarget($legacyUrl);
221 $extractedData['id'] = $this->imageIoService->loadBinaryFileByUri($url)->id;
222
223 if (!$ezimageTag->hasAttribute('filename')) {
224 throw new \RuntimeException('Missing attribute "filename" in <ezimage/> tag.');
225 }
226 $extractedData['fileName'] = $ezimageTag->getAttribute('filename');
227 $extractedData['width'] = $ezimageTag->getAttribute('width');
228 $extractedData['height'] = $ezimageTag->getAttribute('height');
229 $extractedData['mime'] = $ezimageTag->getAttribute('mime_type');
230
231 if (!$ezimageTag->hasAttribute('alternative_text')) {
232 throw new \RuntimeException('Missing attribute "alternative_text" in <ezimage/> tag.');
233 }
234 $extractedData['alternativeText'] = $ezimageTag->getAttribute('alternative_text');
235
236 return $extractedData;
237 }
238 }
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\BinaryFileConverter
Definition: BinaryFileConverter.php:17
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter
Definition: ImageConverter.php:18
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\toStorageValue
toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
Converts data from $value to $storageFieldValue.
Definition: ImageConverter.php:37
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\fillXml
fillXml($imageData, $pathInfo, $timestamp)
Fill the XML template with the data provided.
Definition: ImageConverter.php:109
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\createEmptyLegacyXml
createEmptyLegacyXml($contentMetaData)
Creates an XML considered "empty" by the legacy storage.
Definition: ImageConverter.php:61
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\toFieldDefinition
toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
Converts field definition data in $storageDef into $fieldDef.
Definition: ImageConverter.php:177
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\$imageIoService
$imageIoService
Definition: ImageConverter.php:20
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\toStorageFieldDefinition
toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
Converts field definition data in $fieldDef into $storageFieldDef.
Definition: ImageConverter.php:169
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\createLegacyXml
createLegacyXml(array $data)
Returns the XML required by the legacy database.
Definition: ImageConverter.php:92
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\parseLegacyXml
parseLegacyXml($xml)
Parses the XML from the legacy database.
Definition: ImageConverter.php:202
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\$urlRedecorator
$urlRedecorator
Definition: ImageConverter.php:23
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\toFieldValue
toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
Converts data from $value to $fieldValue.
Definition: ImageConverter.php:160
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter\__construct
__construct(IOServiceInterface $imageIoService, UrlRedecoratorInterface $urlRedecorator)
Definition: ImageConverter.php:25
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition
Definition: StorageFieldDefinition.php:12
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue
Definition: StorageFieldValue.php:12
eZ\Publish\SPI\Persistence\Content\FieldTypeConstraints
Definition: FieldTypeConstraints.php:12
eZ\Publish\SPI\Persistence\Content\FieldValue
Definition: FieldValue.php:12
eZ\Publish\SPI\Persistence\Content\FieldValue\$data
$data
Definition: FieldValue.php:23
eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
Definition: SPI/Persistence/Content/Type/FieldDefinition.php:18
eZ\Publish\Core\IO\IOServiceInterface
Interface for Input/Output handling of binary files.
Definition: IOServiceInterface.php:16
eZ\Publish\Core\IO\UrlRedecoratorInterface
Converts an URL from one decorator to another.
Definition: UrlRedecoratorInterface.php:28
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter
Definition: AuthorConverter.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.