Z
Z
zoommy2011-06-21 22:37:01
PHP
zoommy, 2011-06-21 22:37:01

translucency in php gd?

People tell me plz.
After certain manipulations with GD, there is a picture descriptor with a transparent background and completely opaque elements. How to make the whole picture semi-transparent, for example 30%, and display it?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zoommy, 2011-06-22
@zoommy

Answer found! Thanks for the tip. Here is the function:

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
      // $pct - уровень прозрачности при наложении 0 .. 100
      if(!isset($pct)){ 
          return false; 
      }
      $pct /= 100;
      $w = imagesx( $src_im ); 
      $h = imagesy( $src_im ); 
      imagealphablending( $src_im, false );
      $minalpha = 127; 
      for( $x = 0; $x < $w; $x++ ) 
      for( $y = 0; $y < $h; $y++ ){ 
          $alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF; 
          if( $alpha < $minalpha ){ 
              $minalpha = $alpha; 
          } 
      } 
      for( $x = 0; $x < $w; $x++ ){ 
          for( $y = 0; $y < $h; $y++ ){ 
              $colorxy = imagecolorat( $src_im, $x, $y ); 
              $alpha = ( $colorxy >> 24 ) & 0xFF; 
              if( $minalpha !== 127 ){ 
                  $alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha ); 
              } else { 
                  $alpha += 127 * $pct; 
              } 
              $alphacolorxy = imagecolorallocatealpha( $src_im,
      	    	    	    	    	    	( $colorxy >> 16 ) & 0xFF,
      	    	    	    	    	    	( $colorxy >> 8 ) & 0xFF,
      	    	    	    	    	    	  $colorxy & 0xFF, $alpha
      	    	    	    	      ); 
              if( !imagesetpixel( $src_im, $x, $y, $alphacolorxy ) ){ 
                  return false; 
              } 
          } 
      } 
      imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); 
  }

S
Sergey Beresnev, 2011-06-22
@sectus

Maybe something like this?

                $color=imagecolorat($src, $x, $y);
                $r = ($color >> 16) & 0xFF;
                $g = ($color >> 8) & 0xFF;
                $b = $color & 0xFF;
                $trans_colour = imagecolorallocatealpha($src, $r, $g, $b, 90);
                imagesetpixel($src, $x, $y, $trans_colour);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question