You are here

private function HtmlAssetInliner::imageToDataUri in TMGMT Translator Smartling 8

Converts images from <img> tags to data URIs

Parameters

$path - image path eg src value:

Return value

string - generated data uri

1 call to HtmlAssetInliner::imageToDataUri()
HtmlAssetInliner::convertImageToDataUri in src/Context/HtmlAssetInliner.php
Converts images to data URIs

File

src/Context/HtmlAssetInliner.php, line 195

Class

HtmlAssetInliner

Namespace

Drupal\tmgmt_smartling\Context

Code

private function imageToDataUri($path) {
  $fileType = trim(strtolower(pathinfo($path, PATHINFO_EXTENSION)));
  $mimType = $fileType;

  # since jpg/jpeg images have image/jpeg mime-type
  if (!$fileType || $fileType === 'jpg') {
    $mimType = 'jpeg';
  }
  else {
    if ($fileType === 'ico') {
      $mimType = 'x-icon';
    }
  }

  # make sure that it is an image and convert to data uri
  if (preg_match('#^(gif|png|jp[e]?g|bmp)$#i', $fileType) || $this
    ->isImage($path)) {

    # in case of images from gravatar, etc
    if ($mimType === 'php' || stripos($mimType, 'php') !== FALSE) {
      $mimType = 'jpeg';
    }
    $data = $this
      ->getContents($path);
    $base64 = 'data:image/' . $mimType . ';base64,' . base64_encode($data);
    return $base64;
  }
}