Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\image\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\Cache;
|
Chris@0
|
6 use Drupal\Core\Config\Entity\ConfigEntityBase;
|
Chris@4
|
7 use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
|
Chris@0
|
10 use Drupal\Core\Routing\RequestHelper;
|
Chris@0
|
11 use Drupal\Core\Site\Settings;
|
Chris@0
|
12 use Drupal\Core\Url;
|
Chris@0
|
13 use Drupal\image\ImageEffectPluginCollection;
|
Chris@0
|
14 use Drupal\image\ImageEffectInterface;
|
Chris@0
|
15 use Drupal\image\ImageStyleInterface;
|
Chris@0
|
16 use Drupal\Component\Utility\Crypt;
|
Chris@0
|
17 use Drupal\Component\Utility\UrlHelper;
|
Chris@0
|
18 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
|
Chris@0
|
19 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
Chris@0
|
20 use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Defines an image style configuration entity.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @ConfigEntityType(
|
Chris@0
|
26 * id = "image_style",
|
Chris@0
|
27 * label = @Translation("Image style"),
|
Chris@4
|
28 * label_collection = @Translation("Image styles"),
|
Chris@4
|
29 * label_singular = @Translation("image style"),
|
Chris@4
|
30 * label_plural = @Translation("image styles"),
|
Chris@4
|
31 * label_count = @PluralTranslation(
|
Chris@4
|
32 * singular = "@count image style",
|
Chris@4
|
33 * plural = "@count image styles",
|
Chris@4
|
34 * ),
|
Chris@0
|
35 * handlers = {
|
Chris@0
|
36 * "form" = {
|
Chris@0
|
37 * "add" = "Drupal\image\Form\ImageStyleAddForm",
|
Chris@0
|
38 * "edit" = "Drupal\image\Form\ImageStyleEditForm",
|
Chris@0
|
39 * "delete" = "Drupal\image\Form\ImageStyleDeleteForm",
|
Chris@0
|
40 * "flush" = "Drupal\image\Form\ImageStyleFlushForm"
|
Chris@0
|
41 * },
|
Chris@0
|
42 * "list_builder" = "Drupal\image\ImageStyleListBuilder",
|
Chris@0
|
43 * "storage" = "Drupal\image\ImageStyleStorage",
|
Chris@0
|
44 * },
|
Chris@0
|
45 * admin_permission = "administer image styles",
|
Chris@0
|
46 * config_prefix = "style",
|
Chris@0
|
47 * entity_keys = {
|
Chris@0
|
48 * "id" = "name",
|
Chris@0
|
49 * "label" = "label"
|
Chris@0
|
50 * },
|
Chris@0
|
51 * links = {
|
Chris@0
|
52 * "flush-form" = "/admin/config/media/image-styles/manage/{image_style}/flush",
|
Chris@0
|
53 * "edit-form" = "/admin/config/media/image-styles/manage/{image_style}",
|
Chris@0
|
54 * "delete-form" = "/admin/config/media/image-styles/manage/{image_style}/delete",
|
Chris@0
|
55 * "collection" = "/admin/config/media/image-styles",
|
Chris@0
|
56 * },
|
Chris@0
|
57 * config_export = {
|
Chris@0
|
58 * "name",
|
Chris@0
|
59 * "label",
|
Chris@0
|
60 * "effects",
|
Chris@0
|
61 * }
|
Chris@0
|
62 * )
|
Chris@0
|
63 */
|
Chris@0
|
64 class ImageStyle extends ConfigEntityBase implements ImageStyleInterface, EntityWithPluginCollectionInterface {
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * The name of the image style.
|
Chris@0
|
68 *
|
Chris@0
|
69 * @var string
|
Chris@0
|
70 */
|
Chris@0
|
71 protected $name;
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * The image style label.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @var string
|
Chris@0
|
77 */
|
Chris@0
|
78 protected $label;
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * The array of image effects for this image style.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @var array
|
Chris@0
|
84 */
|
Chris@0
|
85 protected $effects = [];
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * Holds the collection of image effects that are used by this image style.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @var \Drupal\image\ImageEffectPluginCollection
|
Chris@0
|
91 */
|
Chris@0
|
92 protected $effectsCollection;
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * {@inheritdoc}
|
Chris@0
|
96 */
|
Chris@0
|
97 public function id() {
|
Chris@0
|
98 return $this->name;
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * {@inheritdoc}
|
Chris@0
|
103 */
|
Chris@0
|
104 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
Chris@0
|
105 parent::postSave($storage, $update);
|
Chris@0
|
106
|
Chris@0
|
107 if ($update) {
|
Chris@0
|
108 if (!empty($this->original) && $this->id() !== $this->original->id()) {
|
Chris@0
|
109 // The old image style name needs flushing after a rename.
|
Chris@0
|
110 $this->original->flush();
|
Chris@0
|
111 // Update field settings if necessary.
|
Chris@0
|
112 if (!$this->isSyncing()) {
|
Chris@0
|
113 static::replaceImageStyle($this);
|
Chris@0
|
114 }
|
Chris@0
|
115 }
|
Chris@0
|
116 else {
|
Chris@0
|
117 // Flush image style when updating without changing the name.
|
Chris@0
|
118 $this->flush();
|
Chris@0
|
119 }
|
Chris@0
|
120 }
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * {@inheritdoc}
|
Chris@0
|
125 */
|
Chris@0
|
126 public static function postDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@0
|
127 parent::postDelete($storage, $entities);
|
Chris@0
|
128
|
Chris@0
|
129 /** @var \Drupal\image\ImageStyleInterface[] $entities */
|
Chris@0
|
130 foreach ($entities as $style) {
|
Chris@0
|
131 // Flush cached media for the deleted style.
|
Chris@0
|
132 $style->flush();
|
Chris@0
|
133 // Clear the replacement ID, if one has been previously stored.
|
Chris@0
|
134 /** @var \Drupal\image\ImageStyleStorageInterface $storage */
|
Chris@0
|
135 $storage->clearReplacementId($style->id());
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * Update field settings if the image style name is changed.
|
Chris@0
|
141 *
|
Chris@0
|
142 * @param \Drupal\image\ImageStyleInterface $style
|
Chris@0
|
143 * The image style.
|
Chris@0
|
144 */
|
Chris@0
|
145 protected static function replaceImageStyle(ImageStyleInterface $style) {
|
Chris@0
|
146 if ($style->id() != $style->getOriginalId()) {
|
Chris@0
|
147 // Loop through all entity displays looking for formatters / widgets using
|
Chris@0
|
148 // the image style.
|
Chris@0
|
149 foreach (EntityViewDisplay::loadMultiple() as $display) {
|
Chris@0
|
150 foreach ($display->getComponents() as $name => $options) {
|
Chris@0
|
151 if (isset($options['type']) && $options['type'] == 'image' && $options['settings']['image_style'] == $style->getOriginalId()) {
|
Chris@0
|
152 $options['settings']['image_style'] = $style->id();
|
Chris@0
|
153 $display->setComponent($name, $options)
|
Chris@0
|
154 ->save();
|
Chris@0
|
155 }
|
Chris@0
|
156 }
|
Chris@0
|
157 }
|
Chris@4
|
158 foreach (EntityFormDisplay::loadMultiple() as $display) {
|
Chris@0
|
159 foreach ($display->getComponents() as $name => $options) {
|
Chris@0
|
160 if (isset($options['type']) && $options['type'] == 'image_image' && $options['settings']['preview_image_style'] == $style->getOriginalId()) {
|
Chris@0
|
161 $options['settings']['preview_image_style'] = $style->id();
|
Chris@0
|
162 $display->setComponent($name, $options)
|
Chris@0
|
163 ->save();
|
Chris@0
|
164 }
|
Chris@0
|
165 }
|
Chris@0
|
166 }
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 /**
|
Chris@0
|
171 * {@inheritdoc}
|
Chris@0
|
172 */
|
Chris@0
|
173 public function buildUri($uri) {
|
Chris@0
|
174 $source_scheme = $scheme = $this->fileUriScheme($uri);
|
Chris@0
|
175 $default_scheme = $this->fileDefaultScheme();
|
Chris@0
|
176
|
Chris@0
|
177 if ($source_scheme) {
|
Chris@0
|
178 $path = $this->fileUriTarget($uri);
|
Chris@0
|
179 // The scheme of derivative image files only needs to be computed for
|
Chris@0
|
180 // source files not stored in the default scheme.
|
Chris@0
|
181 if ($source_scheme != $default_scheme) {
|
Chris@0
|
182 $class = $this->getStreamWrapperManager()->getClass($source_scheme);
|
Chris@0
|
183 $is_writable = $class::getType() & StreamWrapperInterface::WRITE;
|
Chris@0
|
184
|
Chris@0
|
185 // Compute the derivative URI scheme. Derivatives created from writable
|
Chris@0
|
186 // source stream wrappers will inherit the scheme. Derivatives created
|
Chris@0
|
187 // from read-only stream wrappers will fall-back to the default scheme.
|
Chris@0
|
188 $scheme = $is_writable ? $source_scheme : $default_scheme;
|
Chris@0
|
189 }
|
Chris@0
|
190 }
|
Chris@0
|
191 else {
|
Chris@0
|
192 $path = $uri;
|
Chris@0
|
193 $source_scheme = $scheme = $default_scheme;
|
Chris@0
|
194 }
|
Chris@0
|
195 return "$scheme://styles/{$this->id()}/$source_scheme/{$this->addExtension($path)}";
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 /**
|
Chris@0
|
199 * {@inheritdoc}
|
Chris@0
|
200 */
|
Chris@0
|
201 public function buildUrl($path, $clean_urls = NULL) {
|
Chris@0
|
202 $uri = $this->buildUri($path);
|
Chris@0
|
203 // The token query is added even if the
|
Chris@0
|
204 // 'image.settings:allow_insecure_derivatives' configuration is TRUE, so
|
Chris@0
|
205 // that the emitted links remain valid if it is changed back to the default
|
Chris@0
|
206 // FALSE. However, sites which need to prevent the token query from being
|
Chris@0
|
207 // emitted at all can additionally set the
|
Chris@0
|
208 // 'image.settings:suppress_itok_output' configuration to TRUE to achieve
|
Chris@0
|
209 // that (if both are set, the security token will neither be emitted in the
|
Chris@0
|
210 // image derivative URL nor checked for in
|
Chris@0
|
211 // \Drupal\image\ImageStyleInterface::deliver()).
|
Chris@0
|
212 $token_query = [];
|
Chris@0
|
213 if (!\Drupal::config('image.settings')->get('suppress_itok_output')) {
|
Chris@0
|
214 // The passed $path variable can be either a relative path or a full URI.
|
Chris@0
|
215 $original_uri = file_uri_scheme($path) ? file_stream_wrapper_uri_normalize($path) : file_build_uri($path);
|
Chris@0
|
216 $token_query = [IMAGE_DERIVATIVE_TOKEN => $this->getPathToken($original_uri)];
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 if ($clean_urls === NULL) {
|
Chris@0
|
220 // Assume clean URLs unless the request tells us otherwise.
|
Chris@0
|
221 $clean_urls = TRUE;
|
Chris@0
|
222 try {
|
Chris@0
|
223 $request = \Drupal::request();
|
Chris@0
|
224 $clean_urls = RequestHelper::isCleanUrl($request);
|
Chris@0
|
225 }
|
Chris@0
|
226 catch (ServiceNotFoundException $e) {
|
Chris@0
|
227 }
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@0
|
230 // If not using clean URLs, the image derivative callback is only available
|
Chris@0
|
231 // with the script path. If the file does not exist, use Url::fromUri() to
|
Chris@0
|
232 // ensure that it is included. Once the file exists it's fine to fall back
|
Chris@0
|
233 // to the actual file path, this avoids bootstrapping PHP once the files are
|
Chris@0
|
234 // built.
|
Chris@0
|
235 if ($clean_urls === FALSE && file_uri_scheme($uri) == 'public' && !file_exists($uri)) {
|
Chris@0
|
236 $directory_path = $this->getStreamWrapperManager()->getViaUri($uri)->getDirectoryPath();
|
Chris@0
|
237 return Url::fromUri('base:' . $directory_path . '/' . file_uri_target($uri), ['absolute' => TRUE, 'query' => $token_query])->toString();
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 $file_url = file_create_url($uri);
|
Chris@0
|
241 // Append the query string with the token, if necessary.
|
Chris@0
|
242 if ($token_query) {
|
Chris@0
|
243 $file_url .= (strpos($file_url, '?') !== FALSE ? '&' : '?') . UrlHelper::buildQuery($token_query);
|
Chris@0
|
244 }
|
Chris@0
|
245
|
Chris@0
|
246 return $file_url;
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@0
|
249 /**
|
Chris@0
|
250 * {@inheritdoc}
|
Chris@0
|
251 */
|
Chris@0
|
252 public function flush($path = NULL) {
|
Chris@0
|
253 // A specific image path has been provided. Flush only that derivative.
|
Chris@0
|
254 if (isset($path)) {
|
Chris@0
|
255 $derivative_uri = $this->buildUri($path);
|
Chris@0
|
256 if (file_exists($derivative_uri)) {
|
Chris@0
|
257 file_unmanaged_delete($derivative_uri);
|
Chris@0
|
258 }
|
Chris@0
|
259 return $this;
|
Chris@0
|
260 }
|
Chris@0
|
261
|
Chris@0
|
262 // Delete the style directory in each registered wrapper.
|
Chris@0
|
263 $wrappers = $this->getStreamWrapperManager()->getWrappers(StreamWrapperInterface::WRITE_VISIBLE);
|
Chris@0
|
264 foreach ($wrappers as $wrapper => $wrapper_data) {
|
Chris@0
|
265 if (file_exists($directory = $wrapper . '://styles/' . $this->id())) {
|
Chris@0
|
266 file_unmanaged_delete_recursive($directory);
|
Chris@0
|
267 }
|
Chris@0
|
268 }
|
Chris@0
|
269
|
Chris@0
|
270 // Let other modules update as necessary on flush.
|
Chris@0
|
271 $module_handler = \Drupal::moduleHandler();
|
Chris@0
|
272 $module_handler->invokeAll('image_style_flush', [$this]);
|
Chris@0
|
273
|
Chris@0
|
274 // Clear caches so that formatters may be added for this style.
|
Chris@0
|
275 drupal_theme_rebuild();
|
Chris@0
|
276
|
Chris@0
|
277 Cache::invalidateTags($this->getCacheTagsToInvalidate());
|
Chris@0
|
278
|
Chris@0
|
279 return $this;
|
Chris@0
|
280 }
|
Chris@0
|
281
|
Chris@0
|
282 /**
|
Chris@0
|
283 * {@inheritdoc}
|
Chris@0
|
284 */
|
Chris@0
|
285 public function createDerivative($original_uri, $derivative_uri) {
|
Chris@0
|
286 // If the source file doesn't exist, return FALSE without creating folders.
|
Chris@0
|
287 $image = $this->getImageFactory()->get($original_uri);
|
Chris@0
|
288 if (!$image->isValid()) {
|
Chris@0
|
289 return FALSE;
|
Chris@0
|
290 }
|
Chris@0
|
291
|
Chris@0
|
292 // Get the folder for the final location of this style.
|
Chris@0
|
293 $directory = drupal_dirname($derivative_uri);
|
Chris@0
|
294
|
Chris@0
|
295 // Build the destination folder tree if it doesn't already exist.
|
Chris@0
|
296 if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
|
Chris@0
|
297 \Drupal::logger('image')->error('Failed to create style directory: %directory', ['%directory' => $directory]);
|
Chris@0
|
298 return FALSE;
|
Chris@0
|
299 }
|
Chris@0
|
300
|
Chris@0
|
301 foreach ($this->getEffects() as $effect) {
|
Chris@0
|
302 $effect->applyEffect($image);
|
Chris@0
|
303 }
|
Chris@0
|
304
|
Chris@0
|
305 if (!$image->save($derivative_uri)) {
|
Chris@0
|
306 if (file_exists($derivative_uri)) {
|
Chris@0
|
307 \Drupal::logger('image')->error('Cached image file %destination already exists. There may be an issue with your rewrite configuration.', ['%destination' => $derivative_uri]);
|
Chris@0
|
308 }
|
Chris@0
|
309 return FALSE;
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 return TRUE;
|
Chris@0
|
313 }
|
Chris@0
|
314
|
Chris@0
|
315 /**
|
Chris@0
|
316 * {@inheritdoc}
|
Chris@0
|
317 */
|
Chris@0
|
318 public function transformDimensions(array &$dimensions, $uri) {
|
Chris@0
|
319 foreach ($this->getEffects() as $effect) {
|
Chris@0
|
320 $effect->transformDimensions($dimensions, $uri);
|
Chris@0
|
321 }
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 /**
|
Chris@0
|
325 * {@inheritdoc}
|
Chris@0
|
326 */
|
Chris@0
|
327 public function getDerivativeExtension($extension) {
|
Chris@0
|
328 foreach ($this->getEffects() as $effect) {
|
Chris@0
|
329 $extension = $effect->getDerivativeExtension($extension);
|
Chris@0
|
330 }
|
Chris@0
|
331 return $extension;
|
Chris@0
|
332 }
|
Chris@0
|
333
|
Chris@0
|
334 /**
|
Chris@0
|
335 * {@inheritdoc}
|
Chris@0
|
336 */
|
Chris@0
|
337 public function getPathToken($uri) {
|
Chris@0
|
338 // Return the first 8 characters.
|
Chris@0
|
339 return substr(Crypt::hmacBase64($this->id() . ':' . $this->addExtension($uri), $this->getPrivateKey() . $this->getHashSalt()), 0, 8);
|
Chris@0
|
340 }
|
Chris@0
|
341
|
Chris@0
|
342 /**
|
Chris@0
|
343 * {@inheritdoc}
|
Chris@0
|
344 */
|
Chris@0
|
345 public function deleteImageEffect(ImageEffectInterface $effect) {
|
Chris@0
|
346 $this->getEffects()->removeInstanceId($effect->getUuid());
|
Chris@0
|
347 $this->save();
|
Chris@0
|
348 return $this;
|
Chris@0
|
349 }
|
Chris@0
|
350
|
Chris@0
|
351 /**
|
Chris@0
|
352 * {@inheritdoc}
|
Chris@0
|
353 */
|
Chris@0
|
354 public function supportsUri($uri) {
|
Chris@0
|
355 // Only support the URI if its extension is supported by the current image
|
Chris@0
|
356 // toolkit.
|
Chris@0
|
357 return in_array(
|
Chris@4
|
358 mb_strtolower(pathinfo($uri, PATHINFO_EXTENSION)),
|
Chris@0
|
359 $this->getImageFactory()->getSupportedExtensions()
|
Chris@0
|
360 );
|
Chris@0
|
361 }
|
Chris@0
|
362
|
Chris@0
|
363 /**
|
Chris@0
|
364 * {@inheritdoc}
|
Chris@0
|
365 */
|
Chris@0
|
366 public function getEffect($effect) {
|
Chris@0
|
367 return $this->getEffects()->get($effect);
|
Chris@0
|
368 }
|
Chris@0
|
369
|
Chris@0
|
370 /**
|
Chris@0
|
371 * {@inheritdoc}
|
Chris@0
|
372 */
|
Chris@0
|
373 public function getEffects() {
|
Chris@0
|
374 if (!$this->effectsCollection) {
|
Chris@0
|
375 $this->effectsCollection = new ImageEffectPluginCollection($this->getImageEffectPluginManager(), $this->effects);
|
Chris@0
|
376 $this->effectsCollection->sort();
|
Chris@0
|
377 }
|
Chris@0
|
378 return $this->effectsCollection;
|
Chris@0
|
379 }
|
Chris@0
|
380
|
Chris@0
|
381 /**
|
Chris@0
|
382 * {@inheritdoc}
|
Chris@0
|
383 */
|
Chris@0
|
384 public function getPluginCollections() {
|
Chris@0
|
385 return ['effects' => $this->getEffects()];
|
Chris@0
|
386 }
|
Chris@0
|
387
|
Chris@0
|
388 /**
|
Chris@0
|
389 * {@inheritdoc}
|
Chris@0
|
390 */
|
Chris@0
|
391 public function addImageEffect(array $configuration) {
|
Chris@0
|
392 $configuration['uuid'] = $this->uuidGenerator()->generate();
|
Chris@0
|
393 $this->getEffects()->addInstanceId($configuration['uuid'], $configuration);
|
Chris@0
|
394 return $configuration['uuid'];
|
Chris@0
|
395 }
|
Chris@0
|
396
|
Chris@0
|
397 /**
|
Chris@0
|
398 * {@inheritdoc}
|
Chris@0
|
399 */
|
Chris@0
|
400 public function getReplacementID() {
|
Chris@0
|
401 /** @var \Drupal\image\ImageStyleStorageInterface $storage */
|
Chris@0
|
402 $storage = $this->entityTypeManager()->getStorage($this->getEntityTypeId());
|
Chris@0
|
403 return $storage->getReplacementId($this->id());
|
Chris@0
|
404 }
|
Chris@0
|
405
|
Chris@0
|
406 /**
|
Chris@0
|
407 * {@inheritdoc}
|
Chris@0
|
408 */
|
Chris@0
|
409 public function getName() {
|
Chris@0
|
410 return $this->get('name');
|
Chris@0
|
411 }
|
Chris@0
|
412
|
Chris@0
|
413 /**
|
Chris@0
|
414 * {@inheritdoc}
|
Chris@0
|
415 */
|
Chris@0
|
416 public function setName($name) {
|
Chris@0
|
417 $this->set('name', $name);
|
Chris@0
|
418 return $this;
|
Chris@0
|
419 }
|
Chris@0
|
420
|
Chris@0
|
421 /**
|
Chris@0
|
422 * Returns the image effect plugin manager.
|
Chris@0
|
423 *
|
Chris@0
|
424 * @return \Drupal\Component\Plugin\PluginManagerInterface
|
Chris@0
|
425 * The image effect plugin manager.
|
Chris@0
|
426 */
|
Chris@0
|
427 protected function getImageEffectPluginManager() {
|
Chris@0
|
428 return \Drupal::service('plugin.manager.image.effect');
|
Chris@0
|
429 }
|
Chris@0
|
430
|
Chris@0
|
431 /**
|
Chris@0
|
432 * Returns the image factory.
|
Chris@0
|
433 *
|
Chris@0
|
434 * @return \Drupal\Core\Image\ImageFactory
|
Chris@0
|
435 * The image factory.
|
Chris@0
|
436 */
|
Chris@0
|
437 protected function getImageFactory() {
|
Chris@0
|
438 return \Drupal::service('image.factory');
|
Chris@0
|
439 }
|
Chris@0
|
440
|
Chris@0
|
441 /**
|
Chris@0
|
442 * Gets the Drupal private key.
|
Chris@0
|
443 *
|
Chris@0
|
444 * @return string
|
Chris@0
|
445 * The Drupal private key.
|
Chris@0
|
446 */
|
Chris@0
|
447 protected function getPrivateKey() {
|
Chris@0
|
448 return \Drupal::service('private_key')->get();
|
Chris@0
|
449 }
|
Chris@0
|
450
|
Chris@0
|
451 /**
|
Chris@0
|
452 * Gets a salt useful for hardening against SQL injection.
|
Chris@0
|
453 *
|
Chris@0
|
454 * @return string
|
Chris@0
|
455 * A salt based on information in settings.php, not in the database.
|
Chris@0
|
456 *
|
Chris@0
|
457 * @throws \RuntimeException
|
Chris@0
|
458 */
|
Chris@0
|
459 protected function getHashSalt() {
|
Chris@0
|
460 return Settings::getHashSalt();
|
Chris@0
|
461 }
|
Chris@0
|
462
|
Chris@0
|
463 /**
|
Chris@0
|
464 * Adds an extension to a path.
|
Chris@0
|
465 *
|
Chris@0
|
466 * If this image style changes the extension of the derivative, this method
|
Chris@0
|
467 * adds the new extension to the given path. This way we avoid filename
|
Chris@0
|
468 * clashes while still allowing us to find the source image.
|
Chris@0
|
469 *
|
Chris@0
|
470 * @param string $path
|
Chris@0
|
471 * The path to add the extension to.
|
Chris@0
|
472 *
|
Chris@0
|
473 * @return string
|
Chris@0
|
474 * The given path if this image style doesn't change its extension, or the
|
Chris@0
|
475 * path with the added extension if it does.
|
Chris@0
|
476 */
|
Chris@0
|
477 protected function addExtension($path) {
|
Chris@0
|
478 $original_extension = pathinfo($path, PATHINFO_EXTENSION);
|
Chris@0
|
479 $extension = $this->getDerivativeExtension($original_extension);
|
Chris@0
|
480 if ($original_extension !== $extension) {
|
Chris@0
|
481 $path .= '.' . $extension;
|
Chris@0
|
482 }
|
Chris@0
|
483 return $path;
|
Chris@0
|
484 }
|
Chris@0
|
485
|
Chris@0
|
486 /**
|
Chris@0
|
487 * Provides a wrapper for file_uri_scheme() to allow unit testing.
|
Chris@0
|
488 *
|
Chris@0
|
489 * Returns the scheme of a URI (e.g. a stream).
|
Chris@0
|
490 *
|
Chris@0
|
491 * @param string $uri
|
Chris@0
|
492 * A stream, referenced as "scheme://target" or "data:target".
|
Chris@0
|
493 *
|
Chris@0
|
494 * @see file_uri_target()
|
Chris@0
|
495 *
|
Chris@0
|
496 * @todo: Remove when https://www.drupal.org/node/2050759 is in.
|
Chris@0
|
497 *
|
Chris@0
|
498 * @return string
|
Chris@0
|
499 * A string containing the name of the scheme, or FALSE if none. For
|
Chris@0
|
500 * example, the URI "public://example.txt" would return "public".
|
Chris@0
|
501 */
|
Chris@0
|
502 protected function fileUriScheme($uri) {
|
Chris@0
|
503 return file_uri_scheme($uri);
|
Chris@0
|
504 }
|
Chris@0
|
505
|
Chris@0
|
506 /**
|
Chris@0
|
507 * Provides a wrapper for file_uri_target() to allow unit testing.
|
Chris@0
|
508 *
|
Chris@0
|
509 * Returns the part of a URI after the schema.
|
Chris@0
|
510 *
|
Chris@0
|
511 * @param string $uri
|
Chris@0
|
512 * A stream, referenced as "scheme://target" or "data:target".
|
Chris@0
|
513 *
|
Chris@0
|
514 * @see file_uri_scheme()
|
Chris@0
|
515 *
|
Chris@0
|
516 * @todo: Convert file_uri_target() into a proper injectable service.
|
Chris@0
|
517 *
|
Chris@0
|
518 * @return string|bool
|
Chris@0
|
519 * A string containing the target (path), or FALSE if none.
|
Chris@0
|
520 * For example, the URI "public://sample/test.txt" would return
|
Chris@0
|
521 * "sample/test.txt".
|
Chris@0
|
522 */
|
Chris@0
|
523 protected function fileUriTarget($uri) {
|
Chris@0
|
524 return file_uri_target($uri);
|
Chris@0
|
525 }
|
Chris@0
|
526
|
Chris@0
|
527 /**
|
Chris@0
|
528 * Provides a wrapper for file_default_scheme() to allow unit testing.
|
Chris@0
|
529 *
|
Chris@0
|
530 * Gets the default file stream implementation.
|
Chris@0
|
531 *
|
Chris@0
|
532 * @todo: Convert file_default_scheme() into a proper injectable service.
|
Chris@0
|
533 *
|
Chris@0
|
534 * @return string
|
Chris@0
|
535 * 'public', 'private' or any other file scheme defined as the default.
|
Chris@0
|
536 */
|
Chris@0
|
537 protected function fileDefaultScheme() {
|
Chris@0
|
538 return file_default_scheme();
|
Chris@0
|
539 }
|
Chris@0
|
540
|
Chris@0
|
541 /**
|
Chris@0
|
542 * Gets the stream wrapper manager service.
|
Chris@0
|
543 *
|
Chris@0
|
544 * @return \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
|
Chris@0
|
545 * The stream wrapper manager service
|
Chris@0
|
546 *
|
Chris@0
|
547 * @todo Properly inject this service in Drupal 9.0.x.
|
Chris@0
|
548 */
|
Chris@0
|
549 protected function getStreamWrapperManager() {
|
Chris@0
|
550 return \Drupal::service('stream_wrapper_manager');
|
Chris@0
|
551 }
|
Chris@0
|
552
|
Chris@0
|
553 }
|