CGI::Application::PhotoGallery::Imager - Imager-based graphics adaptor


CGI-Application-PhotoGallery documentation Contained in the CGI-Application-PhotoGallery distribution.

Index


Code Index:

NAME

Top

CGI::Application::PhotoGallery::Imager - Imager-based graphics adaptor

SYNOPSIS

Top

    use CGI::Application::PhotoGallery::Imager;

    my $lib     = CGI::Application::PhotoGallery::Image->new;
    my $pngdata = $lib->resize( $file, 100 );

METHODS

Top

new( )

creates a new CGI::Application::PhotoGallery::Imager object.

load( $file )

Loads $file and returns an Imager object.

size( $file )

Returns the width and height of $file.

resize( $file, $size )

Resizes $file to $sizex$size with margins.

SEE ALSO

Top

* Imager

AUTHOR

Top

Brian Cassidy <bricas@cpan.org>

COPYRIGHT AND LICENSE

Top


CGI-Application-PhotoGallery documentation Contained in the CGI-Application-PhotoGallery distribution.
package CGI::Application::PhotoGallery::Imager;

use strict;
use warnings;

use Imager;

our $VERSION = '0.15';

sub new {
    my $class = shift;
    my $self  = {};
    bless $self, $class;
    return $self;
}

sub load {
    my $self = shift;
    my $file = shift;

    my $image = Imager->new;
    $image->read( file => $file );
    return $image;
}

sub size {
    my $self = shift;
    my $file = shift;

    my $image = $self->load( $file );
    return $image->getwidth, $image->getheight;
}

sub resize {
    my $self = shift;
    my $file = shift;
    my $size = shift;

    my $image = $self->load( $file );
    my $type = $image->getwidth > $image->getheight ? 'x' : 'y';

    $image = $image->scale( "${type}pixels" => $size );
    
    my( $w, $h ) = ( $image->getwidth, $image->getheight );
    my $x = $size == $w ? 0 : int( ($size - $w) / 2 );
    my $y = $size == $h ? 0 : int( ($size - $h) / 2 );

    my $newimage = Imager->new( xsize => $size, ysize => $size );
    $newimage->paste( img => $image, left => $x, top => $y );

    my $return;
    $newimage->write( type => 'png', data=> \$return );
    return $return;
}

1;