PHP: Hypertext Preprocessor (original) (raw)

imagesavealpha

(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)

imagesavealpha — Whether to retain full alpha channel information when saving images

Description

Note: imagesavealpha() is only meaningful for PNG images, since the full alpha channel is always saved for WebP and AVIF. It is not recommended to rely on this behavior, as it may change in the future. Thus, imagesavealpha() should be called deliberately also for WebP andAVIF images.

Alphablending has to be disabled (imagealphablending($im, false)) to retain the alpha-channel in the first place.

Parameters

image

A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().

enable

Whether to save the alpha channel or not. Defaults to [false](reserved.constants.php#constant.false).

Return Values

Returns [true](reserved.constants.php#constant.true) on success or [false](reserved.constants.php#constant.false) on failure.

Changelog

Version Description
8.0.0 image expects a GdImage instance now; previously, a valid gd resource was expected.

Examples

Example #1 Basic imagesavealpha() Usage

`<?php
// Load a png image with alpha channel
$png = imagecreatefrompng('./alphachannel_example.png');// Turn off alpha blending
imagealphablending($png, false);// Do desired operations

// Set alpha flag

imagesavealpha($png, true);// Output image to browser
header('Content-Type: image/png');imagepng($png);
?>`

Found A Problem?

ray hatfield

14 years ago

`After much trial and error and gnashing of teeth I finally figured out how to composite a png with an 8-bit alpha onto a jpg. This was not obvious to me so I thought I'd share. Hope it helps.

I'm using this to create a framed thumbnail image:

width=imagesx(width = imagesx( width=imagesx(frame ); height=imagesy(height = imagesy( height=imagesy(frame );// create the destination/output image. img=imagecreatetruecolor(img=imagecreatetruecolor( img=imagecreatetruecolor(width, $height );// enable alpha blending on the destination image. imagealphablending($img, true);// Allocate a transparent color and fill the new image with it. // Without this the image will have a black background instead of being transparent. transparent=imagecolorallocatealpha(transparent = imagecolorallocatealpha( transparent=imagecolorallocatealpha(img, 0, 0, 0, 127 ); imagefill( img,0,0,img, 0, 0, img,0,0,transparent );// copy the thumbnail into the output image. imagecopyresampled($img,$thumb,32,30,0,0, 130, 100, imagesx( thumb),imagesy(thumb ), imagesy( thumb),imagesy(thumb ) );// copy the frame into the output image (layered on top of the thumbnail) imagecopyresampled($img,$frame,0,0,0,0, width,width,width,height,$width,$height);imagealphablending($img, false);// save the alpha imagesavealpha($img,true);// emit the image header('Content-type: image/png'); imagepng( $img );// dispose imagedestroy($img);// done. exit; ?>

`

phil at unabacus dot net

17 years ago

`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 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:

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.

`