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

I've been using Moveable Type's blog software (the old free version, 2.661) for a while, but it always seems that integrating images with my blog is a hassle. I tried switching to WordPress but the image upload and resizing process still seems more difficult than it needs to be. My wife won't blog because the process to upload images is too cucumbersome (or so she says). (Our digital camera is 4 mega-pixels, so images directly uploaded at the default resolution don't display very well (or perform well for dialup blog readers.)

Can anyone offer me an example of a server-side Perl CGI script which resizes images using Image::Magick (or some other such resizing/thumbnailing tool) allowing a user to upload a folder of images from their local operating system through a browser? The documentation for Image::Magick seems woefully inadequate.

I envision a two-step process in which my wife uploads 20 or 30 images (which are resized/thumbnailed in the background) and then she begins to write her blog. By the time she is ready to incorporate an image, the thumbnails are available in the thumbnail directory.

Once I have the images uploaded and resized to something web-viewable (say, 480x360) I can use WordPress' image gallery feature to browse and include images in a particular blog entry, which will (I expect) make blogging with pictures much easier.

Alternatively, if anyone knows of some decent blog software that I can host on my own site which seamlessly integrates image uploading/resizing, I would appreciate a pointer to that software.


No good deed goes unpunished. -- (attributed to) Oscar Wilde
  • Comment on Image uploading and server-side resizing

Replies are listed 'Best First'.
Re: Image uploading and server-side resizing
by TedYoung (Deacon) on Apr 18, 2006 at 17:10 UTC

    Here is some code to resize images using Image::Magick:

    require Image::Magick; my $img = new Image::Magick; $img->Read($filepath); $img->Scale("640>"); $j->Write($filepath2);

    For the Scale, you can specify something like "640x480" which makes the image exactly those dimensions, or "640>" which states, make it no more than 640 wide, but don't change the aspect ratio and don't enlage. If you want to base the resizing off the hieght, you can do "x480>".

    This can be done with CGI, but you may have trouble uploading a bunch of files at once, without resorting to zipping or tarring, or AJAX.

    Maybe you can offer her a comand-line or dropplet option.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re: Image uploading and server-side resizing
by zentara (Cardinal) on Apr 18, 2006 at 20:07 UTC
    I think you should be making your thumbnails, locally, then uploading them. You probably don't even need to upload the big full images, unless you want people to get the full resolution photo's. Here is a sub to do a directory of Images and make an html table of the thumbnails.

    You can also make lower resolution versions of the main image, for easier downloads, but I'll leave that to you. All you need to do is make a second "thumbnail" of larger size (whatever you want then to download) A 4 meg full-resolution photo may be 1800x1200, but you can downsize it to a 800x600 jpeg, and it will only be about 100k.

    It can all be done in 1 step, with the upload included, via lwp and a cgi script on your server.

    #!/usr/bin/perl use warnings; use strict; use Image::Magick; #watch the width of your picture names #they can widen the table cells if too long umask 0022; my $image = Image::Magick->new; my $count = 0; open(OUT,">thumbs.html"); print OUT<<End_Header; <html> <body> <table align=left bgcolor=9999CC border=2 cellpadding=2 cellspacing=2> <tr> End_Header my @pics= <*.jpg>; foreach my $pic (@pics){ $count++; my ($picbasename) = $pic =~ /^(.*).jpg$/; my $ok; $ok = $image->Read($pic) and warn ($ok); my ($w,$h)= $image->Get('columns','height'); my $thumb = $picbasename . '-t.jpg'; $ok = $image->Scale(geometry => '100x100')and warn ($ok); $ok = $image->Write($thumb)and warn ($ok); my ($tw,$th)= $image->Get('columns','height'); my $picoptions= $w.'x'.$h.'x'.$tw.'x'.$th; print "$pic\t$picoptions\n"; undef @$image; print OUT qq(<td align=center><a href= $pic ><img alt= [$pic-thu +mbnail] src=$thumb height=$th width=$tw ></a><br>$pic</td>); if($count == 4){print OUT "</tr><tr>"; $count = 0}; } print OUT<<EOHTML; </tr> </table> </body> </html> EOHTML close OUT;

    I'm not really a human, but I play one on earth. flash japh
Re: Image uploading and server-side resizing
by chargrill (Parson) on Apr 18, 2006 at 19:32 UTC

    I don't want to totally repost the code from this node, but there's probably a large hunk you can lift from here. If you want to generate thumbnails, the more (to lift) the better. Of course, you'll have to strip away all the cruft of stripping attachments off of emails.



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}
Re: Image uploading and server-side resizing
by leocharre (Priest) on Jun 20, 2006 at 19:31 UTC

    You guys are gonna hate me for this, but .. I've done a lot of messing around with image galleries, and thumbnailing, and perl and image magick and gd... - and Just a few weeks ago I stumbled again on command line mogrify, convert, etc. And I'm affraid for this situation, it's the best answer.

    ptum, chances are you are hosted on a linux server. You got shell? There's a 50/50 chance you got access to something that will blow your mind. Mogrify- it changes images in place, via simple command line arguments, you can resize, crop, whatever.. all of the images you want to. It's fast, it's effective. You have to be careful not to overrite your old images. I just made a gallery out of my 712 photos in the time it takes to upload the images.

    Imagine you have:
    public_html/images/md (here are your images that you uploaded)

    change to the parent directory (images). then run command
    mkdir sm
    and copy all the images there
    cp ./md/*jpg ./sm/

    Then run
    mogrify -resize 100x100 ./sm/*jpg
    done. All the images in public_html/images/sm are thumbnails.

    For the html, I hacked this together.. i run it locally and upload the output.