9 my $css_file   = 'generated.css';
 
  10 my $image_file = 'generated.png';
 
  11 my $class_for_map = 'icon';
 
  14   'css-out=s'    => \$css_file,
 
  15   'image-out=s'  => \$image_file,
 
  16   'icon-class=s' => \$class_for_map,
 
  22 GD::Image->trueColor(1);
 
  26 for my $filename (@files) {
 
  27   my $image = GD::Image->newFromPng($filename);
 
  28    if (!defined $image) {
 
  29      warn "warning: could not load image '$filename'. skpping...";
 
  34     filename => $filename,
 
  39 # for simplification thi will check if all the  images have the same dimensions
 
  41 my $first_height = $gd_images[0]->{gd}->height;
 
  42 my $first_width  = $gd_images[0]->{gd}->width;
 
  46 for my $img (@gd_images) {
 
  47   die 'heights are not equal' if $first_height != $img->{gd}->height;
 
  48   die 'widths are not equal'  if $first_width  != $img->{gd}->width;
 
  52 # we'll be lazy and just put them all together left-to-right
 
  53 my $new_height = $first_height;
 
  54 my $new_width  = $first_width * @gd_images;
 
  56 my $new_image = GD::Image->new($new_width, $new_height, 1);
 
  57 # now copy them all together, and keep a referende to;
 
  59 $new_image->saveAlpha(1);
 
  60 $new_image->alphaBlending(0);
 
  64   $_->{h_offset} = $h_offset;
 
  66   $new_image->copy($_->{gd}, $_->{h_offset}, $_->{v_offset}, 0, 0, $_->{gd}->width, $_->{gd}->height);
 
  68   $h_offset += $_->{gd}->width;
 
  71 # now write that png...
 
  73   open my $file, '>:raw', $image_file or die "can't write to $image_file";
 
  74   print $file $new_image->png;
 
  79   open my $file, ">", $css_file or die "can't write too $css_file";
 
  80   print $file ".$class_for_map { background: url(../$image_file) ${first_width}px 0px no-repeat; padding: 0; width: ${first_width}px; height: ${first_height}px; }\n";
 
  83     my $name = fileparse($_->{filename}, ".png");
 
  85     # the full grammar for valid css class names is completely bonkers (to put it mildly).
 
  86     # so instead of trying to punch filenames into those class names, we'll
 
  87     # just reduce them to a nice minimal set of lower case /[a-z0-9_-]*/
 
  89     $name =~ s/[^a-z0-9_-]/-/g;
 
  90     print $file ".$class_for_map.$name { background-position: -$_->{h_offset}px 0px; }\n";
 
 102 image_maps - generates image maps for css sprites from images in a directory
 
 106   scripts/image_maps.pl \
 
 107     --out-css=css/icons_16.css \
 
 108     --out-image= image/maps/icons_16.png \
 
 121 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>