php旋转图像的透明度

时间:2014-05-27 13:07:06

标签: php rotation gd transparent

我发现有几篇关于在php中旋转图像的文章。但是,它们都将图像的透明部分变为黑色。

旋转时替换的部分是透明的(imagerotate函数$transparency变量工作正常,但原始图像本身不会保持透明。在此图像中,白色区域(替换为imagerotate)应该是透明的,但黑色区域也应该是透明的:

enter image description here

感谢您的帮助。我的代码如下。

<?php

//$mapim=imagecreatefrompng('http://maps.googleapis.com/maps/api/staticmap?center=40.039299261014,-76.112831236771&zoom=20&size=600x600&sensor=false&maptype=satellite');
$panel=base64_decode('iVBORw0KGgoAAAANSUhEUgAAAlgAAAJYCAIAAAAxBA+LAAAABnRSTlMAAAAAAABupgeRAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAHvklEQVR42u3cwWoUdwDH8d24s9lEIuTQmycRvKSlIFrpoRTEd/Dm1ffwKXwIH8Ce0gcI9BAKQttjD3oIrM2STA3bw8iwTqcpFob9T3+fz0FmBC//y49/5hsnEwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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l/FAUMISMxhCxjN8gBAwhozFELOM3CAFDyGgM8T/L+A1C4F+JZSiFWAZwIySaWAYwhEQTywCGkGhiGcAQEk0sA2yFWIZSiGUAN0KiiWUAQ0g0sQxgCIkmlgEMIdHEMsBWiGUohVgGcCMkmlgGMIREE8sAhpBoYhnAEBJNLANshViGUohlADdCoollAENINLEMYAiJJpYBDCHRxDLAVohlKIVYBnAjJJpYBjCERBPLAIaQaGIZwBASTSwDbIVYhlKIZQA3QqKJZQBDSDSxDGAIiSaWAQwh0cQywFaIZSiFWAZwIySaWAYwhEQTywCGkGhiGcAQEk0sA2yFWIZSiGUAN0KinZ4c/4d/dXT/e7EMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAo/AXkWYjnk2OVcYAAAAASUVORK5CYII=');

//ob_start();
$degrees=60;
$srcpanelim=imagecreatefromstring($panel);
$panelim=imagecreatetruecolor(600,600);
$transparency = imagecolorallocatealpha($panelim,0,0,0,127);
imagealphablending( $panelim, false );
imagesavealpha( $panelim, true );
imagecopyresampled($panelim,$srcpanelim,0,0,0,0,600,600,600,600);
ob_start();
    imagepng($panelim, NULL, 9);
    $rawImageBytes = ob_get_clean();
    echo "<img src='data:image/png;base64,".base64_encode($rawImageBytes ) . "' />";
$transparency = imagecolorallocatealpha($panelim,0,0,0,127);
$panelim = imagerotate( $panelim, $degrees, $transparency, 1);

ob_start();
//header( 'Content-Type: image/png' );
imagepng( $panelim );
$rawImageBytes = ob_get_clean();
    echo "<img src='data:image/png;base64,".base64_encode($rawImageBytes ) . "' />";  
imagedestroy( $panelim );

/*$panelim
ob_start();
    imagepng($panelim, NULL, 9);
    $rawImageBytes = ob_get_clean();
    echo "<img src='data:image/png;base64,".base64_encode($rawImageBytes ) . "' />";
    */


?>

1 个答案:

答案 0 :(得分:0)

使用Imagecopy合并而不是imagecopyresampled。 Imagecopyresample与imagecopy以及其他重新采样相同。这里的问题是iamgecopy不适用于透明度。

你可以在php.net上找到关于这个的说明: http://www.php.net/manual/en/function.imagecolortransparent.php