Mihalism Watermark Code

nesterdwarf

Regular Member
Joined
May 30, 2008
Messages
276
Reaction score
549
So, I've jumped on the Image Hosting bus and one of the things that was bothering me about the MMH script was that uploaders could just link straight to the pics and I'd get no credit for my "generousity"...

Looked around the web and have seen a couple of posts about adding a watermark to all the uploaded images... somebody even dug up the code that worked in the previous version, but didn't with the new version (4.0.2)...

So, I'm not going to take any credit for the code, just for finding the right place to put it :p

Anyway, follow these steps and you should be good to go...

Open "upload.php" and search for this -
Code:
$mmhclass->image->create_thumbnail($filename);
Should be line 53 for those following along in their hymnals...

After that line, copy and paste this into the file -
Code:
$path = "images/";
$mmhclass->image->add_watermark($extension, $path, $filename);

Save the file...

Now go into the "source\includes\" folder and open "imagemagick.php".
Goto the end of the file and go back up inside the first curly bracket ("}") and paste this into the file -
Code:
//New function to Add Watermark
		function add_watermark($extension, $path, $filename)
		{
			global $mmhclass;
			$image_upfile = $path.$filename;
		
			if($extension == "jpeg" || $extension == "jpg")
			{
				$watermark = imagecreatefrompng('css/watermark.png'); 
				imageAlphaBlending($watermark, false);
				imageSaveAlpha($watermark, true); 
				$watermark_width = imagesx($watermark);  
				$watermark_height = imagesy($watermark);  
				$image = imagecreatetruecolor($watermark_width, $watermark_height);  
				$image = imagecreatefromjpeg($image_upfile);  
				$size = getimagesize($image_upfile);  
				$dest_x = $size[0] - $watermark_width - 5;  
				$dest_y = $size[1] - $watermark_height - 5;  
				imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
		
				if(@imagejpeg($image, $image_upfile, 90))
				{
					$img_watermakred = "yes";
				}
		
				imagedestroy($image);
				imagedestroy($watermark);
			}
		
			if($extension == "png")
			{
				$watermark = imagecreatefrompng('css/watermark.png');  
				$watermark_width = imagesx($watermark);  
				$watermark_height = imagesy($watermark);  
				$image = imagecreatetruecolor($watermark_width, $watermark_height);  
				$image = imagecreatefrompng($image_upfile);  
				$size = getimagesize($image_upfile);  
				$dest_x = $size[0] - $watermark_width - 5;  
				$dest_y = $size[1] - $watermark_height - 5;  
				imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
		
				if(@imagepng($image, $image_upfile))
				{
					$img_watermakred = "yes";
				}
		
				imagedestroy($image);
				imagedestroy($watermark);
			}
		
			if($extension == "gif")
			{
				$watermark = imagecreatefrompng('css/watermark.png');  
				$watermark_width = imagesx($watermark);  
				$watermark_height = imagesy($watermark);  
				$image = imagecreatetruecolor($watermark_width, $watermark_height);  
				$image = imagecreatefromgif($image_upfile);  
				$size = getimagesize($image_upfile);  
				$dest_x = $size[0] - $watermark_width - 5;  
				$dest_y = $size[1] - $watermark_height - 5;  
				imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
		
				if(@imagegif($image, $image_upfile))
				{
					$img_watermakred = "yes";
				}
		
				imagedestroy($image);
				imagedestroy($watermark);
			}
		}

Save the file...

Finally, create a png file of whatever you want your watermark to be, save it as "watermark.png" and put it in the root "css" folder. If you want to name it something different or put it somewhere else, just search in the code above for "css/watermark.png" and replace it...

Now all the new photos that are uploaded will be marked with your watermark...

If that helped, click the button...

ND
 
errr.... i didnt understand the last code placement.... Goto the end of the file and go back up?
 
Yeah, guess that could have been clearer...you want to include it in the class "mmhclass_image_functions" below all the other standard functions...

ND
 
sorry.. im noob.... in what line exactly we have to put the last code?


-NewBiE-
 
Search for this -

Code:
      }
?>

Paste it in before that...that's it.

ND
 
how to make the watermark have transparent background?
 
I knew that one would come up... I had to jack with it for awhile to get it to work... I'll try and explain it...

You'll need to find a color that isn't in your overlay image and use that as your background color. For example, my overlay image was black and white, so I used red as my background.

Now, when you go to save the image, you'll need to use something like Photoshop "Save for the Web..." In that dialog box, you can select the color that you want to be transparent (red, for example) and then save the image as a png file...

Not the greatest explaination, but if you need a better one, you can search "setting the transparency index in a png" and find alot better examples.

ND
 
Well, this is already posted in mihalism support forum.isnt it? bw have any other mod to share?
 
Back
Top