Projekt

Allgemein

Profil

Herunterladen (2,77 KB) Statistiken
| Zweig: | Markierung: | Revision:
39139d92 Sven Schöling
#!/usr/bin/perl

use strict;
use Getopt::Long;
use File::Basename;

my $css_file = 'generated.css';
my $image_file = 'generated.png';
my $class_for_map = 'icon';

ad048c73 Sven Schöling
my $convert_bin = 'convert';
my $identify_bin = 'identify';

39139d92 Sven Schöling
GetOptions(
'css-out=s' => \$css_file,
'image-out=s' => \$image_file,
'icon-class=s' => \$class_for_map,
);

my @files = @ARGV;
ad048c73 Sven Schöling
my @images;
39139d92 Sven Schöling
# read files

ad048c73 Sven Schöling
for my $filename (sort @files) {
my $image = `$identify_bin $filename`;
39139d92 Sven Schöling
if (!defined $image) {
21da0d1a Steven Schubiger
warn "warning: could not identify image '$filename'. skipping...";
39139d92 Sven Schöling
next;
}
ad048c73 Sven Schöling
$image =~ /^(?<filename>\S+) \s (?<type>\S+) \s (?<width>\d+) x (?<height>\d+)/x;
push @images, {
39139d92 Sven Schöling
filename => $filename,
ad048c73 Sven Schöling
type => $+{type},
width => $+{width},
height => $+{height},
39139d92 Sven Schöling
};
}

# make target layout
# for simplification thi will check if all the images have the same dimensions
# and croak if not
ad048c73 Sven Schöling
my $first_height = $images[0]->{height};
my $first_width = $images[0]->{width};
39139d92 Sven Schöling
use Data::Dumper;

ad048c73 Sven Schöling
for my $img (@images) {
die 'heights are not equal' if $first_height != $img->{height};
die 'widths are not equal' if $first_width != $img->{width};
39139d92 Sven Schöling
}

# all equal? nice.
# we'll be lazy and just put them all together left-to-right
my $new_height = $first_height;
ad048c73 Sven Schöling
my $new_width = $first_width * @images;
39139d92 Sven Schöling
# now copy them all together, and keep a referende to;

ad048c73 Sven Schöling
my $convert_string = "$convert_bin ";
39139d92 Sven Schöling
my $h_offset = 0;
ad048c73 Sven Schöling
for (@images) {
39139d92 Sven Schöling
$_->{h_offset} = $h_offset;
$_->{v_offset} = 0;
ad048c73 Sven Schöling
$convert_string .= ' +append ' . $_->{filename};
39139d92 Sven Schöling
} continue {
ad048c73 Sven Schöling
$h_offset += $_->{width};
39139d92 Sven Schöling
}

ad048c73 Sven Schöling
$convert_string .= " -background none +append $image_file";

39139d92 Sven Schöling
# now write that png...
ad048c73 Sven Schöling
system($convert_string);
39139d92 Sven Schöling
# make css file
{
open my $file, ">", $css_file or die "can't write too $css_file";
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";

ad048c73 Sven Schöling
for (@images) {
39139d92 Sven Schöling
my $name = fileparse($_->{filename}, ".png");
343d80b6 Sven Schöling
# the full grammar for valid css class names is completely bonkers (to put it mildly).
4d1071ce Sven Schöling
# so instead of trying to punch filenames into those class names, we'll
# just reduce them to a nice minimal set of lower case /[a-z0-9_-]*/
343d80b6 Sven Schöling
$name = lc $name;
4d1071ce Sven Schöling
$name =~ s/[^a-z0-9_-]/-/g;
39139d92 Sven Schöling
print $file ".$class_for_map.$name { background-position: -$_->{h_offset}px 0px; }\n";
}
}

1;

__END__

=encoding utf-8

=head1 NAME

image_maps - generates image maps for css sprites from images in a directory

=head1 SYNOPSIS

scripts/image_maps.pl \
--out-css=css/icons_16.css \
--out-image= image/maps/icons_16.png \
image/icons/16x16/*

=head1 DESCRIPTION

=head1 OPTIONS

=head1 BUGS

None yet. :)

=head1 AUTHOR

Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>

=cut