2013-09-21 3 views
0

투명도를 추가 할 PNG가 있습니다.PNG를 투명하게 만들기 - PHP

어떻게하면됩니까? 나는 모른다.

  1. 사용자는 JPEG 파일을 업로드 :

    내 초기 상황은 다음과 같다.

  2. 나는

  3. $image = imagecreatefromjpeg('546654465456456_background_main.jpg'); 
    imagealphablending($image, true); 
    
    $bottom = imagecreatetruecolor(1280,720); 
    imagecopyresampled($bottom,$image,0,0,0,0,1280,720,1280,720); 
    imagealphablending($bottom, true); 
    imagesavealpha($bottom,true); 
    
    imagepng($bottom, 'trans.png', 1);
    가 지금은 이미지에 50 %의 투명도를 추가하려면 다음 코드를 사용하여 PNG로 영상을 만들 수 있습니다.
    < --- 여기가

안부 마이클 GOHL

+0

처럼 사용하는 문제 http://stackoverflow.com/a/76499 42/487334 –

답변

0

<?php 
function filter_opacity(&$img, $opacity) //params: image resource id, opacity in percentage (eg. 80) 
     { 
      if(!isset($opacity)) 
       { return false; } 
      $opacity /= 100; 

      //get image width and height 
      $w = imagesx($img); 
      $h = imagesy($img); 

      //turn alpha blending off 
      imagealphablending($img, false); 

      //find the most opaque pixel in the image (the one with the smallest alpha value) 
      $minalpha = 127; 
      for($x = 0; $x < $w; $x++) 
       for($y = 0; $y < $h; $y++) 
        { 
         $alpha = (imagecolorat($img, $x, $y) >> 24) & 0xFF; 
         if($alpha < $minalpha) 
          { $minalpha = $alpha; } 
        } 

      //loop through image pixels and modify alpha for each 
      for($x = 0; $x < $w; $x++) 
       { 
        for($y = 0; $y < $h; $y++) 
         { 
          //get current alpha value (represents the TANSPARENCY!) 
          $colorxy = imagecolorat($img, $x, $y); 
          $alpha = ($colorxy >> 24) & 0xFF; 
          //calculate new alpha 
          if($minalpha !== 127) 
           { $alpha = 127 + 127 * $opacity * ($alpha - 127)/(127 - $minalpha); } 
          else 
           { $alpha += 127 * $opacity; } 
          //get the color index with new alpha 
          $alphacolorxy = imagecolorallocatealpha($img, ($colorxy >> 16) & 0xFF, ($colorxy >> 8) & 0xFF, $colorxy & 0xFF, $alpha); 
          //set pixel with the new color + opacity 
          if(!imagesetpixel($img, $x, $y, $alphacolorxy)) 
           { return false; } 
         } 
       } 
      return true; 
     } 
?> 

을 시도하고이 당신을 도울 수있는이

$image = imagecreatefrompng("img.png"); 
filter_opacity($image, 75);