Changeset 2609

Show
Ignore:
Timestamp:
06/19/08 20:05:10 (20 months ago)
Author:
bchoate
Message:

Applied patch for GD graphics library support. Thanks, Ogawa-san. BugId:80127

Location:
branches/release-40/lib/MT
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/release-40/lib/MT/App/Wizard.pm

    r2579 r2609  
    142142            'Image::Magick' => { 
    143143                link => 'http://www.imagemagick.org/script/perl-magick.php', 
     144                label => 'This module is needed if you would like to be able to create thumbnails of uploaded images.', 
     145            }, 
     146            'GD' => { 
     147                link => 'http://search.cpan.org/dist/GD', 
    144148                label => 'This module is needed if you would like to be able to create thumbnails of uploaded images.', 
    145149            }, 
  • branches/release-40/lib/MT/Image.pm

    r2085 r2609  
    366366} 
    367367 
     368package MT::Image::GD; 
     369@MT::Image::GD::ISA = qw( MT::Image ); 
     370 
     371sub load_driver { 
     372    my $image = shift; 
     373    eval { require GD }; 
     374    if (my $err = $@) { 
     375        return $image->error(MT->translate("Can't load GD: [_1]", $err)); 
     376    } 
     377    1; 
     378} 
     379 
     380sub init { 
     381    my $image = shift; 
     382    my %param = @_; 
     383 
     384    if ((!defined $param{Type}) && (my $file = $param{Filename})) { 
     385    (my $ext = $file) =~ s/.*\.//; 
     386    $param{Type} = lc $ext; 
     387    } 
     388    my %Types = (jpg => 'jpeg', jpeg => 'jpeg', gif => 'gif', 'png' => 'png'); 
     389    $image->{type} = $Types{ lc $param{Type} } 
     390        or return $image->error(MT->translate("Unsupported image file type: [_1]", $param{Type})); 
     391 
     392    if (my $file = $param{Filename}) { 
     393    $image->{gd} = GD::Image->new($file) 
     394        or return $image->error(MT->translate("Reading file '[_1]' failed: [_2]", $file, $@)); 
     395    } elsif (my $blob = $param{Data}) { 
     396    $image->{gd} = GD::Image->new($blob) 
     397        or return $image->error(MT->translate("Reading image failed: [_2]", $@)); 
     398    } 
     399    ($image->{width}, $image->{height}) = $image->{gd}->getBounds(); 
     400    $image; 
     401} 
     402 
     403sub blob { 
     404    my $image = shift; 
     405    my $type = $image->{type}; 
     406    $image->{gd}->$type; 
     407} 
     408 
     409sub scale { 
     410    my $image = shift; 
     411    my($w, $h) = $image->get_dimensions(@_); 
     412    my $src = $image->{gd}; 
     413    my $gd = GD::Image->new($w, $h); 
     414    $gd->copyResampled($src, 0, 0, 0, 0, $w, $h, $image->{width}, $image->{height}); 
     415    ($image->{gd}, $image->{width}, $image->{height}) = ($gd, $w, $h); 
     416    wantarray ? ($image->blob, $w, $h) : $image->blob; 
     417} 
     418 
     419sub crop { 
     420    my $image = shift; 
     421    my %param = @_; 
     422    my ($size, $x, $y) = @param{qw( Size X Y )}; 
     423    my $src = $image->{gd}; 
     424    my $gd = GD::Image->new($size, $size); 
     425    $gd->copy($src, 0, 0, $x, $y, $size, $size); 
     426    ($image->{gd}, $image->{width}, $image->{height}) = ($gd, $size, $size); 
     427    wantarray ? ($image->blob, $size, $size) : $image->blob; 
     428} 
     429 
     430sub convert { 
     431    my $image = shift; 
     432    my %param = @_; 
     433    $image->{type} = lc $param{Type}; 
     434    $image->blob; 
     435} 
     436 
    3684371; 
    369438__END__ 
     
    387456 
    388457I<MT::Image> contains image manipulation routines using either the 
    389 I<NetPBM> tools or the I<ImageMagick> and I<Image::Magick> Perl module. 
    390 The backend framework used (NetPBM or ImageMagick) depends on the value of 
     458I<NetPBM> tools, the I<ImageMagick> and I<Image::Magick> Perl module, 
     459or the I<GD> and I<GD> Perl module. 
     460The backend framework used (NetPBM, ImageMagick, GD) depends on the value of 
    391461the I<ImageDriver> setting in the F<mt.cfg> file (or, correspondingly, set 
    392462on an instance of the I<MT::ConfigMgr> class).