You are here

function bg_image_add_background_image in Background Images 6

Same name and namespace in other branches
  1. 7 bg_image.module \bg_image_add_background_image()

Adds a background image to the page using the css 'background' property.

Parameters

$image_path: The path of the image to use.

$css_settings: An array of css settings to use. Possible values are:

  • bg_image_selector: The css selector to use
  • bg_image_color: The background color
  • bg_image_x: The x offset
  • bg_image_y: The y offset
  • bg_image_attachment: The attachment property (scroll or fixed)
  • bg_image_repeat: The repeat settings
  • bg_image_background_size: The background size property if necessary

Default settings will be used for any values not provided.

$imagecache_preset: Optionally add an imagecache preset to the image before applying it to the background

Return value

Returns TRUE if successful or FALSE otherwise

1 call to bg_image_add_background_image()
bg_image_add_background_image_from_node in ./bg_image.module
Adds a background image to the page using an image from a node. The node must have an image (or media) field and the field must be configured on the bg_image configuration page for this to work.

File

./bg_image.module, line 361
Allows for customizable background images per page

Code

function bg_image_add_background_image($image_path, $css_settings = array(), $imagecache_preset = NULL, $bg_image_id = 'global') {
  $css_filename = 'bg_image_' . $bg_image_id . '.css';

  // Pull the default css setting if not provided.
  $selector = isset($css_settings['bg_image_selector']) ? $css_settings['bg_image_selector'] : variable_get('bg_image_selector', '');
  $bg_color = isset($css_settings['bg_image_color']) ? $css_settings['bg_image_color'] : variable_get('bg_image_color', '#FFFFFF');
  $bg_x = isset($css_settings['bg_image_x']) ? $css_settings['bg_image_x'] : variable_get('bg_image_x', 'left');
  $bg_y = isset($css_settings['bg_image_y']) ? $css_settings['bg_image_y'] : variable_get('bg_image_y', 'top');
  $attachment = isset($css_settings['bg_image_attachment']) ? $css_settings['bg_image_attachment'] : variable_get('bg_image_attachment', 'scroll');
  $repeat = isset($css_settings['bg_image_repeat']) ? $css_settings['bg_image_repeat'] : variable_get('bg_image_repeat', 'no-repeat');
  $important = isset($css_settings['bg_image_important']) ? $css_settings['bg_image_important'] : variable_get('bg_image_important', 1);
  $background_size = isset($css_settings['bg_image_background_size']) ? $css_settings['bg_image_background_size'] : variable_get('bg_image_background_size', '');
  $media_query = isset($css_settings['bg_image_media_query']) ? $css_settings['bg_image_media_query'] : variable_get('bg_image_media_query', 'all');

  // Apply an Imagecache Preset?
  $imagecache_preset = $imagecache_preset ? $imagecache_preset : variable_get('bg_imagecache_preset', '');

  // If important is true, we turn it into a string for css output
  if ($important) {
    $important = '!important';
  }
  else {
    $important = '';
  }

  // Handle the background size property
  $bg_size = '';
  $ie_bg_size = '';
  if ($background_size) {

    // CSS3
    $bg_size = "    background-size: {$background_size} {$important};\n";

    // Let's cover ourselves for other browsers as well...
    $bg_size .= "    -webkit-background-size: {$background_size} {$important};\n";
    $bg_size .= "    -moz-background-size: {$background_size} {$important};\n";
    $bg_size .= "    -o-background-size: {$background_size} {$important};\n";

    // IE filters to apply the cover effect
    if ($background_size == 'cover' && variable_get('bg_image_background_size_ie8', 0)) {
      $bg_size .= '    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'.' . $image_path . '\', sizingMethod=\'scale\');
      -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' . $image_path . '\', sizingMethod=\'scale\')";' . "\n";
    }
  }

  // See if admin pages are excluded from bg images
  $admin = variable_get('bg_image_exclude_admin', 0) ? !_bg_image_path_is_admin($_GET['q']) : TRUE;

  // Check if we need to run this through an imagecache preset
  if ($imagecache_preset && module_exists('imagecache')) {

    // Image cache paths need to be relative to drupal root with no leading slash
    $imagecache_path = trim(parse_url($image_path, PHP_URL_PATH), '/');
    $image_path = imagecache_create_url($imagecache_preset, $imagecache_path);
  }

  // Add the css if we have everything we need.
  if ($selector && $image_path && $admin) {
    $style = "@media {$media_query} {\n";
    $style .= "  {$selector} {\n";
    $style .= "    background-color: {$bg_color} {$important};\n";
    $style .= "    background-image: url('{$image_path}') {$important};\n";
    $style .= "    background-repeat: {$repeat} {$important};\n";
    $style .= "    background-attachment: {$attachment} {$important};\n";
    $style .= "    background-position: {$bg_x} {$bg_y} {$important};\n";
    $style .= $bg_size;
    $style .= "  }\n";
    $style .= "}\n";
    $name = file_save_data($style, $css_filename, FILE_EXISTS_REPLACE);
    drupal_add_css($name);

    // drupal_add_css($style, array('type' => 'inline', 'media' => $media_query, 'group' => CSS_THEME));
    return TRUE;
  }
  else {
    return FALSE;
  }
}