Archive for January, 2009

ImageMagick & PerlMagick

Tuesday, January 6th, 2009

ImageMagick® is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in over 100 formats. ImageMagick can translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

ImageMagick is free software delivered as a ready-to-run binary distribution or as source code that you may freely use, copy, modify, and distribute. Its license is compatible with the GPL. It runs on all major operating systems and offers an iterface to most programming languages (Java, php, Perl, Pascal, Ruby, Python, C++, …).

The Definitive Guide to ImageMagick explains all of these capabilities and more in a practical, learn-by-example fashion, the book ImageMagick Tricks by Sohail Salehi is a fast-paced and practical tutorial packed with examples of photo manipulations, logo creation, animations, and complete web projects.

PerlMagick is an objected-oriented Perl interface to ImageMagick. This module can read, manipulate and write an image or image sequence from within a Perl script. This makes it very suitable for Web CGI scripts. I created the following test script on my hosted webserver to verify the correct configuration of ImageMagick and PerlMagick.

#!/usr/bin/perl -w
use strict;
use CGI::Carp qw(fatalsToBrowser);
use Image::Magick;
# created by Marco Barnig on 6th january 2009

my $headline = “Perl Test Script test9.pl”;

print “Content-type: text/html\n\n”;
print ‘<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>’, “\n”;
print “<html><head><title>Perl Test</title></head><body>\n”;
print “<h1>$headline</h1>\n”;
print “<p>This test script opens an image file test.jpg, makes a clone, modify it and save it as mynew.gif</p>\n”;

my $image1 = new Image::Magick;
my $status = $image1->Read( “../../httpdocs/media/test.jpg” );
die “Couldn’t open file test.jpg !” if “$status”;
my $image2 = $image1->Clone();
#blah, do something to $image2
$image2 -> Draw (
stroke    => “red”,
primitive => “line”,
points    => “20,20 180,180″);
my $status3 = $image2->Write( “gif:../../httpdocs/media/mynew.gif” );
die “Couldn’t save file mynew.gif in folder httpdocs/media !” if “$status3″;
print “<img src=\”http://www.artgallery.lu/media/logo.gif\”></img>\n”;
print “<br/><br/>”;
print “<img src=\”http://www.neen.lu/media/mynew.gif\”></img>\n”;
print “<br/><br/>”;
print “Das Script ist fertig”;
print “</body></html>\n”;

.htpasswd & .htaccess on webservers

Tuesday, January 6th, 2009

Apache and other webservers lets you password protect individual files, folders, or your entire website fairly easily with .htpasswd and .htaccess.

To password protect a folder on your site, you need to put the following code in your .htaccess file:

AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName “My Secret Folder”
Require valid-user

The path /full/path/to/.htpasswd should be the full path (the path to the file from the Web server’s volume root) to a text file called  .htpasswd uploaded in a folder outside of the web root, if possible.

The textfile .htpasswd contains the following informations, separated by a colon (:)

username:encryptedpassword

To encrypt the password, you can use free webtools like

or  the htpasswd utility that comes with Apache.

More details are available in the following tutorial.

cgi & perl

Monday, January 5th, 2009

The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with information servers such as HTTP servers, maintained by the NCSA. The current version is CGI/1.1 and CGI/1.2 is under progress.

Essentially, a CGI is just a program which runs on the server. It can be written in any programming language, but Perl has become a popular choice for CGI programming because it is available for all platforms, and it has many useful tools that are ideal for the web. By convention cgi programs have the file extension .cgi.

Perl is an interpreted language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It’s also a good language for any system management tasks. The language is intended to be practical rather than beautiful. Perl was created by Larry Wall. Perl scripts have the file extension .pl.

There is no real difference between .cgi and  .pl file extensions. Web servers can be configured for a specific extension or you can even leave off the extension, because it’s the first line called shebang in the script that tells the server where and which interpreter to use. For perl programs I prefer to use the extension .pl.

A typical shebang line for perl is: #!/usr/bin/perl

To let the server know it is a cgi program, the files are generally placed in a special directory on the server called /cgi-bin. For security reasons the webserver does not allow chmod permission settings of 777 or 775 for scripts. I set them to 755.

I use the following reference test perl file to check the correct configuration of my hosted webserver.

#!/usr/bin/perl -w

use strict;
use CGI::Carp qw(fatalsToBrowser);

my $headline = “Perl Reference Script”;

print “Content-type: text/html\n\n”;
print ‘<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>’, “\n”;
print “<html><head><title>Perl Test</title></head><body>\n”;
print “<h1>$headline</h1>\n”;
print “<p>This test script opens a data.txt file, appends a name to it and creates a new file new.txt</p>\n”;
print “</body></html>\n”;

open (MYFILE, ‘data.txt’) || die “Could not open file data.txt”;;
while (<MYFILE>) {
chomp;
print “$_\n”;
}
close (MYFILE);

open (MYFILE, ‘>>data.txt’) || die “Could not write to file data.txt”;;
print MYFILE “Bob\n”;
close (MYFILE);

open (MYFILE, ‘>new.txt’) || die “Could not create file new.txt”;
print MYFILE “This file has been created\n”;
close (MYFILE);

The following commands are used to run the program:

  • -w : switch to turning on warnings
  • use strict : pragma for the interpreter to make it harder to write bad software
  • use CGI::Carp qw(fatalsToBrowser) : command to redirect fatal errors such as compiler or other errors to the browser
  • MYFILE : filehandler

A testfile to show my webserver cgi environment variables is available at the following link.  An advanced testfile can be started here. The access is protected with .htaccess and .htpasswd.