Image-Mate

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

# This program is free software; you can redistribute it and/or #
# modify it under the same terms as Perl itself.                #
#                                                ################
##################################################

- THERE IS NO WARRANTY FOR THIS MODULE WHATSOEVER -

Image::Mate provides a simple interface for creating and manipulating 
graphics utilizing whatever graphics modules are available to Perl. 
The graphics modules it'll attempt to utilize are Imager, GD and 
Image::Magick in that order. You can set the order of preference.

This module is ideal for people (like myself) who produce scripts that can 
end up on all different kinds of servers across the internet. You can never 
guarantee what modules will be available (or which the hosts will be willing 
to install).

This is the first release and distinctly lacking in features :(
Although I'll be adding new features as time goes on :)
Hopefully others will also add new functions as well :D

lib/Image/Mate.pm  view on Meta::CPAN

    require Exporter;
    @ISA = qw(Exporter);
    @EXPORT_OK = qw( im_available im_setpref );
    $VERSION = 0.05;
} #BEGIN

$GMOD = "";
@GMODS = (
    "Imager",
    "GD",
    "Image::Magick",
);
%GMODLIST = ();
foreach my $module (@GMODS) {
    my $exist = &_CheckForModule($module);
    $GMODLIST{$module} = $exist;
    if ($exist) {
        &_LoadModule($module);
        push (@GMODPREF, $module);
        unless ($GMOD) {
            $GMOD = $module;

lib/Image/Mate.pm  view on Meta::CPAN

        } #else
    } #if

    ## GD
    if ($GMOD eq "GD") {
        if ($input{blank}) {
            my $truecolour = 0;
            $truecolour = 1 if $input{blank}->{c};
            $IMGS->{$imgid} = GD::Image->new($input{blank}->{x},$input{blank}->{y},$truecolour);
            unless (ref($IMGS->{$imgid})) {
                croak 'Image::Magick Error creating image';
            } #unless
            $self->{X} = $input{blank}->{x};
            $self->{Y} = $input{blank}->{y};
        } #if
        else {
            $IMGS->{$imgid} = GD::Image->new($input{file});
            croak $@ if $@;
            ($self->{X},$self->{Y}) = $IMGS->{$imgid}->getBounds()
        } #else
    } #if

    ## Image::Magick
    if ($GMOD eq "Image::Magick") {
        if ($input{blank}) {
            if ($input{blank}->{c}) {
                $IMGS->{$imgid} = Image::Magick->new(size=>"$input{blank}->{x}x$input{blank}->{y}", type=>'TrueColor');
            } #if
            else {
                $IMGS->{$imgid} = Image::Magick->new(size=>"$input{blank}->{x}x$input{blank}->{y}");
            } #else
            unless (ref($IMGS->{$imgid})) {
                croak 'Image::Magick Error creating image';
            } #unless
            $self->{X} = $input{blank}->{x};
            $self->{Y} = $input{blank}->{y};
            my $x = $IMGS->{$imgid}->ReadImage('xc:white');
            croak $x if $x;
        } #if
        else {
            $IMGS->{$imgid} = Image::Magick->new;
            my $error = $IMGS->{$imgid}->Read($input{file});
            croak $error if $error;
            ($self->{X},$self->{Y}) = $IMGS->{$imgid}->Get('width','height') or croak 'Image::Magick Cannot get width and height';
        } #else
    } #if
  
    $self->{IMGID} = $imgid;

    return $self;
} ## End sub


##################################################

lib/Image/Mate.pm  view on Meta::CPAN

        $IMGS->{$self->{IMGID}}->box(filled => 1, color => $input{c}) or croak $IMGS->{$self->{IMGID}}->errstr;
    } #if
    
    ## GD
    if ($GMOD eq "GD") {
        my $colour = &_MakeGDColour($self->{IMGID},$input{c});
        $IMGS->{$self->{IMGID}}->filledRectangle(0,0,$self->{X}-1,$self->{Y}-1,$colour);
        croak $@ if $@;
    } #if
    
    ## Image::Magick
    if ($GMOD eq "Image::Magick") {
        my $error = $IMGS->{$self->{IMGID}}->Colorize(fill => $input{c});
        croak $error if $error;
    } #if
    return $self;  
} #sub


##################################################
######################## Draw line in image
##################################################

lib/Image/Mate.pm  view on Meta::CPAN

    
    ## GD
    if ($GMOD eq "GD") {
        $IMGS->{$self->{IMGID}}->setThickness($input{thick}) if $input{thick};
        my $colour = &_MakeGDColour($self->{IMGID},$input{c});
        $IMGS->{$self->{IMGID}}->line($input{start}->{x},$input{start}->{y},$input{end}->{x},$input{end}->{y},$colour);
        croak $@ if $@;
        $IMGS->{$self->{IMGID}}->setThickness(1) if ($input{thick});
    } #if
    
    ## Image::Magick
    if ($GMOD eq "Image::Magick") {
        $input{thick} = 1 unless $input{thick};
        my $error = $IMGS->{$self->{IMGID}}->Draw(stroke=>$input{c}, primitive=>'line', points=>"$input{start}->{x},$input{start}->{y} $input{end}->{x},$input{end}->{y}", strokewidth=>$input{thick});
        croak $error if $error;
    } #if
    return $self;  
} ## End sub


##################################################
######################## Save image

lib/Image/Mate.pm  view on Meta::CPAN

                    croak $@ if $@;
                } #if
                else {
                    print OUTF $IMGS->{$self->{IMGID}}->jpeg();
                    croak $@ if $@;
                } #else
            } #else
        close(OUTF);
    } #if
    
    ## Image::Magick
    if ($GMOD eq "Image::Magick") {
        if ($input{type} eq "gif") {
            my $error = $IMGS->{$self->{IMGID}}->Write($input{filename});
            croak $error if $error;
        } #if
        elsif ($input{type} eq "png") {
            if ($input{'compression'}) {
                my @compression = qw( None BZip Fax Group4 JPEG JPEG2000 LosslessJPEG LZW RLE Zip );
                my $error = $IMGS->{$self->{IMGID}}->Write(filename => $input{filename}, compression => $compression[ $input{'compression'} ]);
                croak $error if $error;
            } #if

lib/Image/Mate.pm  view on Meta::CPAN

This document refers to Image::Mate.pm version 0.05

=head1 SYNOPSIS

    use Image::Mate;

    # Get available graphics modules
    my %list = &Image::Mate->im_available("hash");

    # Set new preference list
    my $error = &Image::Mate->im_setpref("Imager","GD","Image::Magick");
    
    # create a new image
    $img = Image::Mate->new(blank => {x => 100, y => 100, c => 1});
    $img = Image::Mate->new(file => "image.jpg");

    # colour the whole image red
    $img->fillall(c => "#ff0000");

    # draw a black line in the image
    $img->line(c => "#000000", start => {x => 0, y => 0}, end => {x => 10, y => 10});

lib/Image/Mate.pm  view on Meta::CPAN

=item B<$error = Image::Mate-E<gt>im_available(["array","hash"])>

This method returns a list of what graphics modules are available. List can be in the form of a hash 
listing all modules with either a 1 or 0 value. Or an array listing only those available. NOTE: If 
you run setpref before this routine then the array returned by this method will only contain what 
you set. This routine can be exported the local namespace using use Image::Mate qw( im_available );

=item B<$error = Image::Mate-E<gt>im_setpref(LIST)>

This method allows you to set the preference list of which graphics modules you should use first. 
The default is "Imager","GD","Image::Magick". If successful 0 will be returned, otherwise it'll be 
an error code with descriptive error. You cannot set modules that are not available. If you are 
unsure what graphics modules you have available run Image::Mate->available first.
This routine can be exported the local namespace using use Image::Mate qw( im_setpref );

=item B<$img = Image::Mate-E<gt>new(blank => {x => 100, y => 100, c => 1])>
=item B<$img = Image::Mate-E<gt>new(file => "image.jpg")>

Returns an image object. If there was an error with creating this object it will be in $img->{ERROR}.
"c" can have the value of 0 or 1. 0 for stand colour (usually 8bit) or 1 for high colour (usually 16bit).

lib/Image/Mate.pm  view on Meta::CPAN

Draws a line from start x,y point to end x,y point of colour c.

=item B<$img->save(filename => "FILENAME", type => "TYPE", quality => QUALITY, compression => COMPRESSION)>

Saves the image to a file. Supported types are GIF, PNG, and JPG, default is JPG. 
For JPG you can define QUALITY as 0-100 (100 best quality, 0 worest). For PNG you can define 
COMPRESSION as 0-9 (0 best quality, 9 worest).

=back

=head1 Obtaining the GD, Imager and Image::Magick perl modules

They are all available on CPAN. Just run a search http://search.cpan.org
As long as you have any one of these modules installed Image::Mate will work.

On linux I recommend using the CPAN module. Type "perl -MCPAN -e shell;" from
your shell. (If this is the first time you've ran the CPAN module you'll have to
go through a little config first, but don't worry this is menu driven). Then type either
(or all):-
install Imager
install GD
install Image::Magick

On Windows your are probably using ActivePerl from ActiveState (which I also recommend). Use their ppm
utility, from the command prompt type:-
ppm install http://ppd.develop-help.com/ppd/Imager.ppd
ppm install http://theoryx5.uwinnipeg.ca/ppms/GD.ppd
ppm install http://www.bribes.org/perl/ppm/Image-Magick.ppd

Unfortunately, ActiveStates automatic build machine does not include the necessary modules to build
Imager, GD and Image::Magick, so they are not available from their default repository.

=head1 BUGS AND LIMITATIONS

This is the first release and distinctly lacking in features :(
Although I'll be adding new features as time goes on :)
Hopefully others will also add new functions as well :D

=head1 AUTHOR

The Image::Mate interface is copyright 2007, Lyle Raymond Hopkins.  It is
distributed under the same terms as Perl itself.  See the "Artistic
License" in the Perl source code distribution for licensing terms.

I welcome other programmers to submit new features to this module!!

=head1 SEE ALSO

L<Imager>,
L<GD>,
L<Image::Magick>

=cut

1;

__END__



( run in 1.116 second using v1.01-cache-2.11-cpan-beeb90c9504 )