You are here

function _css_emimage_collect in CSS Embedded Images 6.2

Same name and namespace in other branches
  1. 7 css_emimage.inc \_css_emimage_collect()

Helper function to collect CSS declarations to replace with data URIs.

Return value

string a token used during the replacement process

1 call to _css_emimage_collect()
_css_emimage_replace in ./css_emimage.module
preg_replace_callback() callback to replace URLs with data URIs.

File

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

Code

function _css_emimage_collect($info) {
  list($declarations, $file_stats) = _css_emimage_collect_static();
  if (!$declarations) {
    $declarations = array();
  }
  if (!$file_stats) {
    $file_stats = array();
  }
  if (is_array($info)) {
    list($selector, $property, $url, $file, $important, $image) = $info;
    $selector = trim($selector);

    // Normalize the CSS property name - allows us to collapse declarations in
    // some cases, and generated CSS is more consistent.
    if ($property == 'background' || $property == 'list-style') {
      $property .= '-image';
    }
    $current = array_pop($declarations);
    switch (TRUE) {
      case $current && ($current['property'] != $property || $current['file'] != $file || $current['important'] != $important):
        array_push($declarations, $current);
      case !$current:
        $pos = count($declarations);
        $current = array(
          'pos' => $pos,
          'token' => "[css_emimage:{$pos}]",
          'selectors' => array(
            $selector,
          ),
          'property' => $property,
          'url' => $url,
          'file' => $file,
          'important' => $important,
          'base64' => $image ? base64_encode(file_get_contents($file)) : '',
          'mime_type' => $image ? $image['mime_type'] : '',
        );
        if ($current['base64']) {
          if (!isset($file_stats[$file])) {
            $file_stats[$file] = array(
              'indices' => array(
                $pos,
              ),
              'total_length' => strlen($current['base64']),
            );
          }
          else {
            $file_stats[$file]['indices'][] = $pos;
            $file_stats[$file]['total_length'] += strlen($current['base64']);
          }
        }
        break;
      default:
        $current['selectors'][] = $selector;
        break;
    }
    array_push($declarations, $current);
    _css_emimage_collect_static(array(
      $declarations,
      $file_stats,
    ));
    return $current['token'];
  }
  return FALSE;
}