NAME
SVG::SpriteMaker - Combine several SVG images into a single SVG sprite
SYNOPSIS
use File::Slurp qw/write_file/;
use SVG::SpriteMaker;
my $sprite = make_sprite img => '1.svg', '2.svg', '3.svg';
write_file 'sprite.svg', $sprite->xmlify;
# You can now use <img src="sprite.svg#img-1" alt="...">
my @images = <dir/*>; # dir/ImageA.svg dir/ImageB.svg
$sprite = make_sprite sub {
my ($name) = $_[0] =~ m,/([^/.]*),;
uc $name
}, @images; # Sprite will have identifiers #IMAGEA #IMAGEB
DESCRIPTION
A SVG sprite is a SVG image that contains several smaller images that can be referred to using fragment identifiers. For example, this HTML fragment:
<img src="/img/cat.svg" alt="A cat">
<img src="/img/dog.svg" alt="A dog">
<img src="/img/horse.svg" alt="A horse">
Can be replaced with
<img src="/img/sprite.svg#cat" alt="A cat">
<img src="/img/sprite.svg#dog" alt="A dog">
<img src="/img/sprite.svg#horse" alt="A horse">
This module exports a single function:
make_sprite($prefix|$coderef, @files)
Takes a list of filenames, combines them and returns the resulting sprite as a SVG object. Each SVG must have width and height attributes whose values are in pixels.
If the first argument is a coderef, it will be called with each filename as a single argument and it should return the desired fragment identifier.
If the first argument is not a coderef, the following coderef will be used:
sub {
my $base = scalar fileparse $_[0], qr/\..*/s;
"$prefix-$base"
};
where $prefix is the value of the first argument.
If an ID is shared between two or more input files, this module will try to rename each occurence except for the first one. This operation might have false positives (attributes/cdatas that are mistakenly identified to contain the ID-to-be-renamed) and false negatives (attributes/cdatas that actually contain the ID-to-be-renamed but this is missed by the module), and as such SVG::SpriteMaker will warn if duplicate IDs are detected. You can suppress this warning by setting the SVG_SPRITEMAKER_NO_DUPLICATE_WARNINGS
environment variable to a true value.
SEE ALSO
svg-spritemaker, https://css-tricks.com/svg-fragment-identifiers-work/
AUTHOR
Marius Gavrilescu, <marius@ieval.ro>
COPYRIGHT AND LICENSE
Copyright (C) 2015-2017 by Marius Gavrilescu
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.20.2 or, at your option, any later version of Perl 5 you may have available.