CSS-SpriteBuilder

 view release on metacpan or  search on metacpan

lib/CSS/SpriteBuilder.pm  view on Meta::CPAN

package CSS::SpriteBuilder;

use strict;
use warnings;
use File::Spec;
use Scalar::Util qw(openhandle);
use CSS::SpriteBuilder::Constants;
use CSS::SpriteBuilder::Sprite;

our $VERSION = '0.03';

sub new {
    my ($class, @args) = @_;

    my $self = bless {
        source_dir          => undef,
        output_dir          => undef,

        image_quality       => DEF_IMAGE_QUALITY,
        max_image_size      => DEF_MAX_IMAGE_SIZE,
        max_image_width     => DEF_MAX_IMAGE_WIDTH,
        max_image_height    => DEF_MAX_IMAGE_HEIGHT,
        max_sprite_width    => DEF_MAX_SPRITE_WIDTH,
        max_sprite_height   => DEF_MAX_SPRITE_HEIGHT,
        margin              => DEF_MARGIN,
        transparent_color   => undef,
        is_background       => 0,
        layout              => DEF_LAYOUT,
        css_selector_prefix => DEF_CSS_SELECTOR_PREFIX,
        css_url_prefix      => '',
        is_add_timestamp    => 1,

        _sprites            => {},

        @args,
    }, $class;

    return $self;
}

sub build {
    my ($self, %args) = @_;

    my $sprites;
    if ( my $config = delete $args{config} ) {
        $sprites = $self->_parse_xml_config($config);
    }
    else {
        $sprites = delete $args{sprites};
    }

    foreach my $sprite_opts (@$sprites) {
        my $images      = delete $sprite_opts->{images};
        my $output_file = $self->{output_dir}
            ? File::Spec->catfile( $self->{output_dir}, delete $sprite_opts->{file} )
            : delete $sprite_opts->{file}
        ;

        my $sprite = CSS::SpriteBuilder::Sprite->new(
            ( map { $_ => $self->{$_} } grep { exists $self->{$_} } @{ SPRITE_OPTS() } ),
            %$sprite_opts,
            source_images => $images,
            target_file   => $output_file,
        );

        my $sprites = $sprite->build();
        $self->{_sprites} = {%{ $self->{_sprites} }, %$sprites};
    }

    return $self->{_sprites};
}

sub get_sprites_info {
    my ($self) = @_;
    return $self->{_sprites};
}

sub write_css {
    my ($self, $filename) = @_;

    my ($fh, $str);

    if ($filename) {
        if ( openhandle($filename) ) {
            $fh = $filename;
        }

lib/CSS/SpriteBuilder.pm  view on Meta::CPAN

    $builder->build(config => 'config.xml');

    $build->write_css('sprite.css');

=head1 DESCRIPTION

This module generate CSS sprites with one of these modules: Image::Magick or GD.

It has many useful settings and can be used for sites with complex structure.

=head1 METHODS

=head2 new(<%args>)

my $builder = CSS::SpriteBuilder->new(<%args>);

Create instance.

Valid arguments are:

=over

=item * B<source_dir> [ = undef ]

Specify custom location for source images.

=item * B<output_dir> [ = undef ]

Specify custom location for generated images.

=item * B<image_quality> 0..100 [ = 90 ]

Specify image quality for generated images (for JPEG only).

=item * B<max_image_size> [ = 65536 ]

Specify max size of images that will be used to generate a sprite.

=item * B<max_image_width> [ = 2000 ]

Specify max width of images that will be used to generate a sprite.

=item * B<max_image_height> [ = 2000 ]

Specify max height of images that will be used to generate a sprite.

=item * B<max_sprite_width> [ = 2000 ]

Specify max width of sprite.
When sprite has no free space, than creates a new sprite with addition of a suffix to the sprite name.
Opera 9.0 and below have a bug which affects CSS background offsets less than -2042px. All values less than this are treated as -2042px exactly.

=item * B<max_sprite_height> [ = 2000 ]

Specify max height of sprite.

=item * B<margin> [ = 0 ]

Add margin to each image.

=item * B<transparent_color> [ = undef ]

Set transparent color for image, for example: 'white', 'black', ....

=item * B<is_background> [ = 0 ]

If B<is_background> flag is '0' will be generated CSS rule such as: 'width:XXXpx;height:YYYpx;'.

=item * B<layout> [ = 'packed' ]

Specify layout algorithm (horizontal, vertical or packed).

=item * B<css_selector_prefix> [ = '.spr-' ]

Specify CSS selector prefix.
For example, for an image "img/icon/arrow.gif" will be generated selector such as ".spr-img-icon-arrow".

=item * B<css_url_prefix> [ = '' ]

Specify prefix for CSS url.
For example: background-image: url('B<css_url_prefix>sample_sprite.png')

=item * B<is_add_timestamp> [ = 1 ]

If parameter set to '1' than timestamp will be added for CSS url.
For example: background-image: url('sample_sprite.png?12345678')

=back

=head2 build(<%args>)

Build sprites.

    $builder->build(<%args>);

This method returning structure like:

    {
        'sample_sprite_1.png' => [
            {
                'y' => 0,
                'width' => 32,
                'selector' => '.spr-small-add',
                'is_background' => 0,
                'x' => 0,
                'height' => 32,
                'image' => 'small/Add.png',
                'repeat' => 'no'
            },
            ...
        ],
        ...
    }

Valid arguments are:

=over

=item * B<sprites>

Specify sprite list.

    $builder->build(



( run in 0.761 second using v1.01-cache-2.11-cpan-39bf76dae61 )