You are here

private function HtmlAssetInliner::embedCssImages in TMGMT Translator Smartling 8

1 call to HtmlAssetInliner::embedCssImages()
HtmlAssetInliner::embedLocalCss in src/Context/HtmlAssetInliner.php

File

src/Context/HtmlAssetInliner.php, line 464

Class

HtmlAssetInliner

Namespace

Drupal\tmgmt_smartling\Context

Code

private function embedCssImages($css_content, $path) {
  $matches = array();
  preg_match_all('/url\\(([\\d\\D^)]+)\\)/iU', $css_content, $matches);
  foreach ($matches[1] as $k => $img_url) {
    $img_url = trim($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 = $img_url[0] === '/' ? DRUPAL_ROOT . $img_url : pathinfo($path, PATHINFO_DIRNAME) . '/' . $img_url;
    if (!file_exists($src) || !($dataUri = file_get_contents($src))) {
      continue;
    }
    $mimType = $fileType === 'svg' ? 'svg+xml' : 'png';
    $dataUri = 'url("data:image/' . $mimType . ';base64,' . base64_encode($dataUri) . '")';
    $css_content = str_replace($matches[0][$k], $dataUri, $css_content);
  }
  return $css_content;
}