add some more extensions, add add_type_extensions() class method
authorTony Cook <tony@develop-help.com>
Thu, 6 Dec 2018 09:43:18 +0000 (20:43 +1100)
committerTony Cook <tony@develop-help.com>
Thu, 6 Dec 2018 09:43:18 +0000 (20:43 +1100)
Imager.pm
lib/Imager/Files.pod
t/200-file/100-files.t

index dc22d0363eff72931a781fdb4bde894a146653c6..9d3fb0d71caf09a601212cd62167a31e04cbb6f5 100644 (file)
--- 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<Imager::ImageTypes/addtag()> - add image tags
 
+add_type_extensions() -
+L<Imager::Files/add_type_extensions($type, $ext, ...)> - add extensions for
+new image file types.
+
 align_string() - L<Imager::Draw/align_string()> - draw text aligned on a
 point
 
index 1b893a1d92a2d4b30d44f1a668e3ac9c87857037..d61df400d02727020f6c196ccbcb4377ca6378e7 100644 (file)
@@ -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<def_guess_type> 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 C<Imager::File::>I<your-format-name>
index a4c66a444b18eda86915686b853b409ec55e6161..928dce323ed8fcbfa980059bfdcdcf5d1055f4cc 100644 (file)
@@ -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";
 }