相关疑难解决方法(0)

使用PHP的GDlib imagecopyresampled时,是否可以保留PNG图像透明度?

以下PHP代码段使用GD将浏览器上传的PNG大小调整为128x128.它的效果很好,除了原始图像中的透明区域在我的情况下被替换为纯黑色.

虽然imagesavealpha已经确定,但事情并不完全正确.

保留重采样图像透明度的最佳方法是什么?

$uploadTempFile = $myField[ 'tmp_name' ]
list( $uploadWidth, $uploadHeight, $uploadType ) 
  = getimagesize( $uploadTempFile );

$srcImage = imagecreatefrompng( $uploadTempFile );    
imagesavealpha( $targetImage, true );

$targetImage = imagecreatetruecolor( 128, 128 );
imagecopyresampled( $targetImage, $srcImage, 
                    0, 0, 
                    0, 0, 
                    128, 128, 
                    $uploadWidth, $uploadHeight );

imagepng(  $targetImage, 'out.png', 9 );
Run Code Online (Sandbox Code Playgroud)

php png transparency gd alpha

97
推荐指数
6
解决办法
9万
查看次数

如何在PHP中透明地调整png的大小?

我试图在PHP中调整透明背景的png,我在网上找到的代码示例对我不起作用.这是我正在使用的代码,建议将不胜感激!

$this->image = imagecreatefrompng($filename);

imagesavealpha($this->image, true);
$newImage = imagecreatetruecolor($width, $height);

// Make a new transparent image and turn off alpha blending to keep the alpha channel
$background = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
imagecolortransparent($newImage, $background);
imagealphablending($newImage, false);
imagesavealpha($newImage, true);

imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $width, $height,  $this->getWidth(), $this->getHeight());
$this->image = $newImage;  
imagepng($this->image,$filename);
Run Code Online (Sandbox Code Playgroud)


更新 "不工作"我的意思是当我调整pngs时,背景颜色会变为黑色.

php png gd resize

40
推荐指数
3
解决办法
5万
查看次数

标签 统计

gd ×2

php ×2

png ×2

alpha ×1

resize ×1

transparency ×1