旋转 PNG,然后使用图像透明度重新保存 [英] Rotate a PNG then resave with Image Transparency

查看:24
本文介绍了旋转 PNG,然后使用图像透明度重新保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在旋转的 PNG 上获取 PNG 透明度时遇到了一些主要问题.

I'm having some major issues getting PNG transparency on a PNG that is being rotated.

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

推荐答案

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

I've added a working commented version below

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

演示原始图片来自 维基百科

旋转 -45 度,新区域的不透明度约为 50% 以供演示

rotated -45 degrees, new areas with ~50% opacity for a demo

$transparency = imagecolorallocatealpha( $im,0,0,0,55 );

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

rotated -45 degress, new areas with 100% opacity

$transparency = imagecolorallocatealpha( $im,0,0,0,127 );

这篇关于旋转 PNG,然后使用图像透明度重新保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆