title: PHP Watermarking - Zubrag
body: <p>I'm using zubrags PHP watermarking script (attached below) and it works great except when I try and use a PNG-24 as my watermark.  The resulting image has a garbled, non-transparent watermark.  I'm wondering if anyone could help explain what I would need to alter in the script below so that a PNG-24 embeds as a proper watermark on the image?  I did come across this article: <a href="http://www.benholmen.com/blog/2008/12/adding-png-watermarks-to-jpegs-with-php/" rel="nofollow noreferrer">http://www.benholmen.com/blog/2008/12/adding-png-watermarks-to-jpegs-with-php/</a> which mentioned the use of PHP's imagealphablending() function for PNG-24 support. </p>
  
  <p>Thanks in advance for any help.  It's very appreciated :)</p>
  
  <p>Zubrag's Watermark:</p>
  
  <pre><code>class Zubrag_watermark {
  
    var $offset_x = 0;
    var $offset_y = 0;
    var $quality = 100;
    var $image_type = -1; // Image type: 1 = GIF, 2 = JPG, 3 = PNG
    var $force_image_type = -1; // Change image type? (-1 = same as original, 1 = GIF, 2 = JPG, 3 = PNG)
    var $save_to_file = true;
  
    function Zubrag_watermark($image_path='', $offset_x=0, $offset_y=0) {
      $this-&gt;setImagePath($image_path);
      $this-&gt;setOffset($offset_x, $offset_y);
    }
  
    function setImagePath($image_path) {
      $this-&gt;image_path = $image_path;
    }
  
    function setOffset($x, $y) {
      $this-&gt;offset_x = $x;
      $this-&gt;offset_y = $y;
    }
  
    function ImageCreateFromType($type,$filename) {
     $im = null;
     switch ($type) {
       case 1:
         $im = ImageCreateFromGif($filename);
         break;
       case 2:
         $im = ImageCreateFromJpeg($filename);
         break;
       case 3:
         $im = ImageCreateFromPNG($filename);
         break;
      }
      return $im;
    }
  
    function ApplyWatermark($watermark_path) {
  
      $this-&gt;watermark_path = $watermark_path;
  
      // Determine image size and type
      $size = getimagesize($this-&gt;image_path);
      $size_x = $size[0];
      $size_y = $size[1];
      $image_type = $size[2]; // 1 = GIF, 2 = JPG, 3 = PNG
  
      // load source image
      $image = $this-&gt;ImageCreateFromType($image_type, $this-&gt;image_path);
  
      // Determine watermark size and type
      $wsize = getimagesize($watermark_path);
      $watermark_x = $wsize[0];
      $watermark_y = $wsize[1];
      $watermark_type = $wsize[2]; // 1 = GIF, 2 = JPG, 3 = PNG
  
  
  
      // load watermark
      $watermark = $this-&gt;ImageCreateFromType($watermark_type, $watermark_path);
  
      // where do we put watermark on the image?
      $dest_x = $size_x - $watermark_x - $this-&gt;offset_x;
      $dest_y = $size_y - $watermark_y - $this-&gt;offset_y;
  
      imagealphablending($image, true);
   imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_x, $watermark_y, 100);  
  
      $this-&gt;image = &amp;$image;
      $this-&gt;watermark = &amp;$watermark;
      $this-&gt;image_type = $image_type;
  
    } // ApplyWatermark
  
    function OutputImageInternal($filename='') {
  
      $im = &amp;$this-&gt;image;
      $res = null;
  
      $image_type = ($this-&gt;force_image_type != -1 ? $this-&gt;force_image_type : $this-&gt;image_type);
  
      // ImageGIF is not included into some GD2 releases, so it might not work
      // output png if gifs are not supported
      if(($image_type == 1)  &amp;&amp; !function_exists('imagegif')) $image_type = 3;
  
      switch ($image_type) {
        case 1:
          if ($this-&gt;save_to_file) {
            $res = ImageGIF($im,$filename);
          }
          else {
            header("Content-type: image/gif");
            $res = ImageGIF($im);
          }
          break;
        case 2:
          if ($this-&gt;save_to_file) {
            $res = ImageJPEG($im,$filename,$this-&gt;quality);
          }
          else {
            header("Content-type: image/jpeg");
            $res = ImageJPEG($im, NULL, $this-&gt;quality);
          }
          break;
        case 3:
          if (PHP_VERSION &gt;= '5.1.2') {
            // Convert to PNG quality.
            // PNG quality: 0 (best quality, bigger file) to 9 (worst quality, smaller file)
            $quality = 9 - min( round($this-&gt;quality / 10), 9 );
            if ($this-&gt;save_to_file) {
              $res = ImagePNG($im, $filename, $quality);
            }
            else {
              header("Content-type: image/png");
              $res = ImagePNG($im, NULL, $quality);
            }
          }
          else {
            if ($this-&gt;save_to_file) {
              $res = ImagePNG($im, $filename);
            }
            else {
              header("Content-type: image/png");
              $res = ImagePNG($im);
            }
          }
          break;
      }
  
      return $res;
  
    }
  
    function Output($type = -1) {
      $this-&gt;force_image_type = $type;
      $this-&gt;save_to_file = false;
      $this-&gt;OutputImageInternal();
    }
  
    function SaveAsFile($filename, $type = -1) {
      $this-&gt;force_image_type = $type;
      $this-&gt;save_to_file = true;
      $this-&gt;OutputImageInternal($filename);
    }
  
    function Free() {
      imagedestroy($this-&gt;image);
      imagedestroy($this-&gt;watermark);
    }
  
  }
  </code></pre>