Score:0

Is there an easy way to get the image derivatives links?

by flag

I'm working with complex image styles, and I'd like to be able to load all the different derivatives quickly to check they're being processed correctly.

But I don't see how to easily get the links. The best I can come up with is opening the node in JSONAPI and, with the patch from https://www.drupal.org/project/drupal/issues/2825812, getting the URLs for derivatives from that.

It there something easier?

Back in the Drupal 6 days, the Image module showed the links to all the sizes on the image node itself.

Score:1
cn flag

You can get that info with the following code:

$original_uri = 'public://images/image.jpg';

$styles = \Drupal::entityTypeManager()->getStorage('image_style')->loadMultiple();

$urls = array_map(function($style) use ($original_uri) {
  return $style->buildUri($original_uri);
}, $styles);

$urls should now contain a list of URLs based on the original, one for each image style in the system.

Score:1
us flag

To get the image derivative URI for an image to which is applied a specific image style, you can use the following code.

// Load the ImageStyle instance in $image_style, for example with
// $image_style = ImageStyle::load($id).
// $image_uri is the path/URI for the image to which the style is applied. 
if ($image_style->supportsUri($image_uri)) {
  $derivative_uri = $style->buildUri($image_uri);
}

Keep in mind that image derivatives are created only when the URL returned from $image_style->buildURL($image_uri) is requested (from a browser). Building the URI (or the URL) doesn't generate the derivative image; it returns the URI (URL) for an image that couldn't exist, yet.

To check an image derivative could be created, code similar to the one used from ImageStyleDownloadController::deliver() should be used.

if ($image_style->supportsUri($image_uri)) {
  $derivative_uri = $style->buildUri($image_uri);

  if (!file_exists($image_uri)) {
    $path_info = pathinfo($image_uri);
    $converted_image_uri = $path_info['dirname'] . DIRECTORY_SEPARATOR . $path_info['filename'];
    if (file_exists($converted_image_uri)) {
      $image_uri = $converted_image_uri;
    }
  }

  $success = file_exists($derivative_uri) || $image_style->createDerivative($image_uri, $derivative_uri);

  if ($success) {
    $image = Drupal::service('image.factory')->get($derivative_uri);
    $uri = $image->getSource();
  }
}
by flag
Thanks! What I meant though is whether there's a way of getting those out in the UI. Content editors, for example, may want to verify the derivative images quickly.
apaderno avatar
us flag
I don't know of any module that does that. A custom module that uses the code I suggest can show the URIs on a page.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.