View source
<?php
function css_emimage_help($path, $arg) {
switch ($path) {
case 'admin/help#css_emimage':
$output = '<p>' . t('Replaces image URLs in aggregated CSS files with embedded images when <em>CSS optimization</em> has been enabled in the <a href="@performance">Performance settings</a>.', array(
'@performance' => url('admin/settings/performance'),
)) . '</p>';
return $output;
}
}
function css_emimage_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'system_performance_settings') {
$form['bandwidth_optimizations']['preprocess_css']['#description'] .= t(' Once the CSS files have been aggregated, image URLs will be replaced with embedded images.');
$form['bandwidth_optimizations']['css_emimage_ielimit'] = array(
'#type' => 'checkbox',
'#title' => t('Only embed images less than 32KB'),
'#description' => t('Internet Explorer does not support embedded images larger than 32KB. If you are not concerned about IE support you can ignore this limitation; otherwise, it is best to leave this checked.'),
'#default_value' => variable_get('css_emimage_ielimit', 1),
'#weight' => $form['bandwidth_optimizations']['preprocess_css']['#weight'] + 0.1,
);
}
}
function css_emimage_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['page'])) {
if (($key = array_search('css_emimage_preprocess_page', $theme_registry['page']['preprocess functions'])) !== FALSE) {
unset($theme_registry['page']['preprocess functions'][$key]);
}
$theme_registry['page']['preprocess functions'][] = 'css_emimage_preprocess_page';
if (($key = array_search('css_gzip_preprocess_page', $theme_registry['page']['preprocess functions'])) !== FALSE) {
unset($theme_registry['page']['preprocess functions'][$key]);
$theme_registry['page']['preprocess functions'][] = 'css_gzip_preprocess_page';
}
}
}
function css_emimage_preprocess_page(&$variables) {
if (!empty($variables['styles']) && variable_get('preprocess_css', 0)) {
$variables['styles'] = _css_emimage_process($variables['styles']);
}
}
function _css_emimage_process($styles) {
$path_to_files_directory = base_path() . file_directory_path();
$pattern = '/href=".*?' . preg_quote($path_to_files_directory, '/') . '(.*?)(\\?|")/';
if (preg_match_all($pattern, $styles, $matches) > 0) {
foreach ($matches[1] as $aggregated_file_name) {
$datauri_file_name = str_replace('.css', '.emimage.css', $aggregated_file_name);
$datauri_file_path = file_directory_path() . $datauri_file_name;
if (!file_exists($datauri_file_path)) {
$contents = file_get_contents(file_directory_path() . $aggregated_file_name);
$datauri_contents = preg_replace_callback('/[^}]+{[^{}]+(url\\([\'"]?' . preg_quote(base_path(), '/') . '([^\'")]+)[\'"]?\\))[^{}]*}/i', '_css_emimage_replace', $contents);
if (is_null($datauri_contents)) {
$error_code = preg_last_error();
$error_messages = array(
PREG_NO_ERROR => 'NO_ERROR',
PREG_INTERNAL_ERROR => 'INTERNAL_ERROR',
PREG_BACKTRACK_LIMIT_ERROR => 'BACKTRACK_LIMIT_ERROR',
PREG_RECURSION_LIMIT_ERROR => 'RECURSION_LIMIT_ERROR',
PREG_BAD_UTF8_ERROR => 'BAD_UTF8_ERROR',
PREG_BAD_UTF8_OFFSET_ERROR => 'BAD_UTF8_OFFSET_ERROR',
);
watchdog('css_emimage', 'Error while trying to embed images in your CSS, falling back to unmodified CSS. PCRE error was: !error.', array(
'!error' => array_key_exists($error_code, $error_messages) ? $error_messages[$error_code] : $error_code,
), WATCHDOG_ERROR);
$datauri_contents = $contents;
}
file_save_data($datauri_contents, $datauri_file_path, FILE_EXISTS_REPLACE);
}
$styles = str_replace($aggregated_file_name, $datauri_file_name, $styles);
}
}
return $styles;
}
function _css_emimage_replace($matches) {
$replacement = $matches[0];
if ($image = image_get_info($matches[2])) {
$ielimit = variable_get('css_emimage_ielimit', 1);
if (!$ielimit || $ielimit && $image['file_size'] * 1.3333 < 32768) {
$replacement = str_replace($matches[1], 'url(data:' . $image['mime_type'] . ';base64,' . base64_encode(file_get_contents($matches[2])) . ')', $replacement);
$replacement .= "\n * html " . $matches[0] . "\n *+html " . $matches[0] . "\n";
}
}
return $replacement;
}