2010-11-10 55 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'); 

}