3 |
|
|
4 |
extends 'Frey'; |
extends 'Frey'; |
5 |
with 'Frey::Web'; |
with 'Frey::Web'; |
6 |
with 'Frey::Path'; |
with 'Frey::Web::CombineImages'; |
7 |
with 'Frey::Storage'; |
|
8 |
|
use Data::Dump qw/dump/; |
9 |
|
|
10 |
has path => ( |
has path => ( |
11 |
is => 'rw', |
is => 'rw', |
14 |
default => 'static/icons/fugue/', |
default => 'static/icons/fugue/', |
15 |
); |
); |
16 |
|
|
|
use GD; |
|
|
use Digest::MD5 qw/md5_hex/; |
|
|
use File::Slurp; |
|
|
use Data::Dump qw/dump/; |
|
|
|
|
|
=head2 combine_image |
|
|
|
|
|
Combine images into CSS sprites |
|
|
|
|
|
my ( $combined_path, $styles ) = $self->combine_image( @paths ); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub combine_image { |
|
|
my $self = shift; |
|
|
my @images = @_; |
|
|
|
|
|
my $path = 'var/combine/' . md5_hex( join(' ',@images ) ); |
|
|
my $style_path = $path . '.yaml'; |
|
|
$path .= '.png'; |
|
|
|
|
|
return ( $path, $self->load( $style_path ) ) if -e $path; |
|
|
|
|
|
warn "# path $path"; |
|
|
$self->mkbasepath( $path ); |
|
|
|
|
|
# fixed dimensions of images combined |
|
|
my ( $w, $h ) = ( 16, 16 ); |
|
|
my $num_images = $#images + 1; |
|
|
|
|
|
my $combined = GD::Image->new( $w, $num_images * $h ) |
|
|
|| die "can't create $w x ",$num_images * $h," combined image"; |
|
|
$combined->trueColor( 1 ); |
|
|
$combined->saveAlpha( 1 ); |
|
|
my $white = $combined->colorAllocate(255,255,255); |
|
|
$combined->transparent($white); |
|
|
|
|
|
my $y = 0; |
|
|
|
|
|
my $style; |
|
|
|
|
|
foreach my $image_path ( @images ) { |
|
|
my $i = GD::Image->new( $image_path ) || die "can't open $image_path: $!"; |
|
|
$i->trueColor( 1 ); |
|
|
$i->saveAlpha( 1 ); |
|
|
# $i->transparent($white); |
|
|
|
|
|
die "with not $w" unless $i->width == $w; |
|
|
die "with not $h" unless $i->height == $h; |
|
|
|
|
|
warn "# copy $image_path 0 x $y"; |
|
|
$combined->copy( $i, 0, $y, 0, 0, $w, $h ); |
|
|
|
|
|
$style->{$image_path} |
|
|
= qq|background:url(/$path) no-repeat; padding:0 0 0 | . ( $h - 1 ) . qq|px; background-position:0 | |
|
|
. ( $y ? -$y . 'px' : 0 ) |
|
|
; |
|
|
|
|
|
$y += $h; |
|
|
} |
|
|
|
|
|
write_file( $path, $combined->png ); |
|
|
$self->store( $style_path, $style ); |
|
|
warn |
|
|
"# combined $num_images into ", |
|
|
$combined->width, "x", $combined->height, " pixels ", |
|
|
$path, " ", -s $path , " bytes ", |
|
|
" style $style_path ", -s $style_path, " bytes"; |
|
|
|
|
|
return ( $path, $style ); |
|
|
} |
|
|
|
|
17 |
sub as_markup { |
sub as_markup { |
18 |
my ($self) = @_; |
my ($self) = @_; |
19 |
|
|
23 |
my @icons = sort grep { m/$extension/ } readdir($dir); |
my @icons = sort grep { m/$extension/ } readdir($dir); |
24 |
closedir $dir; |
closedir $dir; |
25 |
|
|
26 |
my ( $combined_path, $styles ) = $self->combine_image( map { $self->path . '/' . $_ } @icons ); |
my ( $combined_path, $styles ) = $self->combine_images( map { $self->path . '/' . $_ } @icons ); |
27 |
|
|
28 |
my $html; |
my $html; |
29 |
my $base; |
my $base; |
33 |
$name =~ s{$extension}{}; |
$name =~ s{$extension}{}; |
34 |
|
|
35 |
$base ||= $name; # seed with first |
$base ||= $name; # seed with first |
36 |
my $desc; |
my $desc = ''; |
37 |
|
|
38 |
my $bl = length $base; |
my $bl = length $base; |
39 |
|
|
53 |
|
|
54 |
my $path = $self->path . '/' . $icon; |
my $path = $self->path . '/' . $icon; |
55 |
|
|
56 |
my $style = $styles->{$path} || die "can't find style for $path in ",dump( $styles ); |
my $pic = $styles->{$path} || die "can't find pic for $path in ",dump( $styles ); |
57 |
$html .= qq|<span style="$style" title="$icon"> <b>$name</b>$desc</span>\n|; |
$html .= qq|<span title="$icon">$pic <b>$name</b>$desc</span>\n|; |
58 |
} |
} |
59 |
|
|
60 |
|
|