/[Frey]/trunk/lib/Frey/IconBrowser.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/IconBrowser.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 553 - (show annotations)
Thu Nov 27 15:45:00 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 3036 byte(s)
first version of IconBrowser which can combine images into CSS sprites
1 package Frey::IconBrowser;
2 use Moose;
3
4 extends 'Frey';
5 with 'Frey::Web';
6 with 'Frey::Path';
7 with 'Frey::Storage';
8
9 has path => (
10 is => 'rw',
11 isa => 'Str',
12 required => 1,
13 default => 'static/icons/fugue/',
14 );
15
16 use GD;
17 use Digest::MD5 qw/md5_hex/;
18 use File::Slurp;
19 use Data::Dump qw/dump/;
20
21 =head2 combine_image
22
23 Combine images into CSS sprites
24
25 my ( $combined_path, $styles ) = $self->combine_image( @paths );
26
27 =cut
28
29 sub combine_image {
30 my $self = shift;
31 my @images = @_;
32
33 my $path = 'var/combine/' . md5_hex( join(' ',@images ) );
34 my $style_path = $path . '.yaml';
35 $path .= '.png';
36
37 return ( $path, $self->load( $style_path ) ) if -e $path;
38
39 warn "# path $path";
40 $self->mkbasepath( $path );
41
42 # fixed dimensions of images combined
43 my ( $w, $h ) = ( 16, 16 );
44 my $num_images = $#images + 1;
45
46 my $combined = GD::Image->new( $w, $num_images * $h )
47 || die "can't create $w x ",$num_images * $h," combined image";
48 $combined->trueColor( 1 );
49 $combined->saveAlpha( 1 );
50 my $white = $combined->colorAllocate(255,255,255);
51 $combined->transparent($white);
52
53 my $y = 0;
54
55 my $style;
56
57 foreach my $image_path ( @images ) {
58 my $i = GD::Image->new( $image_path ) || die "can't open $image_path: $!";
59 $i->trueColor( 1 );
60 $i->saveAlpha( 1 );
61 # $i->transparent($white);
62
63 die "with not $w" unless $i->width == $w;
64 die "with not $h" unless $i->height == $h;
65
66 warn "# copy $image_path 0 x $y";
67 $combined->copy( $i, 0, $y, 0, 0, $w, $h );
68
69 $style->{$image_path}
70 = qq|background:url(/$path) no-repeat; padding:0 0 0 | . ( $h - 1 ) . qq|px; background-position:0 |
71 . ( $y ? -$y . 'px' : 0 )
72 ;
73
74 $y += $h;
75 }
76
77 write_file( $path, $combined->png );
78 $self->store( $style_path, $style );
79 warn
80 "# combined $num_images into ",
81 $combined->width, "x", $combined->height, " pixels ",
82 $path, " ", -s $path , " bytes ",
83 " style $style_path ", -s $style_path, " bytes";
84
85 return ( $path, $style );
86 }
87
88 sub as_markup {
89 my ($self) = @_;
90
91 my $extension = '\.(?:png)$';
92
93 opendir(my $dir, $self->path) || die "can't opendir ", $self->path, ": $!";
94 my @icons = sort grep { m/$extension/ } readdir($dir);
95 closedir $dir;
96
97 my ( $combined_path, $styles ) = $self->combine_image( map { $self->path . '/' . $_ } @icons );
98
99 my $html;
100 my $base;
101
102 foreach my $icon ( @icons ) {
103 my $name = $icon;
104 $name =~ s{$extension}{};
105
106 $base ||= $name; # seed with first
107 my $desc;
108
109 my $bl = length $base;
110
111 if ( substr($name, 0, $bl) eq $base ) {
112 ($name,$desc) = (
113 substr($name,0,$bl),
114 substr($name,$bl)
115 );
116 } else {
117 $base = $name;
118 warn "# new base $base";
119 $html .= qq|<hr>\n|;
120 }
121
122 # $html .= qq|<img src="/| . $self->path . qq|/$icon" alt="$icon"> <b>$name</b>$desc<br/>\n|;
123 # $html .= qq|<b>$name</b>$desc<br/>\n|;
124
125 my $path = $self->path . '/' . $icon;
126
127 my $style = $styles->{$path} || die "can't find style for $path in ",dump( $styles );
128 $html .= qq|<span style="$style" title="$icon"> <b>$name</b>$desc</span>\n|;
129 }
130
131
132 # $html .= qq|<img src="$combined_path">|;
133 return $html;
134 }
135
136 1;

  ViewVC Help
Powered by ViewVC 1.1.26