PHP Doku:: Set the flag to save full alpha channel information (as opposed to single-color transparency) when saving PNG images - function.imagesavealpha.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzBildverarbeitung und -generierungBildbearbeitung und GDGD- und Image-Funktionenimagesavealpha

Ein Service von Reinhard Neidl - Webprogrammierung.

GD- und Image-Funktionen

<<imagerotate

imagesetbrush>>

imagesavealpha

(PHP 4 >= 4.3.2, PHP 5)

imagesavealphaSet the flag to save full alpha channel information (as opposed to single-color transparency) when saving PNG images

Beschreibung

bool imagesavealpha ( resource $image , bool $saveflag )

imagesavealpha() sets the flag to attempt to save full alpha channel information (as opposed to single-color transparency) when saving PNG images.

You have to unset alphablending (imagealphablending($im, false)), to use it.

Alpha channel is not supported by all browsers, if you have problem with your browser, try to load your script with an alpha channel compliant browser, e.g. latest Mozilla.

Parameter-Liste

image

Eine von den verschiedenen Erzeugungsfunktionen wie imagecreatetruecolor() gelieferte Grafikressource.

saveflag

Whether to save the alpha channel or not. Default to FALSE.

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Beispiele

Beispiel #1 imagesavealpha() example

<?php
// Load a png image with alpha channels
$png imagecreatefrompng('./alphachannel_example.png');

// Do required operations

// Turn off alpha blending and set alpha flag
imagealphablending($pngfalse);
imagesavealpha($pngtrue);

// Output image to browser
header('Content-Type: image/png');

imagepng($png);
imagedestroy($png);
?>

Anmerkungen

Hinweis: Diese Funktion setzt die GD-Bibliothek in der Version 2.0.1 oder höher (empfohlen wird mindestens 2.0.28) voraus.

Siehe auch


3 BenutzerBeiträge:
- Beiträge aktualisieren...
tobias at silverxnet dot de
7.12.2009 3:07
As I needed a function that enables me to put a separate mask into the alpha channel of a picture, I found there is no function to allow this directly via PHP/GD, hence I wrote a quick'n'dirty function that does the trick.

For the mask any format is fine (jpg, png, gif, ...). Black will be transparent, white will be opaque - greyscale makes it half transparent, like with any alpha channel. The alpha channel is only 7 bit, hence there are just 128 variations of transparency possible.

I use the red channel, for the transparency, so you can make it either greyscale or redscale, doesn't matter.

<?php
function imagealphamask( &$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 < $xSize; $x++ ) {
        for(
$y = 0; $y < $ySize; $y++ ) {
           
$alpha = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) );
           
$alpha = 127 - floor( $alpha[ 'red' ] / 2 );
           
$color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) );
           
imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) );
        }
    }
   
   
// Copy back to original picture
   
imagedestroy( $picture );
   
$picture = $newPicture;
}
?>

To use the function is rather simple:
<?php
// Load source and mask
$source = imagecreatefrompng( 'myPicture.png' );
$mask = imagecreatefrompng( 'myMask.png' );

// Apply mask to source
imagealphamask( $source, $mask );

// Output
header( "Content-type: image/png");
imagepng( $source );
?>

Hope it helps!
phil at unabacus dot net
23.04.2008 15:47
The comment left by "doggz at mindless dot com" will cause a duplication in layering of the transparent image - AlphaImageLoader loads the image as if it were a floating layer on top of the <img> element - so your image will double up.. so don't go thinking something very strange is happening with your PHP it's the silly browser ;)

The easiest (although not the best) way to get around this is to use the CSS background property instead of an image src - because as of yet you can't change an image's src dynamically using currently supported CSS:

<div style="width:200px; height:200px; background: url(my-trans-image.php); *background:url(); *filter:progid:
DXImageTransform.Microsoft.AlphaImageLoader(src='my-trans-image.php', sizingMethod='scale');"></div>

The above (although not pretty) keeps the image loaded as a background for any good browser as they should ignore the starred (*) CSS items and should support Alpha PNGs natively. IE will listen to the starred items and blank out the background whilst applying it's AlphaLoader on top. Obviously you need to know the width and height of your image but you can get this using getimagesize() or just by hardcoding.

Downsides to know:

1. Unless the user has 'backgrounds enabled when printing' your image wont show up when the webpage is printed.

2. You can't stretch or shrink a background image - if you change the div's dimensions from that of the image you will stretch it in IE (due to the 'scale' property - which you can change for sake of standardness to 'crop') but you will crop it in any other browser.

3. Most browsers treat images and backgrounds differently, in load priority and in the way the user can interact with them.

Other Options:

Other methods resort to using JavaScript or Browser Detection on the Server Side.
doggz at mindless dot com
6.10.2003 6:50
To use PNG-24 in IE 5.5+ try:
<img alt="Transparant PNG" src="myimage.gif" style="width: 200px; height: 200px; filter:progid:
DXImageTransform.Microsoft.AlphaImageLoader(src='myimage.png', sizingMethod='scale')" />

there is more info available at http://people.brandeis.edu/~peelle/png/



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)