epic-s6ts
[kivitendo-erp.git] / scripts / image_maps.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Long;
5 use File::Basename;
6
7 my $css_file   = 'generated.css';
8 my $image_file = 'generated.png';
9 my $class_for_map = 'icon';
10
11 my $convert_bin  = 'convert';
12 my $identify_bin = 'identify';
13
14 GetOptions(
15   'css-out=s'    => \$css_file,
16   'image-out=s'  => \$image_file,
17   'icon-class=s' => \$class_for_map,
18 );
19
20 my @files = @ARGV;
21 my @images;
22
23 # read files
24
25 for my $filename (sort @files) {
26    my $image = `$identify_bin $filename`;
27    if (!defined $image) {
28      warn "warning: could not identify image '$filename'. skipping...";
29      next;
30    }
31   $image =~ /^(?<filename>\S+) \s (?<type>\S+) \s (?<width>\d+) x (?<height>\d+)/x;
32   push @images, {
33     filename => $filename,
34     type     => $+{type},
35     width    => $+{width},
36     height   => $+{height},
37   };
38 }
39
40 # make target layout
41 # for simplification thi will check if all the  images have the same dimensions
42 # and croak if not
43 my $first_height = $images[0]->{height};
44 my $first_width  = $images[0]->{width};
45
46 use Data::Dumper;
47
48 for my $img (@images) {
49   die 'heights are not equal' if $first_height != $img->{height};
50   die 'widths are not equal'  if $first_width  != $img->{width};
51 }
52
53 # all equal? nice.
54 # we'll be lazy and just put them all together left-to-right
55 my $new_height = $first_height;
56 my $new_width  = $first_width * @images;
57
58 # now copy them all together, and keep a referende to;
59
60 my $convert_string = "$convert_bin ";
61
62 my $h_offset = 0;
63 for (@images) {
64   $_->{h_offset} = $h_offset;
65   $_->{v_offset} = 0;
66   $convert_string .= ' +append ' . $_->{filename};
67 } continue {
68   $h_offset += $_->{width};
69 }
70
71 $convert_string .= " -background none +append $image_file";
72
73 # now write that png...
74 system($convert_string);
75
76 # make css file
77 {
78   open my $file, ">", $css_file or die "can't write too $css_file";
79   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";
80
81   for (@images) {
82     my $name = fileparse($_->{filename}, ".png");
83
84     # the full grammar for valid css class names is completely bonkers (to put it mildly).
85     # so instead of trying to punch filenames into those class names, we'll
86     # just reduce them to a nice minimal set of lower case /[a-z0-9_-]*/
87     $name = lc $name;
88     $name =~ s/[^a-z0-9_-]/-/g;
89     print $file ".$class_for_map.$name { background-position: -$_->{h_offset}px 0px; }\n";
90   }
91 }
92
93 1;
94
95 __END__
96
97 =encoding utf-8
98
99 =head1 NAME
100
101 image_maps - generates image maps for css sprites from images in a directory
102
103 =head1 SYNOPSIS
104
105   scripts/image_maps.pl \
106     --out-css=css/icons_16.css \
107     --out-image= image/maps/icons_16.png \
108     image/icons/16x16/*
109
110 =head1 DESCRIPTION
111
112 =head1 OPTIONS
113
114 =head1 BUGS
115
116 None yet. :)
117
118 =head1 AUTHOR
119
120 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
121
122 =cut
123
124