You are here

function bueditor_sprite_icons in BUEditor 7

Same name and namespace in other branches
  1. 6.2 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 48
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 = new stdClass();
  $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->source = $sprite->dir . '/' . $sprite->name;
  if (file_exists($sprite->source)) {
    return $sprite->name;
  }
  if (!file_prepare_directory($sprite->dir, 1) || !($icon = image_load($first_icon, 'gd'))) {
    return '';
  }

  //set fixed icon dimensions
  $icon_width = $icon->info['width'];
  $icon_height = $icon->info['height'];

  //set sprite dimensions based on the number of icons
  $sprite->info = $icon->info;
  $sprite->toolkit = $icon->toolkit;
  $sprite->info['width'] = $icon_width * $total;
  $sprite->resource = image_gd_create_tmp($icon, $sprite->info['width'], $sprite->info['height']);

  //copy first icon
  imagecopy($sprite->resource, $icon->resource, 0, 0, 0, 0, $icon_width, $icon_height);
  imagedestroy($icon->resource);

  //copy the rest
  for ($i = 1; $i < $total; $i++) {
    if ($icon = image_load($icons[$i], 'gd')) {
      imagecopy($sprite->resource, $icon->resource, $i * $icon_width, 0, 0, 0, $icon_width, $icon_height);
      imagedestroy($icon->resource);
    }
  }

  //save sprite to file
  $result = image_save($sprite);
  imagedestroy($sprite->resource);
  return $result ? $sprite->name : '';
}