Skip Menu |

This queue is for tickets about the Image-Size CPAN distribution.

Report information
The Basics
Id: 7055
Status: resolved
Priority: 0/
Queue: Image-Size

People
Owner: rjray [...] blackperl.com
Requestors: SREZIC [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 2.992
Fixed in: (no value)



Subject: Defer loading of Image::Magick
Hello Randy, I think the loading of Image::Magick should be moved from BEGIN{} to the point where it is actually necessary to try Image::Magick as a last ressort. It seems to me that Image::Size's native methods cover the most popular image formats, so I think that the fallback to Image::Magick will be rarely necessary in the real world. Regards, Slaven
From: mark [...] summersault.com
[SREZIC - Thu Jul 22 10:27:05 2004]: Show quoted text
> Hello Randy, > > I think the loading of Image::Magick should be moved from BEGIN{} to > the point where it is actually necessary to try Image::Magick as a > last ressort. It seems to me that Image::Size's native methods > cover the most popular image formats, so I think that the fallback > to Image::Magick will be rarely necessary in the real world. > > Regards, > Slaven
Attached is a patch I believe fixes this, as well as adding support for Graphics::Magic, which has been more stable for me. I have included one automated test, but more would be nice.
Mon Feb 7 22:30:18 EST 2005 Mark Stosberg <mark@summersault.com> * delay Image::Magick module loading, and add Graphics::Magick support diff -rN -u Image-Size-2.992-old/ChangeLog Image-Size-2.992-new/ChangeLog --- Image-Size-2.992-old/ChangeLog 2005-02-07 22:32:14.000000000 -0500 +++ Image-Size-2.992-new/ChangeLog 2005-02-07 22:30:14.000000000 -0500 @@ -1,5 +1,11 @@ CHANGE HISTORY +* Delay Image::Magick loading until it is needed. (Mark Stosberg) +* Add support for Graphics::Magick as an alternative to Image::Magick. + If either Graphics::Magick or Image::Magick is loaded into memory + that module will be used. Otherwise, they are both tried to be loaded, + with Graphics::Magick being tried first. (Mark Stosberg). + Changes in 2.992: * Added support for FlashMX (Shockwave Flash ver. 6), as contributed by diff -rN -u Image-Size-2.992-old/MANIFEST Image-Size-2.992-new/MANIFEST --- Image-Size-2.992-old/MANIFEST 2005-02-07 22:32:14.000000000 -0500 +++ Image-Size-2.992-new/MANIFEST 2005-02-07 21:54:00.000000000 -0500 @@ -18,3 +18,4 @@ t/bexjdic.tif t/yasp.swf t/468x60.psd +t/magick.t diff -rN -u Image-Size-2.992-old/Makefile.PL Image-Size-2.992-new/Makefile.PL --- Image-Size-2.992-old/Makefile.PL 2005-02-07 22:32:14.000000000 -0500 +++ Image-Size-2.992-new/Makefile.PL 2005-02-07 21:53:40.000000000 -0500 @@ -8,7 +8,7 @@ (ABSTRACT => 'A library to extract height/width from images', AUTHOR => 'Randy J. Ray (rjray@blackperl.com)') : ()), dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz' }, - PREREQ_PM => { File::Spec => 0 }, + PREREQ_PM => { File::Spec => 0, Test::More => 0, }, EXE_FILES => ['imgsize'], clean => { FILES => 'Image-Size-* *.spec *.rpm rpmrc rpmmacro' }, diff -rN -u Image-Size-2.992-old/Size.pm Image-Size-2.992-new/Size.pm --- Image-Size-2.992-old/Size.pm 2005-02-07 22:32:14.000000000 -0500 +++ Image-Size-2.992-new/Size.pm 2005-02-07 22:19:46.000000000 -0500 @@ -26,7 +26,7 @@ use AutoLoader 'AUTOLOAD'; require Exporter; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $revision $VERSION $NO_CACHE - %PCD_MAP $PCD_SCALE $read_in $last_pos *imagemagick_size); + %PCD_MAP $PCD_SCALE $read_in $last_pos ); BEGIN { @@ -39,34 +39,6 @@ $revision = q$Id: Size.pm,v 1.35 2003/07/21 06:48:47 rjray Exp $; $VERSION = "2.992"; - # Check if we have Image::Magick available - eval { - local $SIG{__DIE__}; # protect against user installed die handlers - require Image::Magick; - }; - if ($@) { - *imagemagick_size = - sub - { - (undef, undef, "Data stream is not a known image file format"); - }; - } else { - *imagemagick_size = - sub - { - my ($file_name) = @_; - my $img = Image::Magick->new(); - my $x = $img->Read($file_name); - # Image::Magick error handling is a bit weird, see - # <http://www.simplesystems.org/ImageMagick/www/perl.html#erro> - if("$x") { - return (undef, undef, "$x"); - } else { - return ($img->Get('width', 'height', 'format')); - } - }; - } - } # This allows people to specifically request that the cache not be used @@ -260,6 +232,55 @@ return (wantarray) ? ($x, $y, $id) : (); } +sub imagemagick_size { + my $module_name; + # First see if we have already loaded Graphics::Magick or Image::Magick + # If so, just use whichever one is already loaded. + if (exists $INC{'Graphics/Magick.pm'}) { + $module_name = 'Graphics::Magick'; + } + elsif (exists $INC{'Image/Magick.pm'}) { + $module_name = 'Image::Magick'; + } + + # If neither are already loaded, try loading either one. + elsif ( _load_magick_module('Graphics::Magick') ) { + $module_name = 'Graphics::Magick'; + } + elsif ( _load_magick_module('Image::Magick') ) { + $module_name = 'Image::Magick'; + } + + if ($module_name) { + my ($file_name) = @_; + my $img = $module_name->new(); + my $x = $img->Read($file_name); + # Image::Magick error handling is a bit weird, see + # <http://www.simplesystems.org/ImageMagick/www/perl.html#erro> + if("$x") { + return (undef, undef, "$x"); + } else { + return ($img->Get('width', 'height', 'format')); + } + + } + else { + return (undef, undef, "Data stream is not a known image file format"); + } + +} + +# load Graphics::Magick or Image::Magick if one is not already loaded. +sub _load_magick_module { + my $module_name = shift; + eval { + local $SIG{__DIE__}; + require $module_name; + }; + return !$@; +} + + sub html_imgsize { my @args = imgsize(@_); diff -rN -u Image-Size-2.992-old/t/magick.t Image-Size-2.992-new/t/magick.t --- Image-Size-2.992-old/t/magick.t 1969-12-31 19:00:00.000000000 -0500 +++ Image-Size-2.992-new/t/magick.t 2005-02-07 22:24:28.000000000 -0500 @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +# Tests related to Image::Magick and Graphics::Magick + + +BEGIN: { + use Test::More tests => 2; + use_ok('Image::Size'); +} + +# This test should work whether or not Image::Magick is installed. +ok(!(exists $INC{'Image/Magick.pm'}), 'Image::Magick should not be loaded until it is needed if it available') + || diag "Image::Magick loaded at: $INC{'Image/Magick.pm'}"; + + + + +
From: srezic [...] cpan.org
[guest - Mon Feb 7 22:36:47 2005]: Show quoted text
> [SREZIC - Thu Jul 22 10:27:05 2004]: >
> > Hello Randy, > > > > I think the loading of Image::Magick should be moved from BEGIN{} to > > the point where it is actually necessary to try Image::Magick as a > > last ressort. It seems to me that Image::Size's native methods > > cover the most popular image formats, so I think that the fallback > > to Image::Magick will be rarely necessary in the real world. > > > > Regards, > > Slaven
> > Attached is a patch I believe fixes this, as well as adding support for > Graphics::Magic, which has been more stable for me.
Looks OK to me. Maybe instead of testing for $INC{Modulename} the code could rather test for the existance of actual code, e.g. with if (Image::Magick->can("new")) { ... } But this is really optional. Regards, Slaven
From: mark [...] summersault.com
[SREZIC - Mon Feb 14 11:29:26 2005]: Show quoted text
> > Looks OK to me. Maybe instead of testing for $INC{Modulename} > the code could rather test for the existance of actual code, e.g. > with > > if (Image::Magick->can("new")) { ... }
I have in mind the persistent mod_perl environment. I have to load Image::Magick into memory for that test, which may cause a problem if Graphics::Magick is already loaded. Are there cases where checking %INC is known to be wrong?
[guest - Thu Feb 17 07:58:35 2005]: Show quoted text
> [SREZIC - Mon Feb 14 11:29:26 2005]:
> > > > Looks OK to me. Maybe instead of testing for $INC{Modulename} > > the code could rather test for the existance of actual code, e.g. > > with > > > > if (Image::Magick->can("new")) { ... }
> > I have in mind the persistent mod_perl environment. I have to load > Image::Magick into memory for that test,
No, this is not true. ->can may be used on non-existing or not yet loaded packages: $ perl -e 'warn FooBar->can("bla")' Warning: something's wrong at -e line 1. Show quoted text
> which may cause a problem if > Graphics::Magick is already loaded. > > Are there cases where checking %INC is known to be wrong?
A user may set $INC{...} himself for some neat tricks. Regards, Slaven
Patch applied and massaged to work with other changes made. This will be delivered in version 3.0.