|
From: <ix...@us...> - 2001-12-02 09:49:11
|
ixjonez 01/12/02 01:49:10
Added: lib/LiveFrame/ImageHelper ImageMagick.pm
Log:
add an implementation of ImageHelper that uses the Image::Magick library
Revision Changes Path
1.1 commons/lib/LiveFrame/ImageHelper/ImageMagick.pm
Index: ImageMagick.pm
===================================================================
# -*- Mode: Perl; indent-tabs-mode: nil; -*-
package LiveFrame::ImageHelper::ImageMagick;
use ImageMagick ();
use strict;
sub new {
my ($type) = @_;
my $class = ref($type) || $type;
my $self = bless
{
photo => undef,
magick => Image::Magick->new(),
}, $class;
return $self;
}
sub load {
my ($self, $photo) = @_;
$self->{magick}->Load($photo->path());
$self->{photo} = $photo;
$photo->height($self->{magick}->Get('height'));
$photo->width($self->{magick}->Get('width'));
return 1;
}
sub ping {
my ($self, $photo) = @_;
my ($width, $height) = $self->{magick}->Ping($photo->path());
$photo->height($height);
$photo->width($width);
return 1;
}
sub reset {
my ($self) = @_;
@{ $self->{magick} } = ();
$self->{photo} = undef;
return 1;
}
sub rotate {
my ($self, $degrees) = @_;
unless ($self->{photo}) {
die "no photo loaded\n";
}
$self->{magick}->Rotate(degrees => $degrees);
return 1;
}
sub save {
my ($self, $new_photo) = @_;
unless ($self->{photo}) {
die "no photo loaded\n";
}
if ($new_photo) {
$self->{magick}->Write($new_photo->path());
}
else {
$self->{magick}->Write($self->{photo}->path());
}
return 1;
}
sub scale {
my ($self, $params) = @_;
unless ($self->{photo}) {
die "no photo loaded\n";
}
$params ||= {};
my $new_height = $params->{height};
my $new_width = $params->{width};
my $percent = $params->{percent};
unless ($new_height || $new_width || $percent) {
die "must specify a non-zero height, width or percent\n";
}
my $old_height = $self->{photo}->height();
my $old_width = $self->{photo}->width();
if ($new_height && !$new_width) {
$percent = $old_height / $new_height;
$new_width = int($old_width * $percent);
}
elsif (!$new_height && $new_width) {
$percent = $old_width / $new_width;
$new_height = int($old_height * $percent);
}
elsif (!$new_height && !$new_width) {
$new_height = int($old_height * $percent);
$new_width = int($old_width * $new_width);
}
$self->{magick}->Scale(height => $new_height,
width => $new_width);
$self->{photo}->height($self->{magick}->Get('height'));
$self->{photo}->width($self->{magick}->Get('width'));
return 1;
}
1;
|