Syntax checking PHP on the commandline

I’m sure most people only thing of PHP as a Weblanguage due to be called through a browser. It has however since version 4.3.0 also been possible to use PHP on the commandline - as you do with Perl, Shell scripts and likewise. If you’re using Linux (or an other Unix-like operating system - including Mac OSX) you probably have a few small programs available which can make it a breeze to check if the syntax in all you PHP scripts is correct.

Converting between image formats with Perl

Changing files from one format to another is quite easy with a little bit of ImageMagick . In the example below a JPG image (test.jpg) is converted into a GIF-image (test.gif). To output in a different (ImageMagick supported ) format, just change the “image->Set” line.

#!/usr/bin/perl -w use strict; use Image::Magick;
my $image = Image::Magick->new();

# To explicitly set image format use this instead: 
# my $image = Image::Magick->new(magick=> 'JPEG');
my $x = $image->Read('test.jpg'); 
$x = $image->Set(magick => 'GIF'); $x = $image->Write('test.gif');
exit();

Image sizes in Perl

If you need for figure out the size of an image, fetch Image::Size from CPAN , it’s just what you need. The module recognizes the most common image-formats such as JPG, GIF, PNG, PSD, TIFF and plenty more. The interface is simple and will get you what you need with no trouble at all.

 #!/usr/bin/perl -w use strict; use Image::Size;

# Just fetch the size 
my ($size_x, $size_y) = Image::Size::imgsize('test.jpg'); 
print "Image is: $size_y x $size_x (height x width)\n";

# HTML-friendly format 
my $size = Image::Size::html_imgsize('test.jpg'); 

exit();

Image sizes and PHP

If you have the GD extension available, you can do a few useful things. One of the simple things is getting an image size is a simple matter. You can either fetch the height and width in seperate variables – or even fetch the height and width in a string pre-formatted for use in an image tag.

Here’s the example in source:

$load\_ext = get\_loaded\_extensions();

if (!in\_array(gd, $load\_ext)) {
    echo "GD is NOT available";
    die();
}

$nikonFile   = './aarhus\_demo\_photo.jpg';
list($width, $height, $type, $img\_txt)  = getimagesize($nikonFile);

echo "The image is ".$width . " by ".$height."\\n";
echo "![]($nikonFile)\\n";

Reading Exif data with PHP

Within most photos from digital cameras besides the actual image, there’s a little ”information block” call EXIF data. If you have the correct PHP extension installed on your server – the one called ”exif” – it’s pretty easy to read the EXIF data and display them, as you like.

First, let’s check if the extension is available?

$load\_ext = get\_loaded\_extensions();
if (!in\_array(exif, $load\_ext)) {
echo "Exif is NOT available";
} else {
echo "Exif extension is available.";
};