path = $path;
$this->excludeButtons = array_merge($this->excludeButtons, $exclude_buttons);
$this->buttons = $this->removePaths( glob($this->path . '/tool_*_f2.gif') );
$this->buttonsCrc = array_map(Array (&$this, 'getButtonCrc'), $this->buttons);
$this->removeDuplicates();
}
function getButtonCrc($button)
{
return crc32( file_get_contents($this->path . '/tool_' . $button . '.gif') );
}
function getButtonImage($button, $state = 1)
{
$path = $this->path . '/tool_' . $button . ($state > 1 ? '_f' . $state : '') . '.gif';
if (file_exists($path)) {
return imagecreatefromgif($path);
}
return false;
}
function removeDuplicates()
{
// collect duplicates
$processed_buttons = Array ();
$duplicate_buttons = $this->excludeButtons;
foreach ($this->buttonsCrc as $index => $button_crc) {
$duplicates = array_keys($this->buttonsCrc, $button_crc);
if (count($duplicates) > 1 && !in_array($this->buttons[$index], $duplicate_buttons)) {
$processed_buttons[] = $this->buttons[$index];
foreach ($duplicates as $duplicate_index) {
$button = $this->buttons[$duplicate_index];
if (!in_array($button, $processed_buttons)) {
$duplicate_buttons[] = $button;
$this->duplicateButtons[$button] = $this->buttons[$index];
}
}
}
}
foreach ($duplicate_buttons as $duplicate_button) {
$index = array_search($duplicate_button, $this->buttons);
unset($this->buttons[$index], $this->buttonsCrc[$index]);
}
// to reset indexing
$this->buttons = array_values($this->buttons);
$this->buttonsCrc = array_values($this->buttonsCrc);
}
function removePaths($buttons)
{
foreach ($buttons as $index => $button) {
$buttons[$index] = preg_replace('/^.*\/tool_(.*?)_f2\.gif$/', '\\1', $button);
}
return $buttons;
}
function output($path, $name, $spacer_url, $btn_width = 32, $btn_height = 32)
{
if (file_exists($path . '/' . $name . '.png')) {
unlink($path . '/' . $name . '.png');
}
if (file_exists($path . '/' . $name . '.css')) {
unlink($path . '/' . $name . '.css');
}
// generate css, that uses given sprite image
$css_fp = fopen($path . '/' . $name . '.css', 'w');
$main_sprite = <<' . "\n");
// generate sprite image
$button_states = Array ('' => 1, 'hover' => 2, 'disabled' => 3);
$sprite_rs = imagecreatetruecolor($btn_width * 3, count($this->buttons) * $btn_height);
imagealphablending($sprite_rs, false);
imagefill($sprite_rs, 0, 0, imagecolorallocatealpha($sprite_rs, 0, 0, 0, 127));
imagesavealpha($sprite_rs, true);
foreach ($this->buttons as $index => $button) {
$selectors = Array ('#' . $button . '-toolbar-button');
$duplicates = array_keys($this->duplicateButtons, $button);
foreach ($duplicates as $duplicate_button) {
$selectors[] = '#' . $duplicate_button . '-toolbar-button';
}
foreach ($button_states as $state_name => $button_state) {
$button_rs = $this->getButtonImage($button, $button_state);
if ( is_resource($button_rs) ) {
$x_pos = $btn_width * ($button_state - 1);
$y_pos = $index * $btn_height;
imagecopy($sprite_rs, $button_rs, $x_pos, $y_pos, 0, 0, $btn_width, $btn_height);
if ($state_name) {
$state_name = '.' . $state_name;
}
fwrite($css_fp, implode($state_name . ', ', $selectors) . $state_name . ' { background-position: -' . $x_pos . 'px -' . $y_pos . 'px; }' . "\n");
foreach ($selectors as $selector) {
fwrite($html_fp, '' . "\n");
}
}
}
fwrite($html_fp, '
' . "\n");
}
imagepng($sprite_rs, $path . '/' . $name . '.png', 0);
fclose($css_fp);
fclose($html_fp);
}
}
$sprite = new Sprite('core/admin_templates/img/toolbar');
$sprite->output('system', 'core-toolbar-sprite', '../core/admin_templates/img/spacer.gif');