You are here

function responsive_image_get_mime_type in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/responsive_image/responsive_image.module \responsive_image_get_mime_type()

Determines the MIME type of an image.

Parameters

string $image_style_name: The image style that will be applied to the image.

string $extension: The original extension of the image (without the leading dot).

Return value

string The MIME type of the image after the image style is applied.

1 call to responsive_image_get_mime_type()
_responsive_image_build_source_attributes in core/modules/responsive_image/responsive_image.module
Helper function for template_preprocess_responsive_image().

File

core/modules/responsive_image/responsive_image.module, line 469
Responsive image display formatter for image fields.

Code

function responsive_image_get_mime_type($image_style_name, $extension) {
  if ($image_style_name == ResponsiveImageStyleInterface::EMPTY_IMAGE) {
    return 'image/gif';
  }

  // The MIME type guesser needs a full path, not just an extension, but the
  // file doesn't have to exist.
  if ($image_style_name === ResponsiveImageStyleInterface::ORIGINAL_IMAGE) {
    $fake_path = 'responsive_image.' . $extension;
  }
  else {
    $fake_path = 'responsive_image.' . ImageStyle::load($image_style_name)
      ->getDerivativeExtension($extension);
  }
  $guesser = \Drupal::service('file.mime_type.guesser.extension');
  if (!$guesser instanceof MimeTypeGuesserInterface) {
    @trigger_error('\\Symfony\\Component\\HttpFoundation\\File\\MimeType\\MimeTypeGuesserInterface is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Implement \\Symfony\\Component\\Mime\\MimeTypeGuesserInterface instead. See https://www.drupal.org/node/3133341', E_USER_DEPRECATED);
    return $guesser
      ->guess($fake_path);
  }
  return $guesser
    ->guessMimeType($fake_path);
}