Tag Archives: graphics

Fetching Image details in Perl

Image::Size is fine, if size is the only thing, which matters. Sometimes, however, it isn’t enough, and when that is the case Image::Info (again fetched from CPAN) is your friend. Point it to a file (through various methods), and it will return a hash with all the information available about the image you pointed at. Most popular formats are supported.

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/perl -w
use strict;
use Image::Info;
 
# Just fetch the size
my $imgInfo = Image::Info::image_info("test.jpg");
 
# Print out all info fetched from the image
for (keys %$imgInfo) { print " $_ -> $imgInfo->{$_}n"; }
 
exit();

Converting between image formats in Perl

Changing files from one format to another is quite easy with a little bit of Magick. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/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();

Rotating an Image with Perl

Turning images is quite simple. In the example below an image is turned 90 degrees clockwise, wirtten to a file, turned another 90 degress and written to a file again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/perl -w
use strict;
use Image::Magick;
 
my $image = Image::Magick->new(magick=>'JPEG');
my $x = $image->Read('test.jpg');
 
$x = $image->Rotate(degrees=>90); # 90 degress clockwise
$x = $image->Write('test.90.jpg');
 
$x = $image->Rotate(degrees=>90); # Another 90 degress clockwise
$x = $image->Write('test.180.jpg');
 
exit();

Making thumbnails with Perl

With the help of ImageMagick you can automagically use Perl to create thumbnails. The example below is quite rude and makes a 50 by 50 thumbnail (no matter which size and shape the master had). Before using it in a real world scenario, check the aspect ratio, the size of the original image and what ever may be applicable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl -w
use strict;
use Image::Magick;
 
my $image = Image::Magick->new(magick=>'JPEG');
my $x = $image->Read('test.jpg');
 
$x = $image->Scale(width=>'50', height=> '50');
 
# The following should also work fine...
# $x = $image->Scale(geometry=> '50x50');
 
$x = $image->Write('test.50x50.jpg');
 
exit();

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.

[php] no_tags
#!/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();[/php]

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.

[php] no_tags
#!/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’);
print “Here is my image \n”;

exit();
[/php]

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:

1
2
3
4
5
6
7
8
9
10
11
12
$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 "<img $img_txt src='$nikonFile'/>\n";

And here’s how it looks (source).

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?

1
2
3
4
5
6
$load_ext = get_loaded_extensions();
if (!in_array(exif, $load_ext)) {
echo "Exif is NOT available";
} else {
echo "Exif extension is available.";
};

If you have the extension available, reading the data is a simple matter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$nikonFile   = './aarhus_demo_photo.jpg';
$exifNikon   = getExifData($nikonFile);
 
$olympusFile = './zanzibar_demo_photo.jpg';
$exifOlympus = getExifData($olympusFile);
 
echo "&lt;h2&gt;Olympus EXIF data&lt;/h2&gt;";
echo "&lt;pre&gt;";
print_r($exifOlympus);
echo "&lt;/pre&gt;";
 
echo "&lt;h2&gt;Nikon EXIF data&lt;/h2&gt;";
echo "&lt;pre&gt;";
print_r($exifNikon);
echo "&lt;/pre&gt;";
 
function getExifData($file) {
$exif = exif_read_data($file, 0, true);
return $exif;
}

As you may notice, there’s a photo from an Olympus camera and a Nikon camera as an example. The data available from each photo isn’t quite the same. Most generic fields are available, but some fields are camera specific (or maker specific).

See the example above in action (source). The two images are from Aarhus (Nikon) and Zanzibar (Olympus). You can read more on EXIF in Wikipedia.