/[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

Annotation of /trunk/lib/Frey/Web/CombineImages.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 557 - (hide annotations)
Thu Nov 27 19:19:01 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 2008 byte(s)
Ported from GD to Imager after I googled a bit about GD transparency
problem (which manifested in black icon background).

Imager also solved image quality problems :-)
1 dpavlin 555 package Frey::Web::CombineImages;
2     use Moose::Role;
3    
4     with 'Frey::Path';
5     with 'Frey::Storage';
6    
7 dpavlin 557 use Imager;
8 dpavlin 555 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 dpavlin 557 my $combined = Imager->new(
39     xsize => $w,
40     ysize => $num_images * ( $h + $padding ),
41     channels => 4, # RGB+alpha
42     ) || die "can't create $w x ",$num_images * ($h + $padding)," combined image";
43 dpavlin 555
44     my $y = 0;
45    
46     my $style;
47    
48     foreach my $image_path ( @images ) {
49 dpavlin 557 my $i = Imager->new;
50     $i->read( file => $image_path ) || die "can't open $image_path: $!";
51 dpavlin 555
52 dpavlin 557 die "with not $w" unless $i->getwidth == $w;
53     die "with not $h" unless $i->getheight == $h;
54 dpavlin 555
55 dpavlin 557 warn "# copy $image_path 0 x $y" if $self->debug;
56     $combined->paste( img => $i, left => 0, top => $y );
57 dpavlin 555
58     $style->{$image_path}
59     = qq|<span style="|
60 dpavlin 557 . qq|background: url(/$path) no-repeat 0px | . -$y . qq|px; |
61 dpavlin 555 . qq|padding: 0 0 0 | . ( $w + 3 ) . qq|px; |
62     . qq|width: ${w}px; height: ${h}px; |
63 dpavlin 557 . qq|"> </span>|;
64 dpavlin 555 ;
65    
66     $y += $h + $padding;
67     }
68    
69 dpavlin 557 $combined->write( file => $path ) || die "can't write $path: $!";
70     $self->store( $style_path, $style ) || die "can't store $style_path: $!";
71 dpavlin 555 warn
72 dpavlin 557 "# combined $num_images ${w}x${h} images into ",
73     $combined->getwidth, "x", $combined->getheight, " ",
74 dpavlin 555 $path, " ", -s $path , " bytes ",
75     " style $style_path ", -s $style_path, " bytes";
76    
77     return ( $path, $style );
78     }
79    
80     1;

  ViewVC Help
Powered by ViewVC 1.1.26