2011-02-04 5 views
6

Questo è il codice che sto utilizzando:Come preservare la trasparenza quando si ridimensiona PNG utilizzando Perl e GD

!/usr/bin/perl 
use GD; 
sub resize 
{ 
    my ($inputfile, $width, $height, $outputfile) = @_; 
    my $gdo = GD::Image->new($inputfile); 

    ## Begin resize 

    my $k_h = $height/$gdo->height; 
    my $k_w = $width/$gdo->width; 
    my $k = ($k_h < $k_w ? $k_h : $k_w); 
    $height = int($gdo->height * $k); 
    $width = int($gdo->width * $k); 

    ## The tricky part 

    my $image = GD::Image->new($width, $height, $gdo->trueColor); 
    $image->transparent($gdo->transparent()); 
    $image->copyResampled($gdo, 0, 0, 0, 0, $width, $height, $gdo->width, $gdo->height); 

    ## End resize 

    open(FH, ">".$outputfile);  
    binmode(FH); 
    print FH $image->png(); 
    close(FH); 
} 
resize("test.png", 300, 300, "tested.png"); 

L'immagine di uscita ha uno sfondo nero e tutti i canali alfa vengono persi.

I'am utilizzando questa immagine: http://i54.tinypic.com/33ykhad.png

Questo è il risultato: http://i54.tinypic.com/15nuotf.png

Ho provato tutte le combinazioni di alfa() e trasparenza(), ecc cose, nessuno di loro ha funzionato ....

Pleas help me con questo problema.

+0

possibile duplicato del [Può PNG trasparenza dell'immagine da preservare quando si utilizza GDlib PHP imagecopyresampled?] (Http://stackoverflow.com/questions/32243/can-png-image-transparency-be -preserved-quando-con-phps-GDlib-imagecopyresampled) – daxim

risposta

7

Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?

#!/usr/bin/env perl 
use strictures; 
use autodie qw(:all); 
use GD; 

sub resize { 
    my ($inputfile, $width, $height, $outputfile) = @_; 
    GD::Image->trueColor(1); 
    my $gdo = GD::Image->new($inputfile); 

    { 
     my $k_h = $height/$gdo->height; 
     my $k_w = $width/$gdo->width; 
     my $k = ($k_h < $k_w ? $k_h : $k_w); 
     $height = int($gdo->height * $k); 
     $width = int($gdo->width * $k); 
    } 

    my $image = GD::Image->new($width, $height); 
    $image->alphaBlending(0); 
    $image->saveAlpha(1); 
    $image->copyResampled($gdo, 0, 0, 0, 0, $width, $height, $gdo->width, $gdo->height); 

    open my $FH, '>', $outputfile; 
    binmode $FH; 
    print {$FH} $image->png; 
    close $FH; 
} 
resize('test.png', 300, 300, 'tested.png');