旋转PNG,然后使用图像透明度重新保存

Joh*_*ake 4 php png alpha rotation transparent

我在一个正在轮换的PNG上获得PNG透明度方面遇到了一些重大问题.

$filename = 'bird_up.png';
$source = imagecreatefrompng($filename) or die('Error opening file '.$filename);
imagealphablending($source, false);
imagesavealpha($source, true);
$rotation = imagerotate($source, $degrees, imageColorAllocateAlpha($source, 0, 0, 0, 127));
imagealphablending($source, false);
imagesavealpha($source, true);
header('Content-type: image/png');
imagepng($rotation);
imagedestroy($source);
imagedestroy($rotation);
Run Code Online (Sandbox Code Playgroud)

Mic*_*eim 13

我在下面添加了一个有效的评论版本

<?php
// this file writes the image into the http response,
// so we cant have any output other than headers and the file data
ob_start();

$filename       = 'tibia.png';
$degrees        = 45;

// open the image file
$im = imagecreatefrompng( $filename );

// create a transparent "color" for the areas which will be new after rotation
// r=0,b=0,g=0 ( black ), 127 = 100% transparency - we choose "invisible black"
$transparency = imagecolorallocatealpha( $im,0,0,0,127 );

// rotate, last parameter preserves alpha when true
$rotated = imagerotate( $im, $degrees, $transparency, 1);

// disable blendmode, we want real transparency
imagealphablending( $rotated, false );
// set the flag to save full alpha channel information
imagesavealpha( $rotated, true );

// now we want to start our output
ob_end_clean();
// we send image/png
header( 'Content-Type: image/png' );
imagepng( $rotated );
// clean up the garbage
imagedestroy( $im );
imagedestroy( $rotated );
Run Code Online (Sandbox Code Playgroud)

演示来自维基百科的原始图像

这是维基百科的原创

旋转-45度,具有~50%不透明度的新区域用于演示

$transparency = imagecolorallocatealpha( $im,0,0,0,55 );
Run Code Online (Sandbox Code Playgroud)

旋转45度,具有~50%不透明度的新区域用于演示

旋转-45度,具有100%不透明度的新区域

$transparency = imagecolorallocatealpha( $im,0,0,0,127 );
Run Code Online (Sandbox Code Playgroud)

旋转45度,新区域100%不透明度