4

My website (coded in html and php in entirety) includes a feature which allows certain users to upload pictures. The picture is resized and watermarked using this code:

function watermarkpic($filename) {

ini_set('max_input_time', 300);

require 'config.php';  

$watermark = imagecreatefrompng('watermarknew.png');
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);

if(preg_match('/[.](jpg)$/', $filename)) {  
        $originalimage = imagecreatefromjpeg($path_to_image_directory . $filename);  
    } else if (preg_match('/[.](gif)$/', $filename)) {  
        $originalimage = imagecreatefromgif($path_to_image_directory . $filename);  
    } else if (preg_match('/[.](png)$/', $filename)) {  
        $originalimage = imagecreatefrompng($path_to_image_directory . $filename);  
    }  

$originalwidth = imagesx($originalimage);
$originalheight = imagesy($originalimage);

$maxsize = 800;
$imgratio = $originalwidth / $originalheight;

if($imgratio > 1) {
    $finalwidth = $maxsize;
    $finalheight = $maxsize / $imgratio;
}
else {
    $finalheight = $maxsize;
    $finalwidth = $maxsize * $imgratio;
}   

$finalimage = imagecreatetruecolor($finalwidth,$finalheight);

imagecopyresampled($finalimage, $originalimage, 0,0,0,0,$finalwidth,$finalheight,$originalwidth,$originalheight);

imagecopymerge($finalimage, $watermark, 0, 0, 0, 0, $watermarkwidth, $watermarkheight, 100);

//now move the file where it needs to go
if(!file_exists($path_to_medimage_directory)) {  
        if(!mkdir($path_to_medimage_directory)) {  
                die("There was a problem. Please try again!");  
        }  
     } 

 imagejpeg($finalimage, $path_to_medimage_directory . $filename);   
}

The issue is that the watermark has a transparent background, but it shows up as having a black background on the image. I have seen stuff about alpha blending, etc. but I do not really know what this is. I want to understand what I am doing, as well as fix the problem so that the watermark is transparent as it should be. The real picture should fill the space.

Thanks in advance.

j08691
  • 204,283
  • 31
  • 260
  • 272
Scott Fink
  • 227
  • 1
  • 4
  • 11

3 Answers3

2

Scott, there are a lot of things that can be going on here.

  1. You need to make sure you saved your PNG using alpha transparency not indexed. Indexed transparency basically says "This color(maybe black) will be displayed as transparent throughout the image." When read by a browser or image editor, it may be transparent, but especially if you're merging it with a JPG, the transparency won't be honored. If you want to understand more, try http://www.idux.com/2011/02/27/what-are-index-and-alpha-transparency/

  2. Make sure you are getting the correct dimensions for both images. See Transparent PNG over JPG in PHP to make sure you're not running into the same issue.

  3. If you are still running into issues, you may want to check here: http://php.net/manual/en/image.examples.merged-watermark.php as it shows how to change the opacity of an image. It may be close to what you are trying to accomplish or might jog another thought.

Community
  • 1
  • 1
AlexC
  • 1,091
  • 13
  • 25
  • So I figured it out, instead of using imagecopymerge I used image copy and it worked fine. I saw somewhere that there may be a bug with imagecopymerge that causes this in php5 – Scott Fink Feb 26 '12 at 20:01
0

So I figured it out; instead of using imagecopymerge() I used imagecopy() and it worked fine.

I saw somewhere that there may be a bug with imagecopymerge()that causes this in PHP5.

Jonast92
  • 4,964
  • 1
  • 18
  • 32
Scott Fink
  • 227
  • 1
  • 4
  • 11
0

http://php.net/manual/en/function.imagecopymerge.php:

<?php
   $src = imagecreatefromstring(file_get_contents('watermark.png'));
   $dest = imagecreatefromstring(file_get_contents('Unmarked/indian_elephant_chasing_bird.jpg'));

   // Set the brush
   imagesetbrush($dest, $src);
   // Draw a couple of brushes, each overlaying each
   imageline($dest,
       imagesx($src)/2,
       imagesy($src)/2,
       imagesx($src)/2 ,  
       imagesy($src)/2,  
       IMG_COLOR_BRUSHED);

   header('Content-Type: image/png');
   imagepng($dest);
?>