gpt4 book ai didi

perl - 使用 Perl 和 GD 调整 PNG 大小时如何保持透明度

转载 作者:行者123 更新时间:2023-12-04 14:51:39 26 4
gpt4 key购买 nike

这是我正在使用的代码:

!/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");

输出图像具有黑色背景,所有 Alpha channel 都将丢失。

我正在使用此图像: http://i54.tinypic.com/33ykhad.png

这是结果: http://i54.tinypic.com/15nuotf.png

我尝试了 alpha() 和 transparancy() 等的所有组合,但都没有奏效.....

请帮我解决这个问题。

最佳答案

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');

关于perl - 使用 Perl 和 GD 调整 PNG 大小时如何保持透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4900766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com