Formatting uploaded images
Hi All, I have a perl script which enables users to upload a jpg file to my webserver from their browser. However, I'd like to reformat the uploaded jpeg to a certain pixel width and height so that it fits in with my webpage template (and also to avoid massive files being uploaded). Is there any way to do this in perl? Thanks in advance, Mo
Re: Formatting uploaded images
Good morning, On 15/2/06 at 12:57 PM -, Moisés Chicharro [EMAIL PROTECTED] wrote: I have a perl script which enables users to upload a jpg file to my webserver from their browser. However, I'd like to reformat the uploaded jpeg to a certain pixel width and height so that it fits in with my webpage template (and also to avoid massive files being uploaded). Is there any way to do this in perl? Quite a few ways; one of the popular ones is ImageMagick. Charlie -- Charlie Garrison [EMAIL PROTECTED] PO Box 141, Windsor, NSW 2756, Australia
Re: Formatting uploaded images
Hi, Thankyou for your response - much appreciated. Problem is the ISP is not too receptive about adding new modules to their perl install ImageMagick doesn't seem to be on there. Before I start the arduous process of convincing them to install a module, is there anyway of doing it without a module? My main aims being: - check uploaded image filesize is not too large (most important) - check uploaded image physical pixel size - change uploaded image physical pixel size (if possible without module) Cheers, Mo On 15 Feb 2006, at 14:09, Charlie Garrison wrote: Good morning, On 15/2/06 at 12:57 PM -, Moisés Chicharro [EMAIL PROTECTED] wrote: I have a perl script which enables users to upload a jpg file to my webserver from their browser. However, I'd like to reformat the uploaded jpeg to a certain pixel width and height so that it fits in with my webpage template (and also to avoid massive files being uploaded). Is there any way to do this in perl? Quite a few ways; one of the popular ones is ImageMagick. Charlie -- Charlie Garrison [EMAIL PROTECTED] PO Box 141, Windsor, NSW 2756, Australia
Re: Formatting uploaded images
To check image upload size: my $uploaded_file = $query-upload( 'uploaded_file' ); # $query = CGI.pm object my $buffer_size = 2049000; my $image; my $size = read( $uploaded_file, $image, $buffer_size ) or die SYSTEM: Error reading uploaded file . $query-param( 'uploaded_file' ); if ( $size == $buffer_size ) { error(Specified image too large ( $buffer_size bytes max ). ); } On 2/15/06, Moisés Chicharro [EMAIL PROTECTED] wrote: Hi, Thankyou for your response - much appreciated. Problem is the ISP is not too receptive about adding new modules to their perl install ImageMagick doesn't seem to be on there. Before I start the arduous process of convincing them to install a module, is there anyway of doing it without a module? My main aims being: - check uploaded image filesize is not too large (most important) - check uploaded image physical pixel size - change uploaded image physical pixel size (if possible without module) Cheers, Mo On 15 Feb 2006, at 14:09, Charlie Garrison wrote: Good morning, On 15/2/06 at 12:57 PM -, Moisés Chicharro [EMAIL PROTECTED] wrote: I have a perl script which enables users to upload a jpg file to my webserver from their browser. However, I'd like to reformat the uploaded jpeg to a certain pixel width and height so that it fits in with my webpage template (and also to avoid massive files being uploaded). Is there any way to do this in perl? Quite a few ways; one of the popular ones is ImageMagick. Charlie -- Charlie Garrison [EMAIL PROTECTED] PO Box 141, Windsor, NSW 2756, Australia
Re: Formatting uploaded images
On Wed, February 15, 2006 9:52 am, Moisés Chicharro said: Hi, Thankyou for your response - much appreciated. Problem is the ISP is not too receptive about adding new modules to their perl install ImageMagick doesn't seem to be on there. Before I start the arduous process of convincing them to install a module, is there anyway of doing it without a module? My main aims being: - check uploaded image filesize is not too large (most important) - check uploaded image physical pixel size - change uploaded image physical pixel size (if possible without module) Not easily. (You'd have to read the file yourself, then do the rescale, then write it back out in correct format. All doable, but there's no helper functions for any of it.) There are about a dozen different modules that'll do this for you. Check to see if the ISP has any of them installed. If not, beg them to install ImageMagick: it's a very common tool for websites. Daniel T. Staal --- This email copyright the author. Unless otherwise noted, you are expressly allowed to retransmit, quote, or otherwise use the contents for non-commercial purposes. This copyright will expire 5 years after the author's death, or in 30 years, whichever is longer, unless such a period is in excess of local copyright law. ---
Re: Formatting uploaded images
On Feb 15, 2006, at 8:52 AM, Moisés Chicharro wrote: My main aims being: - check uploaded image filesize is not too large (most important) Looks like you already got an answer for this... - check uploaded image physical pixel size You need a Graphics Engine like ImageMagic or the GD libraries to work with images using perl and you need a web host that will help get them installed or at least allow you to install it. Check out Imager too: http://search.cpan.org/~tonyc/Imager-0.47/Imager.pm - change uploaded image physical pixel size (if possible without module) If the file is not too big you can just specify the image size in your HTML. The browser will resize the image for you. Kindest Regards, -- Bill Stephenson 417-546-8390
Re: Formatting uploaded images
Moisés == Moisés Chicharro [EMAIL PROTECTED] writes: Moisés Problem is the ISP is not too receptive about adding new modules to Moisés their perl install ImageMagick doesn't seem to be on there. Before I Moisés start the arduous process of convincing them to install a module, is Moisés there anyway of doing it without a module? No, you do that on your computer, *before* you upload. So the ISP doesn't care about it, because it's your machine. :) -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Re: Formatting uploaded images
On Feb 15, 2006, at 10:33 AM, Bill Stephenson wrote: You need a Graphics Engine like ImageMagic or the GD libraries to work with images using perl and you need a web host that will help get them installed or at least allow you to install it. That last part is worth repeating - you only need root access to install a module if you're installing it to a directory that requires root access to write to. You don't need root to install modules under your home directory, or any other location that's writable by you. For that matter, you could build and install your own private copy of perl itself under your home directory too. sherm-- Cocoa programming in Perl: http://camelbones.sourceforge.net Hire me! My resume: http://www.dot-app.org
Re: Formatting uploaded images
(Randal == (Randal L Schwartz) merlyn@stonehenge.com writes: (Randal No, you do that on your computer, *before* you upload. So the ISP (Randal doesn't care about it, because it's your machine. :) Argh. Always read *all* the message before posting. Ignore me. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Re: Formatting uploaded images
At 14:52 + 15/2/06, Moisés Chicharro wrote: My main aims being: - check uploaded image filesize is not too large (most important) Others have explained how to do this. - check uploaded image physical pixel size This can be done relatively easily by parsing the JFIF tags. The JPEG/JFIF file format is just: FF D8 - Start of Image marker Followed by a sequence of headers: FF xx - header tag xx length (two bytes, big endian) - length of header including these two bytes header data The actual data for the image follows the Start of Scan marker (FF DA) header, and the header you want is the Start of frame marker (FF C0) header, where the data for the header is: P -- one byte: sample precision in bits (usually 8, for baseline JPEG) Y -- two bytes X -- two bytes other stuff see http://www.obrador.com/essentialjpeg/headerinfo.htm for details. Parsing this format sufficiently to read the X Y size should not be very challenging. - change uploaded image physical pixel size (if possible without module) As mentioned, you could just specify height and width in your HTML/CSS to set a maximum display size. Actually reprocessing the image to change the size without a library would be quite challenging. Enjoy, Peter. -- http://www.stairways.com/ http://download.stairways.com/
Re: Formatting uploaded images
- check uploaded image physical pixel size This can be done relatively easily by parsing the JFIF tags Nobody's yet mentioned the Image::Size module? http://search.cpan.org/~rjray/Image-Size-2.992/Size.pm That's what I'd use before I went poking about in the bytes of a JPEG. And, the advice to resize images in the code is causing web developers everywhere to weep and wail and gnash their teeth. Please don't do that! The problem of reading file size has been solved previously. The problem of reading pixel size should be fixed with the above module. The whole problem -- how to stop users uploading pictures which are too large in file size and/or too large in pixel size -- should be fixed by refusing to post inappropriate files. unless ( $pixel_size = $pixel_size_limit $file_size = $file_size_limit ) { refuse_picture(); }