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


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

Index


Code Index:

NAME

Top

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

SYNOPSIS

Top

    use CGI::Application::PhotoGallery::GD;

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

METHODS

Top

new( )

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

resize( $file, $size )

Resizes $file to $sizex$size with transparent margins.

load( $file )

Loads $file and returns a GD::Image.

size( $file )

Returns the width and height of $file.

SEE ALSO

Top

* GD

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::GD;

use strict;
use warnings;

use GD;

our $VERSION = '0.15';

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

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

    my $image = $self->load( $file );

    my ( $width, $height ) = $image->getBounds();

    my $image2 = new GD::Image( $size, $size );

    $image2->transparent( $image2->colorAllocate( 0, 0, 0 ) );

    my $hnw = int( ( $height * $size / $width ) + 0.5 );
    my $wnh = int( ( $width * $size / $height ) + 0.5 );

    my @arg = ( $image, 0, 0, 0, 0, $size, $size, $width, $height );

    if ( $width > $height ) {
        $arg[ 2 ] = int( ( $size - $hnw ) / 2 + 0.5 );
        @arg[ 5, 6 ] = ( $size, $hnw );
    }
    elsif ( $width < $height ) {
        $arg[ 1 ] = int( ( $size - $wnh ) / 2 + 0.5 );
        @arg[ 5, 6 ] = ( $wnh, $size );
    }

    $image2->copyResized( @arg );
    return $image2->png;
}

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

    my $image;
    if ( $GD::VERSION < 1.30 ) {
        my ( $path, $type ) = $file =~ /(.*)\.([^.]+)/;
        my %new = (
            gif => 'newFromGif',
            png => 'newFromPng',
            jpg => 'newFromJpeg'
        );
        my $new = $new{ lc( $type ) };
        $image = GD::Image->$new( $file );
    }
    else {
        $image = GD::Image->new( $file );
    }

    return $image;
}

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

    my $image = $self->load( $file );

    return $image->getBounds();
}

1;