如何在使用Perl和GD调整PNG大小时保持透明度
这是我正在使用的代码:如何在使用Perl和GD调整PNG大小时保持透明度,perl,png,gd,alpha,Perl,Png,Gd,Alpha,这是我正在使用的代码: !/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;
!/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");
/usr/bin/perl
使用GD;
子调整大小
{
我的($inputfile,$width,$height,$outputfile)=@;
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);
##棘手的部分
我的$image=GD::image->new($width、$height、$gdo->trueColor);
$image->transparent($gdo->transparent());
$image->copyResampled($gdo,0,0,0,$width,$height,$gdo->width,$gdo->height);
##端部调整大小
打开(FH,“>”$outputfile);
双模式(FH);
打印FH$图像->png();
关闭(FH);
}
调整大小(“test.png”,300300,“tested.png”);
输出图像背景为黑色,所有alpha通道丢失
我正在使用此图像:
结果是:
我尝试了alpha()和Transparance()等的所有组合,但没有一个有效
请帮我解决这个问题。
#/usr/bin/env perl
使用狭窄;
使用autodie qw(:全部);
使用GD;
子调整大小{
我的($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,$width,$height,$gdo->width,$gdo->height);
打开我的$FH,'>',$outputfile;
binmode$FH;
打印{$FH}$image->png;
收盘价$FH;
}
调整大小('test.png',300300,'tested.png');
#!/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');