2012-05-21 30 views
1

我搜索了一个这样的答案,但我似乎无法找到可行的东西。当我旋转图像时,例如5度,图像在创建的边界图像内旋转以容纳旋转。该创建的图像是完全黑色的。用PHP旋转图像时创建的边框中的黑色背景

我试图使边界框图像完全透明。 一些我看了看其他网站和疑问的说,这应该工作:

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

然而,这产生:

yields

我能为黑色边框做是透明的?

谢谢!

回答

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

这很简单,完美无瑕。谢谢。 –

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

+0

该脚本使用GD。我还没有看到任何使用ImageMagick的教程。 –

+0

是的,对不起,我跳了枪,第二次修改了答案。 – pthurmond

+0

尝试添加: '$ black = imagecolorallocate($ png,0,0,0); imagecolortransparent($ png,$ black);'在标题部分之前,没有任何变化。 –

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); 
+0

非常有趣的实现。尝试这一点。 –

+0

我已决定采用上述更简单的解决方案。不过谢谢。 –