You are here

private function HtmlAssetInliner::embedContentImages in TMGMT Translator Smartling 8

1 call to HtmlAssetInliner::embedContentImages()
HtmlAssetInliner::getCompletePage in src/Context/HtmlAssetInliner.php
Gets complete page data and returns generated string

File

src/Context/HtmlAssetInliner.php, line 512

Class

HtmlAssetInliner

Namespace

Drupal\tmgmt_smartling\Context

Code

private function embedContentImages() {
  $matches = array();
  preg_match_all('/<img.*src="([^"]+)".*>/iU', $this->html, $matches);
  foreach ($matches[1] as $k => $img_url) {
    $img_url = trim($img_url, '\'"');
    $img_url = str_replace($this
      ->getBaseUrl(), '', $img_url);

    # make sure that it is an image and convert to data uri
    $fileType = trim(strtolower(pathinfo($img_url, PATHINFO_EXTENSION)));
    if (!preg_match('#^(gif|png|jp[e]?g|bmp|svg)$#i', $fileType)) {
      continue;
    }
    $src = DRUPAL_ROOT . $img_url;
    if (!file_exists($src) || !($dataUri = file_get_contents($src))) {
      continue;
    }
    $mimType = $fileType === 'svg' ? 'svg+xml' : 'png';
    $dataUri = '<img src="data:image/' . $mimType . ';base64,' . base64_encode($dataUri) . '" />';
    $this->html = str_replace($matches[0][$k], $dataUri, $this->html);
  }

  //return $css_content;
}