Png Transparency and Opacity for Watermarks

From PhotoblogWiki

Jump to: navigation, search

Contents

[edit] Problem

This could be a problem with either PHP or the GD library, I have no really looked into it. The problem is if you want to place a transparent PNG image on top of another image and also make that png have a level of opacity.

UPDATE I recently began to play around with the ImageMagick PECL library for PHP and did the same function for it also

[edit] Issue

So in order to make a image have some opacity is imagecopymerge. That function works great, but if the png file has a background that is transparent it will break the transparency. In fact in my testing it will almost completely break the whole png image.

If you want to keep the transparency you could use the imagecopy function. That function works great but you can't make the png have any sort of opacity.

[edit] Solution

So below is a solution to the problem. I hope I have commented it well enough.

[edit] GD

/**
        *
        * $wm is the image resource for the watermark
        * $x is the location on the horizontal you want to place the watermark
        * $y is the location on the vertical
        * $opacity is the opacity level.. 0-100  (0 = transparent 100=solid)
        * $this->image is the background image... this function is in a class so that is how its referenced.
        *
        **/
        private function placeWaterMark($wm, $x, $y, $opacity) {
                // getting the watermark width
                $w = imagesx($wm);
                // getting the watermark height
                $h = imagesy($wm);
 
                // creating a cut resource
                $cut = imagecreatetruecolor($w, $h);
                // copying that section of the background to the cut
                imagecopy($cut, $this->image, 0, 0, $x, $y, $w, $h);
                // inverting the opacity
                $opacity = 100 - $opacity;
 
                // placing the watermark now
                imagecopy($this->image, $wm, $x, $y, 0, 0, $w, $h);
                imagecopymerge($this->image, $cut, $x, $y, 0, 0, $w, $h, $opacity);
        }

[edit] ImageMagick

/**
        *
        * $wm is the image resource for the watermark
        * $x is the location on the horizontal you want to place the watermark
        * $y is the location on the vertical
        * $opacity is the opacity level.. 0-100  (0 = transparent 100=solid)
        * $this->image is the background image... this function is in a class so that is how its referenced.
        *
        **/
        private function placeWaterMark($wm, $x, $y, $opacity) {
		$h = $wm->getImageHeight();
		$w = $wm->getImageWidth();
 
		$cut = $this->image->clone();
		$cut->cropImage($w, $h, $x, $y);
		$opacity = 1.0 - $opacity;
		$cut->setImageOpacity($opacity);
 
		$this->image->compositeImage($wm, $wm->getImageCompose(), $x, $y);
		$this->image->compositeImage($cut, $cut->getImageCompose(), $x, $y);
 
                $cut->destroy();
 
	}
Personal tools