thekestrel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks
I'm making a web-based form whereby a user can upload an image and it will thumbnail it and then add it into a database.
I have the uploading section working and I can send it to the database. I wrote a seperate script to test out Thumbnailing using Image::Magick and can load a file from the harddrive, thumbnail it and thne write the new image back to disk.
The problem I'm having is putting the two pieces together.

This is the first snippet uses CGI that does the uploading and puts it in the database;
... my $fh = upload('image'); if ( !$cover_image && cgi_error ) { print header(-status=>cgi_error); } my $image; while (<$fh>) { $image .=$_; } my $num = insert_image_db($dbh, $user, $type, $image_title, $image); ...


...and the script that makes the thumbnail...
#!/usr/bin/perl use strict; use warnings; use Image::Magick; my $image = Image::Magick->new; open (IMAGE, 'xyz.jpg'); $image->Read(file=>\*IMAGE); close(IMAGE); $image->Thumbnail(geometry=>'150x150'); $image->Write(filename=>'xyz2.jpg');

The thing is the Read method for the thumbnailing only takes files or filehandles as inputs and my other script already has the data loaded. I don't want to have to write the image to disk just so I can load it to shrink it.

Is there a easy way of using the data I already have from the upload and make it a filehandle or is there a better was of doing this. I thought that I could open a pipe push the image down it and then I would have a filehandle at the other end to catch it with, but this seems a bit longwinded.

Has anyone come across this before, is there a strightforward method for doing something I'm not familiar with? I appreciate and advice.

Regards Paul

Replies are listed 'Best First'.
Re: Thumbnails and Filehandles
by holli (Abbot) on Jul 10, 2005 at 06:28 UTC
    Well, you already have a filehandle from the upload, namely $fh. Just feed that to the Read method.
    use strict; use warnings; use Image::Magick; my $image = Image::Magick->new; my $fh = upload('image'); $image->Read(file=>$fh); $image->Thumbnail(geometry=>'150x150'); $image->Write(filename=>'xyz2.jpg');


    holli, /regexed monk/
      Holli,
      Thanks for the feedback. When I need to then write the data back out (i.e. just send the raw image to a database) how do I get a hold of it, is it possible to use the $image->Write???

      Regards Paul.

      Update: I can't seem to get the method you use of passing the filehandle ($fh) to 'Read' to work. I added a little to the script you made so it should print the raw data to the screen when viewed via a browser...
      #!/usr/bin/perl use strict; use warnings; use Image::Magick; use CGI qw/:standard/; my $cgi = new CGI; my $file = $cgi->param('image'); if ( !$file ) { print header,start_multipart_form(), filefield('image',50,80), submit(-name=>'upload', -value=>'Upload'), end_form(); } else { print header; print "<br>FILE: $file\n"; my $image = Image::Magick->new; my $fh = upload('image'); $image->Read(file=>$fh); my $big_image = $image->ImageToBlob(); print "<br>DATA: $big_image\n"; }
Re: Thumbnails and Filehandles
by zentara (Cardinal) on Jul 10, 2005 at 10:51 UTC
    Just for discussion, ImageMagick can read from and write to a scalar, it is called ImageToBlob and BlobToImage. The docs for ImageMagick suck, and make this simple function hard to find. In your case, when you upload the file, you could save it to a scalar, print it to file, then use the same scalar blob to make a thumbnail and write it to file, or store the blob directly in the database.

    I'm not really a human, but I play one on earth. flash japh
      Another option would be to use IO::Scalar to create a filehandle from the scalar and pass that to $image->Read().