Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. OEmbed.php

function 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 477

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal