Link to home
Start Free TrialLog in
Avatar of jayseena
jayseenaFlag for United Arab Emirates

asked on

PHP GD Watermarking

Hi,
I am trying to add a watermark for images in a gallery. Earlier I have used "imagecopymerge" successfully, but this time I am stuck. Relevant code is below (Original working version without watermark first)

The second part is with the manipulation I tried, I have marked where all the changes are.

With this I get a lot of error, the first one being "Warning: Wrong parameter count for imagestring() in C:\wamp\www\automarket3\admin\images\store\details.php on line 285"

And second one "Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\automarket3\admin\images\store\details.php on line 354"

Thanks for any help.
Jay
#-#############################################
# desc: resizes image using GD library
# param: ($fullFilename) filename of full size image ($thumbFilename) filename to save thumbnail as ($size) max width or height to resize to
# returns: true if image created, false if failed
function ResizeImageUsingGD($fullFilename, $thumbFilename, $size) {
 
	list ($width,$height,$type) = GetImageSize($fullFilename);
 
	if($im = ReadImageFromFile($fullFilename,$type)){
		//if image is smaller than the $size, show the original
		if($height <= $size && $width <= $size){
			$newheight=$height;
			$newwidth=$width;
		}
		//if image height is larger, height=$size, then calc width
		else if($height > $width){
			$newheight=$size;
			$newwidth=round($width / ($height/$size));
		}
		//if image width is larger, width=$size, then calc height
		else{
			$newwidth=$size;
			$newheight=round($height / ($width/$size));
		}
 
		$im2=ImageCreateTrueColor($newwidth,$newheight);
		ImageCopyResampled($im2,$im,0,0,0,0,$newwidth,$newheight,$width,$height);
 
		return WriteImageToFile($im2,$thumbFilename,$type);
	}
 
	return false;
}#-#ResizeImageUsingGD()
 
 
 
-----------------------Edited Version ---------------------------------
 
 
#-#############################################
# desc: resizes image using GD library
# param: ($fullFilename) filename of full size image ($thumbFilename) filename to save thumbnail as ($size) max width or height to resize to
# returns: true if image created, false if failed
 
 
function ResizeImageUsingGD($fullFilename, $thumbFilename, $size) {
 
// Edited for adding watermark
// Set the margins for the stamp and get the height/width of the stamp image
 
$stamp = imagecreatefrompng('logo.png');
$marge_right = 0;
$marge_bottom = 0;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
 
// End of watermark edit - part 1
 
	list ($width,$height,$type) = GetImageSize($fullFilename);
 
	if($im = ReadImageFromFile($fullFilename,$type)){
		//if image is smaller than the $size, show the original
		if($height <= $size && $width <= $size){
			$newheight=$height;
			$newwidth=$width;
		}
		//if image height is larger, height=$size, then calc width
		else if($height > $width){
			$newheight=$size;
			$newwidth=round($width / ($height/$size));
		}
		//if image width is larger, width=$size, then calc height
		else{
			$newwidth=$size;
			$newheight=round($height / ($width/$size));
		}
 
		// Added $dst_img for watermark - edit part2
		$dst_img = imagecreatetruecolor($newwidth, $newheight);			
    	imagecopyresampled($dst_img,$im,0,0,0,0,$newwidth,$newheight,$width,$height);
		
	
		$im2 = imagecopymerge($dst_img, $stamp, imagesx($dst_img) - $sx - $marge_right, imagesy($dst_img) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 10);
		imagestring ($im2, 2,10, $destHeight-20);
		
		
	
 
		return WriteImageToFile($im2,$thumbFilename,$type);
	}
 
	return false;
}#-#ResizeImageUsingGD()

Open in new window

Avatar of Valleriani
Valleriani
Flag of Sweden image

Hey mate, try the coding below. Before that, you don't need imagestring for watermarks, thats more for text watermarks, not image watermarks.

I added the variable $stampt , which is stamp transparency, if you want the stamp to be more/less transparent on the image, change it higher/lower. 100 Being full and 0 being invisible.

Basicly, don't need to create a new variable for imagecopymerge, just use the current one, sort of like how imagecopyresampled is...

Hope this helps and this is what you wanted:


function ResizeImageUsingGD($fullFilename, $thumbFilename, $size) {
 
$stamp = imagecreatefrompng('test.png');
$stampt = 25; //Stamp transparency amount
$marge_right = 0;
$marge_bottom = 0;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
 
        list ($width,$height,$type) = GetImageSize($fullFilename);
 
        if($im = ReadImageFromFile($fullFilename,$type)){
                //if image is smaller than the $size, show the original
                if($height <= $size && $width <= $size){
                        $newheight=$height;
                        $newwidth=$width;
                }
                //if image height is larger, height=$size, then calc width
                else if($height > $width){
                        $newheight=$size;
                        $newwidth=round($width / ($height/$size));
                }
                //if image width is larger, width=$size, then calc height
                else{
                        $newwidth=$size;
                        $newheight=round($height / ($width/$size));
                }
 
                $im2=ImageCreateTrueColor($newwidth,$newheight);
                ImageCopyResampled($im2,$im,0,0,0,0,$newwidth,$newheight,$width,$height);
				imagecopymerge($im2, $stamp, imagesx($im2) - $sx - $marge_right, imagesy($im2) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), $stampt);
 
                return WriteImageToFile($im2,$thumbFilename,$type);
        }
 
        return false;
}#-#ResizeImageUsingGD()

Open in new window

Oh, you'll want to change test.png to whatever your watermark is.

20 is a small size for a image though. would be hard to see.

As well http://se2.php.net/manual/en/function.imagestring.php for imagestring if you want to know about it.
Avatar of jayseena

ASKER

Valleriani, thanks for your help. The script is perfect and doesn't throw any errors. But I have messed up something else in between and am trying to figure out what and where. I will check this out and get back.
Once again, thanks.
Ok no problem! Just let me know :)
Hi,
Apparently it worked well. But I made a mistake in identifying the part of the script. The part I mentioned was for creating thumbnail. What I wanted was watermark on the enlarged image. I am using a gallery script which takes main images from a dynamic folder and creates thumbnails on the fly. So now I realise that there is no GD role for the main images. I use lightbox to show pop-up image. And the images are uploaded to a dynamic folder using ADDT multiple image upload server behavior.
Any thoughts on how can I achieve the result I am looking for!
Ohh okay, so you want one thumbnail and one watermarked normal sized image. Let me check.
How about this?

Just some notes:

This is now : ResizeImageUsingGD(Filetoget, Filetowritetoowithwatermark, thumbnail, sizeofthumbnail);
Extra field, "File to write too with watermark" will be the large version of the image WITH the watermark.
EX. ResizeImageUsingGD("test.jpg", "test-l.jpg", "test-t.jpg", 50);

There is an issue however, if you try to write to the same file you are opening, it will cause an error. This was solved by checking if the filename = writename, and chmodding the image to 777 to be able to change, guess it is picky. Note, the script must have chmod 777 as well (probably does anyways.) If you don't WANT to leave the image to 777, (doesn't really matter on images though), you can chmod the filename back down to whatever you want after writing the images.
EX. ResizeImageUsingGD("test.jpg", "test.jpg", "test-t.jpg", 50);


I hope this is what you ment, I didn't really understand to well.
function ResizeImageUsingGD($fullFilename, $fullWriteName, $thumbFilename, $size) {
 
$stamp = imagecreatefrompng('logo.png');
$trans = 30; //Stamp transparency amount
$marge_right = 0;
$marge_bottom = 0;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
	if ($fullFilename == $fullWriteName) {
		chmod($fullFilename, 0777);
	}
 
        list ($width,$height,$type) = GetImageSize($fullFilename);
 
        if($im = ReadImageFromFile($fullFilename,$type)){
                //if image is smaller than the $size, show the original
                if($height <= $size && $width <= $size){
                        $newheight=$height;
                        $newwidth=$width;
                }
                //if image height is larger, height=$size, then calc width
                else if($height > $width){
                        $newheight=$size;
                        $newwidth=round($width / ($height/$size));
                }
                //if image width is larger, width=$size, then calc height
                else{
                        $newwidth=$size;
                        $newheight=round($height / ($width/$size));
                }
 
                $im2=ImageCreateTrueColor($newwidth,$newheight);
                ImageCopyResampled($im2,$im,0,0,0,0,$newwidth,$newheight,$width,$height);
                WriteImageToFile($im2,$thumbFilename,$type);
 
				imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), $trans);
                WriteImageToFile($im,$fullWriteName,$type);
        }
 
        return false;
}#-#ResizeImageUsingGD()

Open in new window

Hi,
Most of the thing you said just passed over my head. I appreciate your help. I tried the above script but didn't see what I am looking for. But there is no error thrown except that simply it doesn't show the watermark on the main image.

I will explain a bit more.
At the user end: The images are uploaded to a folder (multiple image upload at once) with the folder name logged to a database.

At the browser end: The gallery pulls the images and the thumbnails are created on the fly and saved to a folder 'thumbnails' within the main folder.

Here is how the main folder pulled:
// folder where full size images are stored (include trailing slash)
$config['fulls'] = $_GET['car_id'];

And the thumbnails here
// folder where thumbnails are to be created (include trailing slash)
$config['thumbs'] = $row_Recordset1['car_T_id'];

I am attaching the code where it says about the images
In between how can I add a trailing slash at the end of $config['fulls'] = $_GET['car_id']; Currently I am doing this manually in the database. The 'fulls' path should show like  myimagefolder/
Hope it's clear now

// for the images on the page
	for($i=$config['start']; $i<$config['max']; $i++){
 
		$thumb_image = $config['thumbs'].$imagelist[$i];
		$thumb_exists = file_exists($thumb_image);
 
		// create thumb if not exist
		if(!$thumb_exists){
			set_time_limit(30);
			if(!($thumb_exists = ResizeImageUsingGD("$config[fulls]$imagelist[$i]", $thumb_image, $config['size']))){
				Oops("Creating Thumbnail image of <strong>$config[fulls]$imagelist[$i]</strong> failed. Possible directory write permission errors.");
			}
		}
 
		$imagelist[$i] = rawurlencode($imagelist[$i]);
 
 
	#########################################################
	# print out the image and link to the page 
	#########################################################
	
		echo '<td>';
			echo '<a href="'. $config['fulls'].$imagelist[$i] .'" title="'. $imagelist[$i] .'" rel="lightbox[]"">';
			echo '<img src="'. $config['thumbs'].$imagelist[$i] .'" alt="'. $imagelist[$i] .'"  border="0">';
			echo '</a>';
		echo '</td>'."\n";
	#########################################################

Open in new window

SOLUTION
Avatar of Valleriani
Valleriani
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
Thanks. I will try this out.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial