Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I like your script, good idea to remove extra colour information when the pixel is totally transparent. I should point out just a small error (IMO) though if anyone wants to use this method.</p> <pre><code>$color = imagecolorsforindex( $source, imagecolorat( $source, $x, $y ) ); </code></pre> <p>should be</p> <pre><code>$color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) ); </code></pre> <p>also I'm not 100% sure why you're checking rgb values here if the pixel is 100% transparent</p> <pre><code>if(($alpha['red'] == 0) &amp;&amp; ($alpha['green'] == 0) &amp;&amp; ($alpha['blue'] == 0) &amp;&amp; ($alpha['alpha'] == 0)) ... </code></pre> <p>and I'm not sure alpha blending from the mask file would work well with your method, as its only used when rgba values all equal 0.</p> <p>Jules's script is pretty good too, though it expects the mask to be a grey scale representation of a mask (which is pretty common practice). </p> <p>In Matt's query he was after a script that grabs just the alpha transparency from an existing image and applies it to another image. Here's a simple mod of Jules's script just to grab the alpha from the mask image, and preserve the alpha of the source image.</p> <pre><code>&lt;?php // Load source and mask $source = imagecreatefrompng( '1.png' ); $mask = imagecreatefrompng( '2.png' ); // Apply mask to source imagealphamask( $source, $mask ); // Output header( "Content-type: image/png"); imagepng( $source ); function imagealphamask( &amp;$picture, $mask ) { // Get sizes and set up new picture $xSize = imagesx( $picture ); $ySize = imagesy( $picture ); $newPicture = imagecreatetruecolor( $xSize, $ySize ); imagesavealpha( $newPicture, true ); imagefill( $newPicture, 0, 0, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) ); // Resize mask if necessary if( $xSize != imagesx( $mask ) || $ySize != imagesy( $mask ) ) { $tempPic = imagecreatetruecolor( $xSize, $ySize ); imagecopyresampled( $tempPic, $mask, 0, 0, 0, 0, $xSize, $ySize, imagesx( $mask ), imagesy( $mask ) ); imagedestroy( $mask ); $mask = $tempPic; } // Perform pixel-based alpha map application for( $x = 0; $x &lt; $xSize; $x++ ) { for( $y = 0; $y &lt; $ySize; $y++ ) { $alpha = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) ); //small mod to extract alpha, if using a black(transparent) and white //mask file instead change the following line back to Jules's original: //$alpha = 127 - floor($alpha['red'] / 2); //or a white(transparent) and black mask file: //$alpha = floor($alpha['red'] / 2); $alpha = $alpha['alpha']; $color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) ); //preserve alpha by comparing the two values if ($color['alpha'] &gt; $alpha) $alpha = $color['alpha']; //kill data for fully transparent pixels if ($alpha == 127) { $color['red'] = 0; $color['blue'] = 0; $color['green'] = 0; } imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) ); } } // Copy back to original picture imagedestroy( $picture ); $picture = $newPicture; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload