Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed bug where saving to gif would lose transparency
  • Loading branch information
stefangabos committed Feb 11, 2019
1 parent 376459a commit 132c192
Showing 1 changed file with 11 additions and 46 deletions.
57 changes: 11 additions & 46 deletions Zebra_Image.php
Expand Up @@ -1565,70 +1565,35 @@ private function _prepare_image($width, $height, $background_color = '#FFFFFF')
// create a blank image
$identifier = imagecreatetruecolor((int)$width <= 0 ? 1 : (int)$width, (int)$height <= 0 ? 1 : (int)$height);

// if we are creating a transparent PNG image
if ($this->target_type == 'png' && $background_color == -1) {
// if we are creating a transparent image, and image type supports transparency
if ($background_color == -1 && $this->target_type != 'jpg') {

// disable blending
imagealphablending($identifier, false);

// save full alpha channel information
imagesavealpha($identifier, true);

// allocate a transparent color
$transparent_color = imagecolorallocatealpha($identifier, 0, 0, 0, 127);

// fill the image with the transparent color
imagefill($identifier, 0, 0, $transparent_color);

// if we are creating a transparent GIF image
} elseif ($this->target_type == 'gif' && $background_color == -1) {

// if source image was *not* also a transparent gif
if (!isset($this->source_transparent_color_index) || $this->source_transparent_color_index < 0) {

// this will be the "transparent" color from now on, for this image
$color = $this->_hex2rgb('#FFFFFF');

// set this property
$this->source_transparent_color = array(
'red' => $color['r'],
'green' => $color['g'],
'blue' => $color['b'],
);
$background_color = imagecolorallocatealpha($identifier, 0, 0, 0, 127);

}

// allocate the source image's transparent color also to the new image resource
$transparent_color = imagecolorallocate(
$identifier,
$this->source_transparent_color['red'],
$this->source_transparent_color['green'],
$this->source_transparent_color['blue']
);
// we also need to set this for saving gifs
imagecolortransparent($identifier, $background_color);

// fill the background of the new image with transparent color
imagefill($identifier, 0, 0, $transparent_color);

// from now on, every pixel having the same RGB as the transparent color will be transparent
imagecolortransparent($identifier, $transparent_color);
// save full alpha channel information
imagesavealpha($identifier, true);

// for other image types
// if we are not creating a transparent image
} else {

// if transparent background color specified, revert to white
if ($background_color == -1) $background_color = '#FFFFFF';

// convert hex color to rgb
$background_color = $this->_hex2rgb($background_color);

// prepare the background color
$background_color = imagecolorallocate($identifier, $background_color['r'], $background_color['g'], $background_color['b']);

// fill the image with the background color
imagefill($identifier, 0, 0, $background_color);

}

// fill the image with the background color
imagefill($identifier, 0, 0, $background_color);

// return the image's identifier
return $identifier;

Expand Down

0 comments on commit 132c192

Please sign in to comment.