You are here

function _css_emimage_replace in CSS Embedded Images 6.2

Same name and namespace in other branches
  1. 6 css_emimage.module \_css_emimage_replace()
  2. 7 css_emimage.inc \_css_emimage_replace()

preg_replace_callback() callback to replace URLs with data URIs.

1 string reference to '_css_emimage_replace'
_css_emimage_text_processor in ./css_emimage.module
Process the css text and replace it with image data where necessary.

File

./css_emimage.module, line 542
CSS Embedded Images module.

Code

function _css_emimage_replace($matches) {
  list($declaration, $selector, $properties) = $matches;
  $pattern = '/(background(?:-image)?|list-style(?:-image)?):[^{};)]*?((?:none|url\\([\'"]?(.+?)[\'"]?\\)))([^{};]*)/i';
  preg_match_all($pattern, $properties, $matches);
  foreach ($matches[1] as $i => $property) {
    $url = $matches[2][$i];
    $file = $matches[3][$i];
    $important = stripos($matches[4][$i], '!important') !== FALSE;
    if ($file && strpos($file, base_path()) === 0 && ($image = image_get_info($file = substr($file, strlen(base_path()))))) {
      $ielimit = variable_get('css_emimage_ielimit', 1);

      // only embed data URIs less than 32KB, thanks IE
      if ($ielimit && $image['file_size'] * 1.3333 >= CSS_EMIMAGE_IE_DATAURI_LIMIT) {
        $image = NULL;
      }
      $token = _css_emimage_collect(array(
        $selector,
        $property,
        $url,
        $file,
        $important,
        $image,
      ));
      $declaration = str_replace($url, $token, $declaration);
    }
    else {
      _css_emimage_collect(array(
        $selector,
        $property,
        $url,
        $file,
        $important,
        NULL,
      ));
    }
  }
  return $declaration;
}