2013-05-02 4 views
0

알파 패스로 변환 (http://erskinedesign.com/blog/fireworks-tips-convert-alpha/ 참조)을 사용하여 Fireworks에서 수행하는 작업과 비슷한 것을 달성하려고합니다. php gd 함수만으로 가능합니까?PHP GD를 사용하여 알파로 변환

$img = imagecreatefromstring(file_get_contents('...')); 

imagealphablending($img, true); 
$transparentcolour = imagecolorallocate($img, 255,255,255); 
imagecolortransparent($img, $transparentcolour); 
imagefilter($img, IMG_FILTER_GRAYSCALE); 

$w = imagesx($img); 
$h = imagesy($img); 

for ($x=0; $x<$w; $x++) 
{ 
    for ($y=0; $y<$h; $y++) 
    { 
     $color = imagecolorsforindex($img, imagecolorat($img, $x, $y)); 

     if ($color['alpha'] == 0) 
      continue; 
    } 
} 
imagepng($img); 
exit; 

내 생각은, 그레이 스케일로 변환 '어두운'픽셀이 다음 검은 알파로 변환하는 방법을 측정하는 것입니다,하지만 난 나 자신을 혼동하는 것 : 같은

내 코드 보인다.

+0

이 말은 해당 기능에 대한 전체 코드입니까? 나는 어떤 imagepng()도 여기 보지 않는다. – Viscocent

+0

imagepng()는 for 루프 바로 뒤에 있습니다. – Zahymaka

답변

1

궁금한 점은 당신의 질문을 발견했을 때 나는 똑같은 것을 찾고있었습니다. 이 기능을 구축하는 것으로 끝났습니다. 원하는 방식대로 작동합니다.

function n2_image_make_alpha($im , $percentage) 
{ 
    imagefilter($im , IMG_FILTER_GRAYSCALE); 

    $width = imagesx($im); 
    $height = imagesy($im); 

    imagealphablending($im , false); 
    imagesavealpha($im , true); 

    $newim = imagecreatetruecolor($width , $height); 
    imagealphablending($newim , false); 
    imagesavealpha($newim , true); 


    //Loop through pixels 
    for ($x = 0 ; $x < $width ; $x++) 
    { 
     for ($y = 0 ; $y < $height ; $y++) 
     { 
      //Get the color of the pixel 
      $color = imagecolorat($im , $x , $y); 
      //Get the rgba of the color 
      $rgba = imagecolorsforindex($im , $color); 
      $alpha = $rgba['alpha']; 
      if ($alpha < 127) 
      { 
       $base_alpha = 127 - $alpha; //100% of the difference between completely transparent and the current alpha 
       $percentage_to_increase_alpha = intVal($percentage * $base_alpha/100); 
       $alpha = $alpha + $percentage_to_increase_alpha; 
      } 
      $new_color = imagecolorallocatealpha ($newim , $rgba['red'] , $rgba['green'] , $rgba['blue'] , $alpha); 
      imagesetpixel ($newim , $x , $y , $new_color); 
     } 
    } 
    return $newim; 
} 
관련 문제