2012-11-24 139 views
4

我在正在旋轉的PNG上獲取PNG透明度的一些主要問題。旋轉PNG,然後使用圖像透明度重新保存

$filename = 'bird_up.png'; 
$source = imagecreatefrompng($filename) or die('Error opening file '.$filename); 
imagealphablending($source, false); 
imagesavealpha($source, true); 
$rotation = imagerotate($source, $degrees, imageColorAllocateAlpha($source, 0, 0, 0, 127)); 
imagealphablending($source, false); 
imagesavealpha($source, true); 
header('Content-type: image/png'); 
imagepng($rotation); 
imagedestroy($source); 
imagedestroy($rotation); 

回答

12

我從Wikipedia

That's the original from wikipedia

添加以下

<?php 
// this file writes the image into the http response, 
// so we cant have any output other than headers and the file data 
ob_start(); 

$filename  = 'tibia.png'; 
$degrees  = 45; 

// open the image file 
$im = imagecreatefrompng($filename); 

// create a transparent "color" for the areas which will be new after rotation 
// r=0,b=0,g=0 (black), 127 = 100% transparency - we choose "invisible black" 
$transparency = imagecolorallocatealpha($im,0,0,0,127); 

// rotate, last parameter preserves alpha when true 
$rotated = imagerotate($im, $degrees, $transparency, 1); 

// disable blendmode, we want real transparency 
imagealphablending($rotated, false); 
// set the flag to save full alpha channel information 
imagesavealpha($rotated, true); 

// now we want to start our output 
ob_end_clean(); 
// we send image/png 
header('Content-Type: image/png'); 
imagepng($rotated); 
// clean up the garbage 
imagedestroy($im); 
imagedestroy($rotated); 

演示 原始圖像的工作註釋版本旋轉-45度,新領域〜50%的不透明度演示

$transparency = imagecolorallocatealpha($im,0,0,0,55); 

rotated 45 degrees, new areas with ~50% opacity for a demo

旋轉-45輩分,以100%的不透明度的新領域

$transparency = imagecolorallocatealpha($im,0,0,0,127); 

rotated 45 degress, new areas with 100% opacity

+0

這爲90/180的偉大工程,只是程度不同外45/90/etc等,如37等仍然打破。有任何想法嗎? –

+0

對不起,在imagecolorallocatealpha函數中有一個輸入錯誤。這是現在糾正(圖像資源參數是第一個) –

+0

啊 - 這是非常接近 - 現在唯一的問題是,它似乎認爲盒仍然存在,這是阿爾法,無論如何修剪它在最後? –

0

<?php 
      $text ="New"; $font = "fonts/AARDV.ttf"; $size = 100; 
      function html2rgb() 
      { 
       $color='FF0000'; 
       if(strlen($color)==6) 
       list($r, $g, $b)=array($color[0].$color[1],$color[2].$color[3],$color[4].$color[5]); 
       elseif (strlen($color) == 3) 
       list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]); 
       else 
       return false; 
       $r = hexdec($r); 
       $g = hexdec($g); 
       $b = hexdec($b); 
       return array($r, $g, $b); 
      } 
      $c=html2rgb(); 
      $bbox = imagettfbbox($size, 0, $font, $text); 
      $width = abs($bbox[2] - $bbox[0]); 
      $height = abs($bbox[7] - $bbox[1]); 
      $image = imagecreatetruecolor($width, $height); 
      $bgcolor = imagecolorallocate($image, 1, 1,0); 
      imagecolortransparent($image, $bgcolor); 
      $color = imagecolorallocate($image, $c[0],$c[1],$c[2]); 
      $x = $bbox[0] + ($width/2) - ($bbox[4]/2); 
      $y = $bbox[1] + ($height/2) - ($bbox[5]/2); 
      imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor); 
      imagettftext($image, $size, 0, $x, $y, $color, $font, $text); 
      $last_pixel= imagecolorat($image, 0, 0); 
      for ($j = 0; $j < $height; $j++) 
      { 
       for ($i = 0; $i < $width; $i++) 
       { 
        if (isset($blank_left) && $i >= $blank_left) 
        { 
         break;  
        } 
        if (imagecolorat($image, $i, $j) !== $last_pixel) 
        { 
         if (!isset($blank_top)) 
         { 
          $blank_top=$j; 
         } 
         $blank_left=$i;break; 
        } 
        $last_pixel=imagecolorat($image, $i, $j); 
       } 
      } 

      $x -= $blank_left; 
      $y -= $blank_top; 
      imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor); 
      imagettftext($image, $size, 0, $x, $y, $color, $font, $text); 
      header('Content-type: image/png'); 
      $transparency = imagecolorallocatealpha($image,0,0,0,127); 
      $image=imagerotate($image, 10, $transparency, 1); 
      imagealphablending($image, false); 
      imagesavealpha($image, true); 
      ob_end_clean(); 
      imagepng($image); 
      imagedestroy($image); 
      ?> 

      This one not work..Black color 
      any one please answer this.. 


      enter code here