2010-11-10 36 views
5

所以我有PNG图像,我旋转它,但我得到一个黑色背景..或者,如果我做的颜色代码OFR白我白拿。我试着这样做..如何在使用php转动png图像后获得透明背景?

$trans = imagecolorallocatealpha(image, 0, 0, 0, 127); 
imagerotate($image, $degree, $trans) 

我也试过..

$trans = imagecolorallocatealpha($image, 255, 255, 255, 127); 

有人可以帮我吗?

这里是我的代码..如果我改变allocatealpha为0,0,255,0然后它变蓝。但0,0,0,127仍然是黑色。

function rotate($degrees) {
$image = $this->image;
imagealphablending($image, false);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($this->image, 0, 0, $color);
$rotate = imagerotate($image, $degrees, $color);
imagesavealpha($image, TRUE);
$this->image = $rotate;

+0

是否原代码使用'image'而不是'$ image'这样? – MiffTheFox 2010-11-10 20:34:35

+0

是啊,他们原来的代码使用$图片 – Chris 2010-11-11 03:56:44

回答

0

你试过吗?

imagecolortransparent

希望我理解您的问题!

+0

谢谢!它终于奏效了! – Chris 2010-11-10 20:40:19

+0

@好听!记得检查一个答案是正确的! (Doest必须是我的,但是最能帮助你的那个) – Trufa 2010-11-10 23:58:50

+0

是的,我发现它并没有真正的工作。这只是一个白色的背景,所以它似乎这样,所以我仍然在寻找答案。感谢您向我展示该功能! – Chris 2010-11-11 00:29:58

0
 // Turn off transparency blending (temporarily) 
     imagealphablending($image, false); 

     // Create a new transparent color for image 
     $color = imagecolorallocatealpha($image, 0, 0, 0, 127); 

     // Completely fill the background of the new image with allocated color. 
     imagefill($image, 0, 0, $color); 

     // Restore transparency blending 
     imagesavealpha($image, true); 
+0

我之前试过这个,它并没有工作,因为某种原因 – Chris 2010-11-10 20:41:01

+0

好吧,所以我说谎imagecolortransparent不工作它只是看起来像那样因为白色背景..我再次尝试这种方式,我知道它的工作原理,因为我可以改变背景蓝色和红色等等,但当我搞砸了alpha通道,它确实使它对黑色透明..例如,如果我有这样的分配.. 0,0,255,75它只是使一个更深的蓝色阴影如果它使蓝色透明,但它后面有黑色 – Chris 2010-11-10 21:09:57

9
$destimg = imagecreatefromjpeg("image.png"); 
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127); 
$rotatedImage = imagerotate($destimg, 200, $transColor); 
imagesavealpha($rotatedImage, true); 
imagepng($rotatedImage,"rotated.png"); 
+0

好的答案...完美的工作:-) – abhis 2011-09-29 05:40:12

+2

“imagecreatefromjpeg(”image.png“)”应该是“imagecreatefrompng(”image.png“)” – 2014-10-21 23:52:43

0
$info = pathinfo($pathToImage); 
    $name = str_replace("." . $info['extension'], "", $info['basename']); 

    $size = getimagesize($pathToImage); 



    $type = isset($size['type']) ? $size['type'] : $size[2]; 

    // Check support of file type 
    if (!(imagetypes() & $type)) { 
     // Server does not support file type 
     return false; 
    } 

    $source = self::imageCreateFrom($pathToImage, trim($info['extension'])) or notfound(); 
    $transColor = imagecolorallocatealpha($source, 255, 255, 255, 127); 

    // $transparency = imagecolorallocatealpha($source, 0, 0, 0, 127); 
    $rotate = imagerotate($source, 360 - $rotate_angle, $transColor); 
    //imagealphablending($rotate, false); 


    imagesavealpha($rotate, TRUE); 
    //imagejpeg($rotate,$pathToThumbs.DIRECTORY_SEPARATOR.$info['basename']); 
    self::createImage($rotate, $pathToThumbs, $info['basename'], trim($info['extension'])); 

    // imagejpeg($rotate); 
    imagedestroy($source); 
    imagedestroy($rotate); 
0

这是我在用,这个工作非常适合具有透明背景的.png文件。击掌!

function rotate($degrees) { 

    // Switch from default counter-clockwise to clockwise 
    $degrees = 360 - $degrees; 

    // Get the image 
    $source = imagecreatefrompng("images/image.png"); 

    // Rotate the image 
    $rotated_image = imagerotate($source, $degrees, imageColorAllocateAlpha($source, 0, 0, 0, 127)); 

    // Save the rotated image 
    imagepng($rotated_image, 'images/rotated_image.png'); 

}