From 504721f30ef723739a4e772a8945c1b4e19e10a3 Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Thu, 6 Dec 2018 20:43:18 +1100 Subject: [PATCH] add some more extensions, add add_type_extensions() class method --- Imager.pm | 15 ++++++++++++++- lib/Imager/Files.pod | 9 +++++++++ t/200-file/100-files.t | 8 +++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Imager.pm b/Imager.pm index dc22d036..9d3fb0d7 100644 --- a/Imager.pm +++ b/Imager.pm @@ -4222,7 +4222,7 @@ sub _set_error { # Default guess for the type of an image from extension -my @simple_types = qw(png tga gif raw ico cur xpm mng jng ilbm pcx psd eps); +my @simple_types = qw(png tga gif raw ico cur xpm mng jng ilbm pcx psd eps webp xwd xpm dng ras); my %ext_types = ( @@ -4257,6 +4257,15 @@ sub def_guess_type { return $type; } +sub add_type_extensions { + my ($class, $type, @exts) = @_; + + for my $ext (@exts) { + exists $ext_types{lc $ext} or $ext_types{lc $ext} = lc $type; + } + 1; +} + sub combines { return @combine_types; } @@ -4736,6 +4745,10 @@ paletted image addtag() - L - add image tags +add_type_extensions() - +L - add extensions for +new image file types. + align_string() - L - draw text aligned on a point diff --git a/lib/Imager/Files.pod b/lib/Imager/Files.pod index 1b893a1d..d61df400 100644 --- a/lib/Imager/Files.pod +++ b/lib/Imager/Files.pod @@ -1853,6 +1853,15 @@ all the parameters supplied to the read_multi() method. =back +=item add_type_extensions($type, $ext, ...) + +This class method can be used to add extensions to the map used by +C when working out the file type a filename extension. + + Imager->add_type_extension(mytype => "mytype", "mytypish"); + ... + $im->write(file => "foo.mytypish") # use the mytype handler + =back If you name the reader module CI diff --git a/t/200-file/100-files.t b/t/200-file/100-files.t index a4c66a44..928dce32 100644 --- a/t/200-file/100-files.t +++ b/t/200-file/100-files.t @@ -4,7 +4,7 @@ # the file format use strict; -use Test::More tests => 94; +use Test::More; use Imager; -d "testout" or mkdir "testout"; @@ -342,6 +342,7 @@ FLIF ILBM => "ilbm", pcx => "pcx", psd => "psd", + webp => "webp", ); while (my ($ext, $expect) = splice(@tests, 0, 2)) { @@ -349,10 +350,15 @@ FLIF is(Imager::def_guess_type($filename), $expect, "type for $filename should be $expect"); } + Imager->add_type_extensions("x123", "x321"); + is(Imager::def_guess_type("foo.x321"), "x123", + "test adding a file type works"); } Imager->close_log; +done_testing(); + unless ($ENV{IMAGER_KEEP_FILES}) { unlink "testout/t1000files.log"; } -- 2.39.5