Imager module with GIFs

Discussion in 'Perl Misc' started by jwcarlton, Feb 22, 2011.

  1. jwcarlton

    jwcarlton Guest

    Is it possible to convert an image from GIF to JPG using the Imager
    module, or am I going to have to switch over to Image::Magick?

    Currently, converting PNG or BMP is working fine, it's just a GIF that
    gives me an error. Here's the code I'm using:

    ($filename, $ext) = $pic =~ m/(.*)\.(.*)/;

    my $image = Imager->new;
    $image->read(file => "$path/$pic");
    $image->write(file => "$path/$filename.jpg")) or
    die $image->errstr;


    When I upload a GIF, I get an error on the "write(file => ...)" line
    that says "Can't call method "write" on an undefined value".

    This is the same if I change the line to:

    $image->write(file => "$path/$pic")) or
    die $image->errstr;

    Which doesn't actually convert anything, but I still get an error when
    writing.

    TIA,

    Jason
    jwcarlton, Feb 22, 2011
    #1
    1. Advertising

  2. jwcarlton

    J. Gleixner Guest

    jwcarlton wrote:
    > Is it possible to convert an image from GIF to JPG using the Imager
    > module, or am I going to have to switch over to Image::Magick?
    >
    > Currently, converting PNG or BMP is working fine, it's just a GIF that
    > gives me an error. Here's the code I'm using:


    Where's the following:

    use Imager;
    use strict;
    use warnings;

    ???
    >
    > ($filename, $ext) = $pic =~ m/(.*)\.(.*)/;


    What's the value of $pic?

    >
    > my $image = Imager->new;


    What if new fails?
    my $image = Imager->new() or die Imager->errstr;

    > $image->read(file => "$path/$pic");


    What if the read fails?
    $image->read( file => "$path/$pic" ) or die $image->errstr . " When
    trying to read $path/$pic.";

    What's the value of $path?

    > $image->write(file => "$path/$filename.jpg")) or
    > die $image->errstr;
    >
    >
    > When I upload a GIF, I get an error on the "write(file => ...)" line


    Upload???..

    > that says "Can't call method "write" on an undefined value".


    That's not coming from it being a gif, jpg, etc. It's saying that
    $image is not defined.

    perldoc perldiag
    J. Gleixner, Feb 22, 2011
    #2
    1. Advertising

  3. jwcarlton

    jwcarlton Guest

    > Where's the following:
    >
    > use Imager;
    > use strict;
    > use warnings;
    >
    > ???


    I obviously didn't want to paste the entire script, just the relevant
    parts. I'm also using use CGI::Carp qw(fatalsToBrowser).


    > > ($filename, $ext) = $pic =~ m/(.*)\.(.*)/;

    >
    > What's the value of $pic?


    $pic is the file name of the uploaded image. In this case, it's found
    with the following:

    $old_pic = upload('pic');

    $pic = lc($old_pic);
    $pic =~ s {.*[\:\\\/]} []gos;
    $pic =~ s/[^A-Za-z0-9\._ \-=@\x80-\xFE]/_/go;
    $pic =~ s/ /_/g;

    open PIC, ">$path/$pic";
    binmode (PIC);
    while ($bytes = read($old_pic,$data,16384)) { print PIC $data; }
    close PIC;


    > > my $image = Imager->new;

    >
    > What if new fails?
    > my $image = Imager->new() or die Imager->errstr;
    >
    > > $image->read(file => "$path/$pic");

    >
    > What if the read fails?
    > $image->read( file => "$path/$pic" ) or die $image->errstr . " When
    > trying to read $path/$pic.";


    Neither are failing. I did add the die() statement here for testing,
    but the only error is on the write() line.


    > What's the value of $path?


    /home/mydomain/www/image


    > > $image->write(file => "$path/$filename.jpg")) or
    > >   die $image->errstr;

    >
    > > When I upload a GIF, I get an error on the "write(file => ...)" line

    >
    > Upload???..
    >
    > > that says "Can't call method "write" on an undefined value".

    >
    > That's not coming from it being a gif, jpg, etc. It's saying that
    > $image is not defined.
    >
    > perldoc perldiag


    I get that, but it's only happening when $pic is a GIF. On JPG, PNG,
    and BMP, it's writing with no problem.

    Thanks,

    Jason
    jwcarlton, Feb 22, 2011
    #3
  4. jwcarlton

    J. Gleixner Guest

    jwcarlton wrote:
    >> Where's the following:
    >>
    >> use Imager;
    >> use strict;
    >> use warnings;
    >>
    >> ???

    >
    > I obviously didn't want to paste the entire script, just the relevant
    > parts. I'm also using use CGI::Carp qw(fatalsToBrowser).


    Obviously... Ever read the posting guidelines?

    >
    >
    >>> ($filename, $ext) = $pic =~ m/(.*)\.(.*)/;

    >> What's the value of $pic?

    >
    > $pic is the file name of the uploaded image. In this case, it's found
    > with the following:
    >
    > $old_pic = upload('pic');


    etc..
    what does $image->write_types() report?

    Possibly you don't have the GIF library installed or when installing
    Imager it didn't find it. Did make test work?

    Check the README for hints on giflib.

    I'd suggest removing CGI from the equation and start with an
    image on your machine and make sure you can read/write to
    other formats, if that works, then it should work as a CGI,
    provided it works for other formats.
    J. Gleixner, Feb 22, 2011
    #4
  5. jwcarlton wrote:
    >
    > $pic = lc($old_pic);
    > $pic =~ s {.*[\:\\\/]} []gos;


    You have no variables in that pattern so the /o option is superfluous.

    perldoc -q /o


    > $pic =~ s/[^A-Za-z0-9\._ \-=@\x80-\xFE]/_/go;


    Same as above. The characters A-Z, a-z, 0-9 and _ can be abbreviated
    with \w.


    > $pic =~ s/ /_/g;


    Probably better as:

    $pic =~ tr/ /_/;


    > open PIC, ">$path/$pic";
    > binmode (PIC);
    > while ($bytes = read($old_pic,$data,16384)) { print PIC $data; }


    If read() only returns one byte and that byte contains "0" then your
    loop will exit prematurely.


    > close PIC;




    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
    John W. Krahn, Feb 23, 2011
    #5
  6. jwcarlton

    Dr.Ruud Guest

    On 2011-02-23 04:48, John W. Krahn wrote:
    > jwcarlton wrote:


    >> $pic =~ s/[^A-Za-z0-9\._ \-=@\x80-\xFE]/_/go;

    >
    > [...] The characters A-Z, a-z, 0-9 and _ can be abbreviated
    > with \w.


    Be careful with that advice. A \w matches many, many code points.

    --
    Ruud
    Dr.Ruud, Feb 23, 2011
    #6
  7. jwcarlton

    jwcarlton Guest

    On Feb 22, 3:19 pm, "J. Gleixner" <>
    wrote:
    > jwcarlton wrote:
    > >> Where's the following:

    >
    > >> use Imager;
    > >> use strict;
    > >> use warnings;

    >
    > >> ???

    >
    > > I obviously didn't want to paste the entire script, just the relevant
    > > parts. I'm also using use CGI::Carp qw(fatalsToBrowser).

    >
    > Obviously...  Ever read the posting guidelines?
    >
    >
    >
    > >>> ($filename, $ext) = $pic =~ m/(.*)\.(.*)/;
    > >> What's the value of $pic?

    >
    > > $pic is the file name of the uploaded image. In this case, it's found
    > > with the following:

    >
    > > $old_pic = upload('pic');

    >
    > etc..
    > what does $image->write_types() report?
    >
    > Possibly you don't have the GIF library installed or when installing
    > Imager it didn't find it.  Did make test work?
    >
    > Check the README for hints on giflib.
    >
    > I'd suggest removing CGI from the equation and start with an
    > image on your machine and make sure you can read/write to
    > other formats, if that works, then it should work as a CGI,
    > provided it works for other formats.


    You're right on this one. I took for granted that giflib installed
    with Imager, but apparently not.

    I found giflib on SourceForge, but I have no clue how to install it.
    I've been looking everywhere for installation instructions (at least
    something with little SSH experience can follow), but can't find
    anything :-(

    I hate to ask this type of question, but can you point me in the right
    direction on getting started? This is a live, active server with
    minimal tech support, so if I mess up, I could be down for awhile.
    jwcarlton, Feb 24, 2011
    #7
  8. jwcarlton

    jwcarlton Guest

    On Feb 22, 11:10 pm, "Dr.Ruud" <> wrote:
    > On 2011-02-23 04:48, John W. Krahn wrote:
    >
    > > jwcarlton wrote:
    > >> $pic =~ s/[^A-Za-z0-9\._ \-=@\x80-\xFE]/_/go;

    >
    > > [...] The characters A-Z, a-z, 0-9 and _ can be abbreviated
    > > with \w.

    >
    > Be careful with that advice. A \w matches many, many code points.
    >
    > --
    > Ruud


    Thanks for the tips. Sherm, I had no idea that people had started
    going back to the 3-argument form; I thought that was old school! LOL
    Is there a technical/speed advantage? Or is it just one of those
    things?

    Off topic, I learned recently that, in school, kids are now being
    taught "mean" instead of "average". My parents learned "mean", but we
    learned "average", and now kids are back to "mean". Education goes in
    circles, I guess :)
    jwcarlton, Feb 24, 2011
    #8
  9. jwcarlton <> wrote:
    >I hate to ask this type of question, but can you point me in the right
    >direction on getting started? This is a live, active server with
    >minimal tech support, so if I mess up, I could be down for awhile.


    Aeeehhhm, excuse me for asking stupid questions, but are you hinting at
    that you are developing code on a live server?

    jue
    Jürgen Exner, Feb 24, 2011
    #9
  10. jwcarlton

    jwcarlton Guest

    On Feb 23, 8:14 pm, J rgen Exner <> wrote:
    > jwcarlton <> wrote:
    > >I hate to ask this type of question, but can you point me in the right
    > >direction on getting started? This is a live, active server with
    > >minimal tech support, so if I mess up, I could be down for awhile.

    >
    > Aeeehhhm, excuse me for asking stupid questions, but are you hinting at
    > that you are developing code on a live server?
    >
    > jue


    Yup. I have 2 servers; one is used for shared accounts, and the other
    (this one) is used for this one site. So, I don't have a non-live
    server to test with.
    jwcarlton, Feb 24, 2011
    #10
  11. jwcarlton

    Uri Guttman Guest

    >>>>> "SP" == Sherm Pendley <> writes:

    SP> jwcarlton <> writes:
    >> Sherm, I had no idea that people had started
    >> going back to the 3-argument form; I thought that was old school!


    SP> You thought wrong. It's the newer form, and has been preferred since it
    SP> was introduced; 2-argument open() is supported only for the sake of
    SP> compatibility with old scripts.

    not exactly. i doubt it would ever be dropped. there are some useful
    things you can do with 2 arg open that you can't with 3 arg. you can
    pass in a file name with a | (leading or trailing depending on needs)
    and run a program instead of opening a file. the caller can do that only
    if it is 2 arg open. with 3 arg the caller would need to specify an
    extra arg in the api. not a big win but it is useful for example to pass
    in a compressed file with the decompressor as its filter. the perl open
    never needs to know about the compression.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Feb 24, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    581
    Travis Newbury
    Sep 13, 2006
  2. Andrew

    Raw Imager

    Andrew, Feb 19, 2007, in forum: Python
    Replies:
    0
    Views:
    348
    Andrew
    Feb 19, 2007
  3. imager sample for text

    , Jan 17, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    99
  4. joe

    Imager with UTF8

    joe, Nov 25, 2008, in forum: Perl Misc
    Replies:
    3
    Views:
    102
    RedGrittyBrick
    Nov 25, 2008
  5. Jason C

    Imager module to resize a GIF

    Jason C, Oct 19, 2011, in forum: Perl Misc
    Replies:
    3
    Views:
    480
    J. Gleixner
    Oct 20, 2011
Loading...

Share This Page