You are here

function bueditor_sprite_icons in BUEditor 6.2

Same name and namespace in other branches
  1. 7 admin/bueditor.sprite.inc \bueditor_sprite_icons()

Create an icon sprite for a set of icons.

1 call to bueditor_sprite_icons()
bueditor_sprite_buttons in admin/bueditor.sprite.inc
Create an icon sprite for a set of buttons.

File

admin/bueditor.sprite.inc, line 46
Generates CSS sprites for editor buttons.

Code

function bueditor_sprite_icons($icons) {
  if (image_get_toolkit() != 'gd' || empty($icons) || ($total = count($icons)) < 2) {
    return '';
  }

  //Image properties of the first one in the list will be the reference for the sprite
  $first_icon = $icons[0];
  $sprite_dir = bueditor_sprites_dir();
  $sizes = @array_map('filesize', $icons);
  $sprite_name = 'sprite_' . md5(implode(' ', array_merge($icons, $sizes))) . substr($first_icon, strrpos($first_icon, '.'));
  $sprite_path = $sprite_dir . '/' . $sprite_name;
  if (file_exists($sprite_path)) {
    return $sprite_name;
  }
  if (!file_check_directory($sprite_dir, 1) || !($first_info = image_get_info($first_icon)) || !($first_im = image_gd_open($first_icon, $first_info['extension']))) {
    return '';
  }

  //set fixed icon dimensions
  $icon_width = $first_info['width'];
  $icon_height = $first_info['height'];

  //set sprite dimensions based on the number of icons
  $sprite_width = $icon_width * $total;
  $sprite_height = $icon_height;
  $sprite_ext = $first_info['extension'];
  $sprite = imagecreatetruecolor($sprite_width, $sprite_height);

  //handle png transperancy
  if ($sprite_ext == 'png') {
    imagealphablending($sprite, FALSE);
    $transparency = imagecolorallocatealpha($sprite, 0, 0, 0, 127);
    imagefill($sprite, 0, 0, $transparency);
    imagesavealpha($sprite, TRUE);
  }
  elseif ($sprite_ext == 'gif' && ($transparency_index = imagecolortransparent($first_im)) >= 0) {
    $transparent_color = imagecolorsforindex($first_im, $transparency_index);
    $transparency_index = imagecolorallocate($sprite, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
    imagefill($sprite, 0, 0, $transparency_index);
    imagecolortransparent($sprite, $transparency_index);
    imagetruecolortopalette($sprite, TRUE, 256);
  }

  //copy first icon
  imagecopy($sprite, $first_im, 0, 0, 0, 0, $icon_width, $icon_height);
  imagedestroy($first_im);

  //copy the rest
  for ($i = 1; $i < $total; $i++) {
    if (($info = image_get_info($icons[$i])) && ($im = image_gd_open($icons[$i], $info['extension']))) {
      imagecopy($sprite, $im, $i * $icon_width, 0, 0, 0, $icon_width, $icon_height);
      imagedestroy($im);
    }
  }

  //save sprite to file
  $result = image_gd_close($sprite, $sprite_path, $sprite_ext);
  imagedestroy($sprite);
  return $result ? $sprite_name : '';
}