You are here

function _css_emimage_process in CSS Embedded Images 6.2

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

Helper function to replace URLs with data URIs.

1 call to _css_emimage_process()
css_emimage_preprocess_page in ./css_emimage.module
Implementation of hook_preprocess_hook().

File

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

Code

function _css_emimage_process($styles) {
  global $base_url;
  $path_to_files_directory = base_path() . file_directory_path();
  $pattern = '/<link(.*?)href=".*?' . preg_quote($path_to_files_directory, '/') . '(.*?)(\\?[^"]*)?"(.*?)\\/>/';
  if (preg_match_all($pattern, $styles, $matches) > 0) {
    foreach ($matches[2] as $i => $aggregated_file_name) {
      $file_path = file_directory_path();
      $aggregated_file_path = $file_path . $aggregated_file_name;
      $emimage_file_name = str_replace('.css', '.emimage.css', $aggregated_file_name);
      $emimage_file_path = $file_path . $emimage_file_name;
      $orig_file_name = str_replace('.css', '.orig.css', $aggregated_file_name);
      $orig_file_path = $file_path . $orig_file_name;

      // Save the processed CSS file if it doesn't exist yet.
      if (!file_exists($emimage_file_path) || filemtime($aggregated_file_path) > filemtime($emimage_file_path)) {
        $contents = $orig_contents = file_get_contents($aggregated_file_path);

        // Save a copy of the original aggregated CSS for IE < 8 fallback.
        file_save_data($orig_contents, $orig_file_path, FILE_EXISTS_REPLACE);

        // Save the modified aggregated CSS file.
        _css_emimage_text_processor($orig_contents, $orig_file_path, 'base');
        file_save_data($orig_contents, $aggregated_file_path, FILE_EXISTS_REPLACE);

        // Save the CSS file containing the embedded images.
        // This may be empty, but we use the file as a flag to prevent
        // processing the CSS on every uncached request.
        _css_emimage_text_processor($contents, $orig_file_path, 'emimage');
        file_save_data($contents, $emimage_file_path, FILE_EXISTS_REPLACE);
      }

      // Replace the aggregated file with the processed CSS file.
      if (file_exists($emimage_file_path) && filesize($emimage_file_path)) {
        $styles = str_replace($matches[0][$i], "<!--[if gte IE 8]><!-->\n" . (filesize($aggregated_file_path) ? $matches[0][$i] . "\n" : '') . str_replace($aggregated_file_name, $emimage_file_name, $matches[0][$i]) . "\n<!--<![endif]-->\n" . "<!--[if lt IE 8]>\n" . str_replace($aggregated_file_name, $orig_file_name, $matches[0][$i]) . "\n<![endif]-->", $styles);
      }
    }
  }
  return $styles;
}