/[Frey]/trunk/lib/Frey/Web/CombineImages.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/Web/CombineImages.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 555 - (show annotations)
Thu Nov 27 17:39:00 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 2105 byte(s)
extracted Frey::Web::CombineImages and use it also from ClassBrowser
1 package Frey::Web::CombineImages;
2 use Moose::Role;
3
4 with 'Frey::Path';
5 with 'Frey::Storage';
6
7 use GD;
8 use Digest::MD5 qw/md5_hex/;
9 use File::Slurp;
10 use Data::Dump qw/dump/;
11
12 =head2 combine_images
13
14 Combine images into CSS sprites based on L<http://www.alistapart.com/articles/sprites/>
15
16 my ( $combined_path, $styles ) = $self->combine_images( @paths );
17
18 =cut
19
20 sub combine_images {
21 my $self = shift;
22 my @images = @_;
23
24 my $path = 'var/combine/' . md5_hex( join(' ',@images ) );
25 my $style_path = $path . '.yaml';
26 $path .= '.png';
27
28 return ( $path, $self->load( $style_path ) ) if -e $path;
29
30 warn "# path $path";
31 $self->mkbasepath( $path );
32
33 # fixed dimensions of images combined
34 my ( $w, $h ) = ( 16, 16 );
35 my $padding = 5; # space between pictures when used within text
36 my $num_images = $#images + 1;
37
38 my $combined = GD::Image->new( $w, $num_images * $h )
39 || die "can't create $w x ",$num_images * ($h + $padding)," combined image";
40 $combined->trueColor( 1 );
41 $combined->saveAlpha( 1 );
42 my $white = $combined->colorAllocate(255,255,255);
43 $combined->transparent($white);
44
45 my $y = 0;
46
47 my $style;
48
49 foreach my $image_path ( @images ) {
50 my $i = GD::Image->new( $image_path ) || die "can't open $image_path: $!";
51 my $i_white = $i->colorAllocate(255,255,255);
52 $i->transparent( $i_white );
53 $i->saveAlpha( 1 );
54 $i->trueColor( 1 );
55
56 die "with not $w" unless $i->width == $w;
57 die "with not $h" unless $i->height == $h;
58
59 warn "# copy $image_path 0 x $y";
60 $combined->copy( $i, 0, $y, 0, 0, $w, $h );
61
62 $style->{$image_path}
63 = qq|<span style="|
64 . qq|background:url(/$path) no-repeat; |
65 . qq|background-position: 0px | . -$y . qq|px; |
66 . qq|padding: 0 0 0 | . ( $w + 3 ) . qq|px; |
67 . qq|width: ${w}px; height: ${h}px; |
68 . qq|"></span>|;
69 ;
70
71 $y += $h + $padding;
72 }
73
74 write_file( $path, $combined->png );
75 $self->store( $style_path, $style );
76 warn
77 "# combined $num_images into ",
78 $combined->width, "x", $combined->height, " pixels ",
79 $path, " ", -s $path , " bytes ",
80 " style $style_path ", -s $style_path, " bytes";
81
82 return ( $path, $style );
83 }
84
85 1;

  ViewVC Help
Powered by ViewVC 1.1.26