Open In App

PHP | imagesavealpha() Function

Last Updated : 19 Feb, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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
  
// Load the png image
$png = imagecreatefrompng(
  
// Turn off alpha blending
imagealphablending($png, false);
  
// Add alpha as 100 to image
$transparent = imagecolorallocatealpha($png, 255, 255, 255, 100);
imagefill($png, 0, 0, $transparent);
  
// Set alpha flag to true so that
// alpha info is saved in the image
imagesavealpha($png, true);
  
// Save the image
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
  
// Load the png image
$png = imagecreatefrompng(
  
// Turn off alpha blending
imagealphablending($png, false);
  
// Add alpha as 100 to image
$transparent = imagecolorallocatealpha($png, 255, 255, 255, 100);
imagefill($png, 0, 0, $transparent);
  
// Set alpha flag to false so that
// alpha info is not saved in the image
imagesavealpha($png, false);
  
// Save the image
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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads