我已经在互联网上寻找答案,但我似乎找不到有用的东西。当我旋转图像(例如,5度)时,该图像在创建的边界图像内旋转以容纳旋转。创建的图像完全是黑色的。
我正在努力使边界框图像完全透明。 我看过的一些其他网站和问题说这应该有效:
<?PHP
$png = imagecreatefrompng('polaroids/polaroid0002.png');
// Do required operations
$png = imagerotate($png, 354.793, 0, 0);
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
?>
然而,这产生了这个:
我可以做些什么让黑色边框变得透明?
谢谢!
答案 0 :(得分:2)
只需为背景颜色设置透明色imagecolorallocatealpha($png, 0, 0, 0, 127)
。
<?php
$png = imagecreatefrompng('polaroids/polaroid0002.png');
// Do required operations
$png = imagerotate($png, 354.793, imagecolorallocatealpha($png, 0, 0, 0, 127), 0);
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
?>
答案 1 :(得分:0)
不确定,我不熟悉使用该特定PHP函数。也就是说,大多数开发人员似乎更喜欢使用ImageMagick或GD直接使用PHP进行图像处理。您使用的功能似乎是GD功能。我会开始看这里。您可能需要更多地修改图像资源以设置透明度或不同的颜色。
http://php.net/manual/en/book.image.php
http://php.net/manual/en/book.imagick.php
我建议看一下:http://php.net/manual/en/function.imagecolortransparent.php
答案 2 :(得分:0)
这是一个WORKS的代码(从我3岁的档案中挖出来的)。我用它来组合2个png图像并渲染一些文字,同时保持透明度。它会创建一个新图像,设置透明度颜色,然后将图像复制到其中。这个确切的文件适用于我的实现(但是我现在已经编写了颜色,因为当时我正在从数据库中提取它们,所以我没有记忆他们是什么以及他们的意思)。对不起,如果它很乱。生成的图像为$image
,由$ruler_img
作为第一层,$index_img
作为第二层,文本作为第三层。您的案例中只有一个图层。
//prepare first layer
$ruler_img=$ruler_img=imagecreatefrompng($_SERVER['DOCUMENT_ROOT'].$ruler['out_ruler_img']);
//create main image, basic layer, background
$image = imageCreateTrueColor($ruler_width, $ruler_height);
imageSaveAlpha($image, true);
$transparentColor = imagecolorallocatealpha($image, 0,0,0, 127); //some color used as transparency key, this uses black
$color = imagecolorallocatealpha($image, 0,0,0,127);
//fill with transparent color
imagefill($image, 0, 0, $transparentColor);
imagealphablending($ruler_img,true);
//copy the first layer
imagecopy($image,$ruler_img,0,0,0,0,$ruler_width,$ruler_height);
//prepare the second layer
$index_img=imagecreatefrompng($_SERVER['DOCUMENT_ROOT'].$ruler['out_index_img']);
$size=getimagesize($_SERVER['DOCUMENT_ROOT'].$ruler['out_index_img']);
$index_width=$size[0];
$index_height=$size[1];
imagealphablending($index_img,true);
$ratio=1.0;
if($index_height>$ruler_height)
$ratio=$ruler_height*1.0/$index_height;
$new_width=$index_width*$ratio;
$new_height=$index_height*$ratio;
//now I copy the resampled second layer
imagecopyresampled(
$image,
$index_img,
$position*($ruler_width-$new_width),
($ruler_height-$new_height)/2,
0,
0,
$new_width,
$new_height,
$index_width,
$index_height);
//render text
$font = "fonts/comic.ttf";
$fontSize=10;
$box=imagettfbbox($fontSize, 0, $font,$text);
$textWidth=$box[2]-$box[6];
$textHeight=$box[3]-$box[7];
imagettftext($image, $fontSize, 0, $ruler_width-$textWidth-10, $ruler_height+12, $color, $font, $text);
header("Content-type: image/png");
//that's it!
imagepng($image);