3 namespace Drupal\image\Entity;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Config\Entity\ConfigEntityBase;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
9 use Drupal\Core\Routing\RequestHelper;
10 use Drupal\Core\Site\Settings;
12 use Drupal\image\ImageEffectPluginCollection;
13 use Drupal\image\ImageEffectInterface;
14 use Drupal\image\ImageStyleInterface;
15 use Drupal\Component\Utility\Crypt;
16 use Drupal\Component\Utility\UrlHelper;
17 use Drupal\Component\Utility\Unicode;
18 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
19 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
20 use Drupal\Core\Entity\Entity\EntityViewDisplay;
23 * Defines an image style configuration entity.
27 * label = @Translation("Image style"),
30 * "add" = "Drupal\image\Form\ImageStyleAddForm",
31 * "edit" = "Drupal\image\Form\ImageStyleEditForm",
32 * "delete" = "Drupal\image\Form\ImageStyleDeleteForm",
33 * "flush" = "Drupal\image\Form\ImageStyleFlushForm"
35 * "list_builder" = "Drupal\image\ImageStyleListBuilder",
36 * "storage" = "Drupal\image\ImageStyleStorage",
38 * admin_permission = "administer image styles",
39 * config_prefix = "style",
45 * "flush-form" = "/admin/config/media/image-styles/manage/{image_style}/flush",
46 * "edit-form" = "/admin/config/media/image-styles/manage/{image_style}",
47 * "delete-form" = "/admin/config/media/image-styles/manage/{image_style}/delete",
48 * "collection" = "/admin/config/media/image-styles",
57 class ImageStyle extends ConfigEntityBase implements ImageStyleInterface, EntityWithPluginCollectionInterface {
60 * The name of the image style.
67 * The image style label.
74 * The array of image effects for this image style.
78 protected $effects = [];
81 * Holds the collection of image effects that are used by this image style.
83 * @var \Drupal\image\ImageEffectPluginCollection
85 protected $effectsCollection;
90 public function id() {
97 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
98 parent::postSave($storage, $update);
101 if (!empty($this->original) && $this->id() !== $this->original->id()) {
102 // The old image style name needs flushing after a rename.
103 $this->original->flush();
104 // Update field settings if necessary.
105 if (!$this->isSyncing()) {
106 static::replaceImageStyle($this);
110 // Flush image style when updating without changing the name.
119 public static function postDelete(EntityStorageInterface $storage, array $entities) {
120 parent::postDelete($storage, $entities);
122 /** @var \Drupal\image\ImageStyleInterface[] $entities */
123 foreach ($entities as $style) {
124 // Flush cached media for the deleted style.
126 // Clear the replacement ID, if one has been previously stored.
127 /** @var \Drupal\image\ImageStyleStorageInterface $storage */
128 $storage->clearReplacementId($style->id());
133 * Update field settings if the image style name is changed.
135 * @param \Drupal\image\ImageStyleInterface $style
138 protected static function replaceImageStyle(ImageStyleInterface $style) {
139 if ($style->id() != $style->getOriginalId()) {
140 // Loop through all entity displays looking for formatters / widgets using
142 foreach (EntityViewDisplay::loadMultiple() as $display) {
143 foreach ($display->getComponents() as $name => $options) {
144 if (isset($options['type']) && $options['type'] == 'image' && $options['settings']['image_style'] == $style->getOriginalId()) {
145 $options['settings']['image_style'] = $style->id();
146 $display->setComponent($name, $options)
151 foreach (EntityViewDisplay::loadMultiple() as $display) {
152 foreach ($display->getComponents() as $name => $options) {
153 if (isset($options['type']) && $options['type'] == 'image_image' && $options['settings']['preview_image_style'] == $style->getOriginalId()) {
154 $options['settings']['preview_image_style'] = $style->id();
155 $display->setComponent($name, $options)
166 public function buildUri($uri) {
167 $source_scheme = $scheme = $this->fileUriScheme($uri);
168 $default_scheme = $this->fileDefaultScheme();
170 if ($source_scheme) {
171 $path = $this->fileUriTarget($uri);
172 // The scheme of derivative image files only needs to be computed for
173 // source files not stored in the default scheme.
174 if ($source_scheme != $default_scheme) {
175 $class = $this->getStreamWrapperManager()->getClass($source_scheme);
176 $is_writable = $class::getType() & StreamWrapperInterface::WRITE;
178 // Compute the derivative URI scheme. Derivatives created from writable
179 // source stream wrappers will inherit the scheme. Derivatives created
180 // from read-only stream wrappers will fall-back to the default scheme.
181 $scheme = $is_writable ? $source_scheme : $default_scheme;
186 $source_scheme = $scheme = $default_scheme;
188 return "$scheme://styles/{$this->id()}/$source_scheme/{$this->addExtension($path)}";
194 public function buildUrl($path, $clean_urls = NULL) {
195 $uri = $this->buildUri($path);
196 // The token query is added even if the
197 // 'image.settings:allow_insecure_derivatives' configuration is TRUE, so
198 // that the emitted links remain valid if it is changed back to the default
199 // FALSE. However, sites which need to prevent the token query from being
200 // emitted at all can additionally set the
201 // 'image.settings:suppress_itok_output' configuration to TRUE to achieve
202 // that (if both are set, the security token will neither be emitted in the
203 // image derivative URL nor checked for in
204 // \Drupal\image\ImageStyleInterface::deliver()).
206 if (!\Drupal::config('image.settings')->get('suppress_itok_output')) {
207 // The passed $path variable can be either a relative path or a full URI.
208 $original_uri = file_uri_scheme($path) ? file_stream_wrapper_uri_normalize($path) : file_build_uri($path);
209 $token_query = [IMAGE_DERIVATIVE_TOKEN => $this->getPathToken($original_uri)];
212 if ($clean_urls === NULL) {
213 // Assume clean URLs unless the request tells us otherwise.
216 $request = \Drupal::request();
217 $clean_urls = RequestHelper::isCleanUrl($request);
219 catch (ServiceNotFoundException $e) {
223 // If not using clean URLs, the image derivative callback is only available
224 // with the script path. If the file does not exist, use Url::fromUri() to
225 // ensure that it is included. Once the file exists it's fine to fall back
226 // to the actual file path, this avoids bootstrapping PHP once the files are
228 if ($clean_urls === FALSE && file_uri_scheme($uri) == 'public' && !file_exists($uri)) {
229 $directory_path = $this->getStreamWrapperManager()->getViaUri($uri)->getDirectoryPath();
230 return Url::fromUri('base:' . $directory_path . '/' . file_uri_target($uri), ['absolute' => TRUE, 'query' => $token_query])->toString();
233 $file_url = file_create_url($uri);
234 // Append the query string with the token, if necessary.
236 $file_url .= (strpos($file_url, '?') !== FALSE ? '&' : '?') . UrlHelper::buildQuery($token_query);
245 public function flush($path = NULL) {
246 // A specific image path has been provided. Flush only that derivative.
248 $derivative_uri = $this->buildUri($path);
249 if (file_exists($derivative_uri)) {
250 file_unmanaged_delete($derivative_uri);
255 // Delete the style directory in each registered wrapper.
256 $wrappers = $this->getStreamWrapperManager()->getWrappers(StreamWrapperInterface::WRITE_VISIBLE);
257 foreach ($wrappers as $wrapper => $wrapper_data) {
258 if (file_exists($directory = $wrapper . '://styles/' . $this->id())) {
259 file_unmanaged_delete_recursive($directory);
263 // Let other modules update as necessary on flush.
264 $module_handler = \Drupal::moduleHandler();
265 $module_handler->invokeAll('image_style_flush', [$this]);
267 // Clear caches so that formatters may be added for this style.
268 drupal_theme_rebuild();
270 Cache::invalidateTags($this->getCacheTagsToInvalidate());
278 public function createDerivative($original_uri, $derivative_uri) {
279 // If the source file doesn't exist, return FALSE without creating folders.
280 $image = $this->getImageFactory()->get($original_uri);
281 if (!$image->isValid()) {
285 // Get the folder for the final location of this style.
286 $directory = drupal_dirname($derivative_uri);
288 // Build the destination folder tree if it doesn't already exist.
289 if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
290 \Drupal::logger('image')->error('Failed to create style directory: %directory', ['%directory' => $directory]);
294 foreach ($this->getEffects() as $effect) {
295 $effect->applyEffect($image);
298 if (!$image->save($derivative_uri)) {
299 if (file_exists($derivative_uri)) {
300 \Drupal::logger('image')->error('Cached image file %destination already exists. There may be an issue with your rewrite configuration.', ['%destination' => $derivative_uri]);
311 public function transformDimensions(array &$dimensions, $uri) {
312 foreach ($this->getEffects() as $effect) {
313 $effect->transformDimensions($dimensions, $uri);
320 public function getDerivativeExtension($extension) {
321 foreach ($this->getEffects() as $effect) {
322 $extension = $effect->getDerivativeExtension($extension);
330 public function getPathToken($uri) {
331 // Return the first 8 characters.
332 return substr(Crypt::hmacBase64($this->id() . ':' . $this->addExtension($uri), $this->getPrivateKey() . $this->getHashSalt()), 0, 8);
338 public function deleteImageEffect(ImageEffectInterface $effect) {
339 $this->getEffects()->removeInstanceId($effect->getUuid());
347 public function supportsUri($uri) {
348 // Only support the URI if its extension is supported by the current image
351 Unicode::strtolower(pathinfo($uri, PATHINFO_EXTENSION)),
352 $this->getImageFactory()->getSupportedExtensions()
359 public function getEffect($effect) {
360 return $this->getEffects()->get($effect);
366 public function getEffects() {
367 if (!$this->effectsCollection) {
368 $this->effectsCollection = new ImageEffectPluginCollection($this->getImageEffectPluginManager(), $this->effects);
369 $this->effectsCollection->sort();
371 return $this->effectsCollection;
377 public function getPluginCollections() {
378 return ['effects' => $this->getEffects()];
384 public function addImageEffect(array $configuration) {
385 $configuration['uuid'] = $this->uuidGenerator()->generate();
386 $this->getEffects()->addInstanceId($configuration['uuid'], $configuration);
387 return $configuration['uuid'];
393 public function getReplacementID() {
394 /** @var \Drupal\image\ImageStyleStorageInterface $storage */
395 $storage = $this->entityTypeManager()->getStorage($this->getEntityTypeId());
396 return $storage->getReplacementId($this->id());
402 public function getName() {
403 return $this->get('name');
409 public function setName($name) {
410 $this->set('name', $name);
415 * Returns the image effect plugin manager.
417 * @return \Drupal\Component\Plugin\PluginManagerInterface
418 * The image effect plugin manager.
420 protected function getImageEffectPluginManager() {
421 return \Drupal::service('plugin.manager.image.effect');
425 * Returns the image factory.
427 * @return \Drupal\Core\Image\ImageFactory
430 protected function getImageFactory() {
431 return \Drupal::service('image.factory');
435 * Gets the Drupal private key.
438 * The Drupal private key.
440 protected function getPrivateKey() {
441 return \Drupal::service('private_key')->get();
445 * Gets a salt useful for hardening against SQL injection.
448 * A salt based on information in settings.php, not in the database.
450 * @throws \RuntimeException
452 protected function getHashSalt() {
453 return Settings::getHashSalt();
457 * Adds an extension to a path.
459 * If this image style changes the extension of the derivative, this method
460 * adds the new extension to the given path. This way we avoid filename
461 * clashes while still allowing us to find the source image.
463 * @param string $path
464 * The path to add the extension to.
467 * The given path if this image style doesn't change its extension, or the
468 * path with the added extension if it does.
470 protected function addExtension($path) {
471 $original_extension = pathinfo($path, PATHINFO_EXTENSION);
472 $extension = $this->getDerivativeExtension($original_extension);
473 if ($original_extension !== $extension) {
474 $path .= '.' . $extension;
480 * Provides a wrapper for file_uri_scheme() to allow unit testing.
482 * Returns the scheme of a URI (e.g. a stream).
485 * A stream, referenced as "scheme://target" or "data:target".
487 * @see file_uri_target()
489 * @todo: Remove when https://www.drupal.org/node/2050759 is in.
492 * A string containing the name of the scheme, or FALSE if none. For
493 * example, the URI "public://example.txt" would return "public".
495 protected function fileUriScheme($uri) {
496 return file_uri_scheme($uri);
500 * Provides a wrapper for file_uri_target() to allow unit testing.
502 * Returns the part of a URI after the schema.
505 * A stream, referenced as "scheme://target" or "data:target".
507 * @see file_uri_scheme()
509 * @todo: Convert file_uri_target() into a proper injectable service.
511 * @return string|bool
512 * A string containing the target (path), or FALSE if none.
513 * For example, the URI "public://sample/test.txt" would return
516 protected function fileUriTarget($uri) {
517 return file_uri_target($uri);
521 * Provides a wrapper for file_default_scheme() to allow unit testing.
523 * Gets the default file stream implementation.
525 * @todo: Convert file_default_scheme() into a proper injectable service.
528 * 'public', 'private' or any other file scheme defined as the default.
530 protected function fileDefaultScheme() {
531 return file_default_scheme();
535 * Gets the stream wrapper manager service.
537 * @return \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
538 * The stream wrapper manager service
540 * @todo Properly inject this service in Drupal 9.0.x.
542 protected function getStreamWrapperManager() {
543 return \Drupal::service('stream_wrapper_manager');