PHP | imagesavealpha() Function
Last Updated :
19 Feb, 2020
The imagesavealpha() function is an inbuilt function in PHP which is used to set whether to retain full alpha channel information when saving PNG images or not. Alpha channel tells whether the image is fully transparent or opaque. Alpha blending has to be disabled using imagealphablending($im, false)) to retain the alpha-channel in the first place.
Syntax:
bool imagesavealpha( resource $image, bool $save_flag )
Parameters: This function accepts two parameters as mentioned above and described below:
- $image: It specifies the image resource to work on.
- $save_flag: It specifies whether to save the alpha channel or not.
Return Value: This function returns TRUE on success or FALSE on failure.
Below examples illustrate the imagesavealpha() function in PHP:
Example 1: In this example we will enable saving alpha info.
<?php
$png = imagecreatefrompng(
imagealphablending( $png , false);
$transparent = imagecolorallocatealpha( $png , 255, 255, 255, 100);
imagefill( $png , 0, 0, $transparent );
imagesavealpha( $png , true);
imagepng( $png , 'imagewithalphainfo.png' );
imagedestroy( $png );
?>
|
Output:
This will save the image in the same folder as imagewithalphainfo.png with alpha info.
Example 2: In this example we will disable saving alpha info.
<?php
$png = imagecreatefrompng(
imagealphablending( $png , false);
$transparent = imagecolorallocatealpha( $png , 255, 255, 255, 100);
imagefill( $png , 0, 0, $transparent );
imagesavealpha( $png , false);
imagepng( $png , 'imagewithoutalphainfo.png' );
imagedestroy( $png );
?>
|
Output:
This will save the image in the same folder as
imagewithoutalphainfo.png without alpha info.
Reference: https://www.php.net/manual/en/function.imagesavealpha.php
Share your thoughts in the comments
Please Login to comment...