1. Perl
  2. Module
  3. here

Imager - Easy to edit images

The "Imager" module makes it easy to "edit images". Here are some tips.

Draw "Japanese"

To draw Japanese with Imager:

use strict;
use warnings;
use utf8;

use Imager;

my $img = Imager->new;

# Load image
$img->read(file =>'example.jpg')
  or die $img->errstr;

# Font
my $font = Imager::Font->new(
  file =>'/usr/share/fonts/ja/TrueType/kochi-gothic-subst.ttf'
);

# Drawing a string
$img->string(
  x => 50,
  y => 50,
  string =>'I did it',
  font => $font,
  size => 15,
  aa => 1,
  color =>'# 000000',
) or die $img->errstr;

# Save image
$img->write(file =>'new_example.jpg')
  or die $img->errstr;

Creating a font object with Imager::Font, specify the path where the font actually exists. The above example is for CentOS.

I'm using the string method to draw a string on top of the image. aa is an option to apply antialiasing.

Save the script in UTF-8 and enable the utf8 pragma.

Read binary data

Use the data option of the read method to read binary data in Perl's Imager module.

$img->read(data => $data, type =>'jpeg')
  or die $img->errstr;

Please note that you also need to specify the image type.

Preparation for using Imager on CentOS

Imager is a Perl module for processing images. Install the following libraries on CentOS in advance to handle image formats and fonts such as jpeg, png, and gif.

yum -y install libjpeg-devel
yum -y install libpng-devel
yum -y install giflib-devel
yum -y install freetype-devel

Then install Imager.

cpan Imager

Related Informatrion