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

I am trying to upload both a full pdf and a jpg thumbnail of the first page to a MySQL data base but I am having no luck. I am using the following sub routine:
sub read_image_file { my $fh = shift; # filename/file handle my $img = new Image::Magick; my ($full, $thumb, $err); # read full-size image directly from upload file (read ($fh, $full, -s $fh) == -s $fh) or error ("Can't read image file: $!"); # produce jpg from pdf (this is where i'm lost because i get a can +'t convert error) # produce thumbnail from full-size image $err = $img->BlobToImage ($full); error ("Can't convert image data: $err") if $err; $err = $img->Scale (geometry => "150x194"); error ("Can't scale image file: $err") if $err; $thumb = $img->ImageToBlob (); return ($full, $thumb); }
This works ok with images but not with PDFs. My guess is I need to do some kind of conversion to the PDF but can figure out how. Thanks in advance for any help. Rich

Replies are listed 'Best First'.
Re: PDF to JPG for MySQL
by ww (Archbishop) on Jun 26, 2008 at 04:09 UTC
    Check out Image::Magick::Thumbnail::PDF (or, if using W32, Active State, and ppm, Image-Magick-Thumbnail-PDF).
Re: PDF to JPG for MySQL
by leocharre (Priest) on Jun 26, 2008 at 12:46 UTC

    As ww mentioned, this should work;

    use Image::Magick::Thumbnail::PDF 'create_thumbnail'; my $abs_thumb = create_thumbnail('/home/myself/mypdfile.pdf');

    If you have problems installing or trying it out, I can help you, I wrote the thing, PDF::OCR- I work with pdf/images a bunch at work.

    My question is by golly why are you doing this... I mean.. I know you *can* .. but by Richard Stallman's beard oh why!!!

    You're not gonna have a lot of records on this table are you? This could be a slow database or.. just huge.. if there are lot of records.

    I would look into that if you haven't already. How it scales.. I suppose.

      I agree. It's usually not a good idea to store actual FILES in a database (PDFs, images, movies, whatever). Instead, store the files in a secure data directory (i.e., not under your web root, if you want controlled access), then store the PATH in the database. When you need access, pull the path out of the DB, then fetch the file from that path, then stream it out to the client.
        Yeah, I mean... A database is a place where you want to store stuff that relates to itself somehow.. To each other, the records.. Like, you wanna see that the top 20 somethings are.

        You could save image metadata, pdf data, etc.. But the binary data???? (You know that pdfs can store thumbs inside themselves, right? You could code to make sure that such are present.. if not, make them and inject them into the pdf file )

        That's more what regular filesystems are for.

        Sure, a filesystem itself is a kind of database, it holds stuff - It's great if you know where the stuff is (the regular filesystem I mean ), but when you don't.. a database is great.

        Maybe rich731, you're trying to use the database server as a data server? Like, connect accross regular networks to ask 'site a' for thumbnail to 'file x' in 'site c' ?
        If so, there could be much better .. expedient ways to do that kind of thing. Like a gool ol' fashioned cgi.

Re: PDF to JPG for MySQL
by Anonymous Monk on Jun 27, 2008 at 05:06 UTC
    binmode?