0

I'm trying to make a image uploader and everything is working pretty good with the exception of placing a watermark on top of a transparent gif. The gif it self stays transparent but, the part of the watermark that gets placed on the gif's transparent background gets a red background for some reason and I'm struggling to find out why. Any suggestions?

enter image description here

else if ($this->fileType === "image/gif") {

    $newFilename = $this->placeholder();
    $img = imageCreateFromGif($this->fileTmpName);
    imageAlphaBlending($img, true);
    imageSaveAlpha($img, true);

    if($this->watermarkEnabled === true) {

        $this->watermark($img);

    }

    $newFilename = imageGif($img, $newFilename.".".$this->fileExtension); 


}

function watermark($img) {

    // creating png image of watermark
    $watermark = imagecreatefrompng($this->watermarkImage); 

    // getting dimensions of watermark image
    $watermarkWidth = imagesx($watermark);  
    $watermarkHeight = imagesy($watermark);

    // placing the watermark 10px from bottom and right
    $destX = $this->imageSize[0] - $watermarkWidth - 10;  
    $destY = $this->imageSize[1] - $watermarkHeight - 10;

    // creating a cut resource
    $cut = imagecreatetruecolor($watermarkWidth, $watermarkHeight);
    imagefill($cut,0,0,0x7fff0000);

    // copying that section of the background to the cut
    imagecopy($cut, $img, 0, 0, $destX, $destY, $watermarkWidth, $watermarkHeight);

    // placing the watermark now
    imagecopy($cut, $watermark, 0, 0, 0, 0, $watermarkWidth, $watermarkHeight);

    // merging both of the images
    imagecopymerge($img, $cut, $destX, $destY, 0, 0, $watermarkWidth, $watermarkHeight, 100);

    imagedestroy($watermark);

}
Rajohan
  • 1,411
  • 2
  • 10
  • 27
  • imagefill($cut,0,0,0x7fff0000); it has red fill, change 0x7fff0000 to 0x7f000000 – ikdekker Mar 12 '18 at 10:35
  • That makes the background black. 0x7fff0000 was somehting i found on another stackoverflow thread. But, unfortunately either gives me a transparent background. – Rajohan Mar 12 '18 at 10:41
  • Cant you just omit the imagefill? – ikdekker Mar 12 '18 at 10:42
  • I don't need it for png or jpg. But, it was something i tested for gif. Without it the background of the watermark is black if the background of the image where the watermark is placed is transparent. – Rajohan Mar 12 '18 at 10:45
  • 1
    Transparency in GIF is implemented by just declaring one single color from the palette as transparent. You need to match this with your image that you want to overlay on it - the pixels from your second image, that you want to be transparent in the result, must use that same palette color, as your first image. (And I think not only the same color value, but the same palette index. You would need to “convert” the palette of your second image to match that of the first one, I guess.) – CBroe Mar 12 '18 at 10:53

1 Answers1

0

I solved this by converting the image from Gif to Png and then add the watermark.

Rajohan
  • 1,411
  • 2
  • 10
  • 27