Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Imager module with GIFs

Reply
Thread Tools

Imager module with GIFs

 
 
jwcarlton
Guest
Posts: n/a
 
      02-22-2011
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
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      02-22-2011
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

 
Reply With Quote
 
 
 
 
jwcarlton
Guest
Posts: n/a
 
      02-22-2011
> 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
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      02-22-2011
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.
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-23-2011
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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      02-23-2011
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
 
Reply With Quote
 
jwcarlton
Guest
Posts: n/a
 
      02-24-2011
On Feb 22, 3:19*pm, "J. Gleixner" <(E-Mail Removed)>
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.
 
Reply With Quote
 
jwcarlton
Guest
Posts: n/a
 
      02-24-2011
On Feb 22, 11:10*pm, "Dr.Ruud" <(E-Mail Removed)> 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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      02-24-2011
jwcarlton <(E-Mail Removed)> 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
 
Reply With Quote
 
jwcarlton
Guest
Posts: n/a
 
      02-24-2011
On Feb 23, 8:14*pm, J rgen Exner <(E-Mail Removed)> wrote:
> jwcarlton <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Imager module to resize a GIF Jason C Perl Misc 3 10-20-2011 02:55 PM
there are 3-imager video cameras, are there 3-imager still cameras? Bob Fleischer Digital Photography 14 02-07-2010 08:49 PM
Embedding Gifs, animated gifs and other html pages into HTML winstontuck@gmail.com HTML 1 09-13-2006 11:11 AM
making big GIFs and little GIFs Franklin Digital Photography 6 02-09-2005 09:00 PM
CCD Imager swnw Digital Photography 5 09-21-2003 07:05 AM



Advertisments