You are here

function media_entity_twitter_preprocess_media_entity_twitter_tweet_thumbnail in Media entity Twitter 8

Same name and namespace in other branches
  1. 8.2 media_entity_twitter.module \media_entity_twitter_preprocess_media_entity_twitter_tweet_thumbnail()

Preprocess function for media_entity_twitter_tweet_thumbnail theme hook.

Parameters

array $variables: Variables to be injected into the template.

File

./media_entity_twitter.module, line 32
Hook implementations for media_entity_twitter module.

Code

function media_entity_twitter_preprocess_media_entity_twitter_tweet_thumbnail(array &$variables) {

  // If the avatar exists, load it directly into memory and base64 encode it.
  // For security reasons, browsers don't always allow external images xlinked
  // in an SVG to be displayed when the SVG is being embedded via an <img> tag.
  // The workaround is to embed the image directly into the SVG as a base64
  // encoded string.
  if ($variables['avatar']) {
    $extension = pathinfo($variables['avatar'], PATHINFO_EXTENSION);
    $extension = strtolower($extension);

    // Don't fetch the avatar if it has an unrecognized extension.
    if (in_array($extension, [
      'gif',
      'jpg',
      'jpeg',
      'png',
      'webp',
    ])) {
      $data = file_get_contents($variables['avatar']);
      if ($data) {

        // image/jpg is not a thing.
        if ($extension == 'jpg') {
          $extension = 'jpeg';
        }
        $variables['avatar'] = 'data:image/' . $extension . ';base64,' . base64_encode($data);
      }
      else {
        unset($variables['avatar']);
      }
    }
    else {
      unset($variables['avatar']);
    }
  }
}