May
22
A Non-Scientific Contrast of PHP & Perl
Filed Under Computers & Tech, Software Development on May 22, 2007 at 9:03 pm
It’s no secret that I don’t like PHP. In fact, I dedicated an entire article to explaining just why I don’t like PHP last year. Recently I’ve found another reason to dislike PHP. The GD image manipulation libraries SUCK! They are so insanely and needlessly complex. All I wanted to do was write a simple command-line script to read in a folder and edit each JPEG image in the folder to resize it, insert a Creative Commons logo, and the URL to my website. I’m currently working on uploading some of my photography to my website and doing this manually for all my photos is just not an option, it would take forever! So, I started writing a PHP script because I know PHP can do these things thanks to the DB libs. That turned out to be a bad idea so I ran home to Perl π
[tags]Perl, PHP, GD Libs, Image Magick[/tags]
I got about 30 lines into the PHP code, realised it would be a total mess and gave up. The complexity of doing even the most simple things in the GD libs is astounding. Surely, you’d expect to be able to re-size an image with a one-liner. Not with GD! Inserting one image into another, also not a one liner. Adding in text, DEFINITELY not a one liner. At that stage I’d had enough. I went for Perl and the Image Magick binaries that I had installed anyway. In about half the time I’d already dedicated to my very incomplete PHP solution I threw together a working Perl solution. The real work is done in this very small Perl module:
package WebifyImages; # static variables $WebifyImages::CC_ICON = '~/scripts/webifyImages/cc80x15.png'; $WebifyImages::URL_FONT = '~/scripts/webifyImages/hanshand.ttf'; # function to addin the watermark image # arguments: the file names to modify sub insertCCIcon{ print "\nInserting CC Icon:\n"; print "------------------\n\n"; foreach my $image (@_){ print "processing file $image\n"; `composite -gravity southwest -geometry +5+5 ${WebifyImages::CC_ICON} $image $image`; } } # function to add in the URL # arguments: the file names to modify sub insertUrl{ print "\nInserting URL:\n"; print "--------------\n\n"; foreach my $image(@_){ print "processing file $image\n"; `convert $image -fill white -pointsize 17 -font ${WebifyImages::URL_FONT} -gravity southeast -annotate +7-3 'www.bartbusschots.ie' $image`; } } # function to re-size images # arguments: the file names to modify sub resizeImages{ print "\nResizing Images:\n"; print "----------------\n\n"; foreach my $image(@_){ print "processing file $image\n"; `mogrify -size 1024x1024 -thumbnail 800x800 $image`; } } # function to add a credit into an image # Arguments: # 1) the image name # 2) the name to credit sub insertCredit{ my $image = shift; my $credit = shift; print "Crediting $credit on the image $image\n"; `convert $image -fill white -pointsize 17 -font ${WebifyImages::URL_FONT} -gravity southeast -annotate +7+18 'Credit: $credit' $image`; } my $perl_is_dumb = 1;
To actually invoke this functionality on a directory I write the following very small script:
#!/usr/bin/perl use lib '/Users/bart/scripts/webifyImages'; use strict; use WebifyImages; # Script to webify a folder of images # print a warning print <<endl; WARNING WARNING WARNING This script will modify all .jpg files in this folder! Are you sure you want to continue? (y/n) endl unless(<> =~ m/y/i){ exit; } # get the images to modify my @images = <./*.jpg>; WebifyImages::resizeImages(@images); WebifyImages::insertCCIcon(@images); WebifyImages::insertUrl(@images);
Because some of my older images were already the right size and already had my name on them I also wrote another simple little script which uses the same module to only insert the CC icon:
#!/usr/bin/perl use lib '/Users/bart/scripts/webifyImages'; use strict; use WebifyImages; print <<endl; WARNING WARNING WARNING This script will modify all .jpg files in this folder! Are you sure you want to continue? (y/n) endl unless(<> =~ m/y/i){ exit; } # get the images to modify my @images = <./*.jpg>; WebifyImages::insertCCIcon(@images);
Finally, a few of the images in my photo gallery were taken by other family members using my camera so it seems only fair to credit them, hence this other simple little script:
#!/usr/bin/perl use lib '/Users/bart/scripts/webifyImages'; use strict; use WebifyImages; # Script to insert a credit into an image # get arg my $image = $ARGV[0]; my $credit = $ARGV[1]; unless ($image){ print "ERROR: must supply the path to the image to credit and the name to credit as arguments.\n\n"; exit; } # print a warning print <<endl; WARNING WARNING WARNING This script will the image $image to insert a credit to $credit Are you sure you want to continue? (y/n) endl unless(<STDIN> =~ m/y/i){ exit; } #do it WebifyImages::insertCredit($image, $credit);
The things to note from all this is how simple the code is, how easy it is to understand, and how much Perl can do in one line. That contrasts very strongly with PHP and it’s GD libs!
Close the old link
Thanks Des!
you left an anchor tag open, thats what the last comment was supposed to say before the html got ripped.
Good work though.
fixed your comment …. and the post π
Cheers for the heads up … that’ll learn me to add in a link after previewing a post.
Bart.
Its interesting how attitudes shift over time. I’m happy enough to use php off the shelf stuff now, because thats where the focus has been for mainstream web applications, theres not really a choice of using perl over php. You just have to ensure its kept up to date, and configure php/apache as well as you can.
But I don’t want anything to do with writing or maintaining php code. I remember a good while back, that you went away from perl towards php, and have now come back to perl.
I wonder if many more php developers are migrating to perl.
I think with perl and ttk, theres no need to consider php for web apps that you are going to maintain over time.
Is ttk the best there is for templating in perl?
More on topic for your post, would you not have considered using Image Magick in php? π
Thanks for the comments Phil. I’ve definitely come full circle. Initially I was a big fan of PHP but the more I learn and the more experience I get the less I like PHP and the more I love Perl. It’s such a great language and when you combine it with the power of CPAN, the efficiency of Perl DBI and the TTK you have a great platform. I look at PHP as being Perl-Lite.
But, like you said, for off-the-shelf stuff it’s your own real option. The only down side is you spend far too much time patching and patching and patching. If it isn’t PHP itself that’s vulnerable then it’s WordPress or PHPBB or what ever.
As for your question on TTK, no I haven’t found anything better but TBH I’m not looking because so far TTK has never let me down. I’ve never found myself in a position where it wouldn’t work so I haven’t really dedicated time to investigating alternatives.
At work I now exclusively program web stuff in Perl (usually OO Perl) + TTK. For my PhD work it’s still Java + Apache Struts all the way and were I to have to build any large web app from scratch that would still be my first choice, but for anything smaller Perl + TTK would be my pick. PHP doesn’t really figure anymore in my mind.
I’d echo phil’s question. Imagemagick and Perl is teh sex.
I probably should have answered Phil’s question. Once I decided to shell out to ImageMagick I just couldn’t see a reason to use PHP from the command-line over Perl. Had the GD libs been the dogs bollox as I had hopped I’d have used PHP of course, but since I was going the shelling out route anyway and since Perl rocks I just didn’t see a reason to do it in PHP …. yes …. I think JK has had an effect on me π
Ah right, on closer inspection I see you do you imagemagick. Still, I think you can call most of that functionality from within perl rather than shelling out. I dislike shelling out stuff for no real valid reason.
[…] and easier to use Perl to shell out to the command line tools from Image Magick. At the time I wrote a post on my choice to do this which also contained the initial code. That code has been expanded and evolved since, and now includes functions for rendering nice (in […]