2011-02-04 5 views
6

를 사용하여 PNG 크기를 조정하면 투명성을 유지하는 방법 :이 내가 사용하고 코드입니다 펄 및 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"); 

출력 이미지는 검정색 배경을 가지고 있으며, 모든 알파 채널이 손실됩니다. 이 이미지를 사용하여

I'am가 : http://i54.tinypic.com/33ykhad.png

이 결과 :

http://i54.tinypic.com/15nuotf.png 나는 알파의 모든 조합을 시도()와, 투명성() 등의 일들, 그들 중 누구도 일했다 ....

이 문제와 관련하여 도움을 받으십시오.

+0

의 중복 가능성 [가 PNG 이미지의 투명도 PHP의 GDlib이 imagecopyresampled 사용하는 경우 보존 할 수 있습니까?] (http://stackoverflow.com/questions/32243/can-png-image-transparency-be -preserved-when-using-phps-gdlib-imagecopyresampled) – daxim

답변

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'); 
관련 문제