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

Hi there,

I'm building a site where users can upload their own pictures, I already have a solution working for JPEG files using Image::Resize, but I've had a number of users attempting to upload BMP and PNG files which I have no support for.

I could just force the end user to upload a JPEG or nothing, but I would really prefer to offer BMP and PNG support as well. So, I've been scanning google for a couple of days and I have come up with nothing.

Has anyone solved a problem like this? What modules do I need to convert the PNG and BMP files into JPEGS before they go through the resizer?

Any links to good docs, or advice will be greatly appreciated.

Replies are listed 'Best First'.
Re: Image upload and conversion
by zwon (Abbot) on Oct 18, 2009 at 11:34 UTC

    You can use GD or Image::Magick for converting images to JPEG. Both modules support PNG. I'm not sure if GD supports BMP, but Image::Magick do.

Re: Image upload and conversion
by dsheroh (Monsignor) on Oct 18, 2009 at 13:24 UTC
    GD would be the way to go, given "Image::Resize - Simple image resizer using GD". I'm currently using it for doing PNG->JPG conversion/sizing and the docs mention that GD supports WBMP (which I assume means "Windows .bmp"), although I wasn't able to quickly find a complete list of formats supported by GD.

    Some sample code to help get you started:

    binmode $img_fh; local $/; undef $/; my $img = GD::Image->new($img_fh) or die "Failed to read image file: + $!\n"; # ... my $fh; my $fullpath = "$path/$filename" . ($png_img ? '.png' : '.jpg'); open $fh, '>', $fullpath or die "Failed to write image file: $!\n"; binmode $fh; print $fh ($png_img ? $img->png : $img->jpeg); close $fh;
Re: Image upload and conversion
by stonecolddevin (Parson) on Oct 19, 2009 at 00:53 UTC

    I personally prefer Imager. Much less futzing and a smoother ride.

    mtfnpy

      I'll second Imager for simple things but Graphics Magick is a good choice if you need more complex image manipulation. I've also found that Graphics Magick handles animated GIFs better than Imager despite the horrible interface the GM inherited from Image Magick.

        I despise ImageMagick, I've never heard of GraphicsMagic, but I'll have to check it out. ImageMagick is *just* atrocious.

        mtfnpy