Problem with PHP & Watermarking

Discussion in 'PHP' started by screamfine, Jan 30, 2009.

  1. #1
    Hi,

    I'm using the following function to watermark a picture on the server:

    
    function ks_watermark($image, $watermark, $save_as, $position=9, $transparency=50, $t_x=0, $t_y=0) {
    	// Positionen:
    		// 1 oben links
    		// 2 oben mittig
    		// 3 oben rechts
    		// 4 Mitte links
    		// 5 Mitte
    		// 6 Mitte rechts
    		// 7 unten links
    		// 8 unten mittig
    		// 9 unten rechts
    	// erlaubt sind png und jpeg
    	if($position < 1 || $position > 9)
    		return FALSE;
    	if(!file_exists($image) || !file_exists($watermark))
    		return FALSE;
    	$infos_img = getimagesize($image);
    	$infos_wat = getimagesize($watermark);
    	if(!in_array($infos_img[2], array(2,3)) ||     !in_array($infos_wat[2], array(2,3)))
    		return FALSE;
    	if($infos_img[0]<$infos_wat[0] || $infos_img[1]<$infos_wat[1])
    		return FALSE;
    	if($infos_wat[0]<$t_x || $infos_wat[1]<$t_y)
    		return FALSE;
    	$transparency = 100 - $transparency;
    	if($transpareny < 0 || $transpareny > 100)
    		return FALSE;
    	@unlink($save_as);
    
    	// Position x
    	switch (($position-1)%3) {
    		case 0:
    			$pos_x = 0;
    			break;
    		case 1:
    			$pos_x = round(($infos_img[0]-$infos_wat[0])/2, 0);
    			break;
    		case 2:
    			$pos_x = $infos_img[0]-$infos_wat[0];
    			break;
    	}
    	// Position y
    	switch (floor(($position-1)/3)) {
    		case 0:
    			$pos_y = 0;
    			break;
    		case 1:
    			$pos_y = round(($infos_img[1]-$infos_wat[1])/2, 0);
    			break;
    		case 2:
    			$pos_y = $infos_img[1]-$infos_wat[1];
    			break;
    	}
    	// watermark-procedure
    	if($infos_img[2] == 2)
    		$img_image = imagecreatefromjpeg($image);
    	if($infos_img[2] == 3)
    		$img_image = imagecreatefrompng($image);
    	if($infos_wat[2] == 2)
    		$img_watermark = imagecreatefromjpeg($watermark);
    	if($infos_wat[2] == 3)
    		$img_watermark = imagecreatefrompng($watermark);
    	imagealphablending($img_image, TRUE);
    	imagealphablending($img_watermark, TRUE);
    	imagecolortransparent($img_watermark, imagecolorat($img_watermark, $t_x, $t_y));
    	imagecopymerge($img_image, $img_watermark, $pos_x, $pos_y, 0, 0, $infos_wat[0], $infos_wat[1], $transparency);
    	if(strtolower(substr($save_as, -3)) == "png") {
    		if(imagepng($img_image, $save_as))
    			return TRUE;
    		else
    			return FALSE;
    	} else {
    		if(imagejpeg($img_image, $save_as))
    			return TRUE;
    		else
    			return FALSE;
    	}
    	return FALSE;
    }
    
    PHP:
    I'm using a PNG 24-Bit with transparency. But after bringing the watermark png on the image ... the transparent area is also filled with the colour of the watermark picture (e.g. if my watermark picture is a yellow circle with transparent areas all around it ... the final watermarked picture doesn't show the yellow circle but a yellow rectangle => transparent areas aren't visible).

    I think it must be the function, but I have no clue how I can get this to work?

    Can anybody help me?
     
    screamfine, Jan 30, 2009 IP
  2. Sangeetha.M

    Sangeetha.M Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    
    function watermark($SourceFile, $WatermarkFile, $SaveToFile = NULL)
    
    {
    
        $watermark = @imagecreatefrompng($WatermarkFile) 
    
    
        or exit('Cannot open the watermark file.');
    
        imageAlphaBlending($watermark, false);
    
        imageSaveAlpha($watermark, true);
    
        $image_string = @file_get_contents($SourceFile) 
    
    
        or exit('Cannot open image file.');
    
        $image = @imagecreatefromstring($image_string)
    
        or exit('Not a valid image format.');
    
        $imageWidth=imageSX($image);
    
        $imageHeight=imageSY($image);
    
    
        $watermarkWidth=imageSX($watermark);
    
        $watermarkHeight=imageSY($watermark);
    
        $coordinate_X = ( $imageWidth - 5) - ( $watermarkWidth);
    
    
        $coordinate_Y = ( $imageHeight - 5) - ( $watermarkHeight);
    
        imagecopy($image, $watermark, $coordinate_X, $coordinate_Y, 
    
    
            0, 0, $watermarkWidth, $watermarkHeight);
    
        if(!($SaveToFile)) header('Content-Type: image/jpeg');
    
        imagejpeg ($image, $SaveToFile, 100);
    
    
        imagedestroy($image);
    
        imagedestroy($watermark);
    
        if(!($SaveToFile)) exit;
    
    }
    $SourceFile="path_of_the_sourcefile";
    $WatermarkFile="Path_of_the_watermark_image";
    watermark($SourceFile,$WatermarkFile);
    ?>
    PHP:
    this is script which i use to watermark, its working fine for me...if want just try this out...
     
    Sangeetha.M, Jan 30, 2009 IP
  3. screamfine

    screamfine Guest

    Messages:
    114
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, your script is working but with your script there's another problem: gradient fills are being removed ...
     
    screamfine, Feb 4, 2009 IP