Imager module with GIFs

J

jwcarlton

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
 
J

J. Gleixner

jwcarlton said:
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

jwcarlton

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).

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;

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


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




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
 
J

J. Gleixner

jwcarlton said:
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?
$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

John W. Krahn

jwcarlton said:
$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
 
J

jwcarlton

Obviously...  Ever read the posting guidelines?






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.
 
J

jwcarlton

jwcarlton said:
$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.

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 :)
 
J

Jürgen Exner

jwcarlton said:
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

jwcarlton

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.
 
U

Uri Guttman

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top