function OEmbed::getThumbnailFileExtensionFromUrl

Same name and namespace in other branches
  1. 9 core/modules/media/src/Plugin/media/Source/OEmbed.php \Drupal\media\Plugin\media\Source\OEmbed::getThumbnailFileExtensionFromUrl()
  2. 11.x core/modules/media/src/Plugin/media/Source/OEmbed.php \Drupal\media\Plugin\media\Source\OEmbed::getThumbnailFileExtensionFromUrl()

Tries to determine the file extension of a thumbnail.

Parameters

string $thumbnail_url: The remote URL of the thumbnail.

\Psr\Http\Message\ResponseInterface $response: The response for the downloaded thumbnail.

Return value

string|null The file extension, or NULL if it could not be determined.

1 call to OEmbed::getThumbnailFileExtensionFromUrl()
OEmbed::getLocalThumbnailUri in core/modules/media/src/Plugin/media/Source/OEmbed.php
Returns the local URI for a resource thumbnail.

File

core/modules/media/src/Plugin/media/Source/OEmbed.php, line 483

Class

OEmbed
Provides a media source plugin for oEmbed resources.

Namespace

Drupal\media\Plugin\media\Source

Code

protected function getThumbnailFileExtensionFromUrl(string $thumbnail_url, ResponseInterface $response) : ?string {
  // First, try to glean the extension from the URL path.
  $path = parse_url($thumbnail_url, PHP_URL_PATH);
  if ($path) {
    $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
    if ($extension) {
      return $extension;
    }
  }
  // If the URL didn't give us any clues about the file extension, see if the
  // response headers will give us a MIME type.
  $content_type = $response->getHeader('Content-Type');
  // If there was no Content-Type header, there's nothing else we can do.
  if (empty($content_type)) {
    return NULL;
  }
  $extensions = MimeTypes::getDefault()->getExtensions(reset($content_type));
  if ($extensions) {
    return reset($extensions);
  }
  // If no file extension could be determined from the Content-Type header,
  // we're stumped.
  return NULL;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.