Log in

View Full Version : [GD Lib] Create Watermark



sysout
02-06-2009, 03:41 AM
how to creates watermark for copyrighted images?

Like this I mean :
http://www.fashiongrosir.com/products_pictures/BJbeyonce-.jpg

djr33
02-06-2009, 05:12 AM
Watermarks may appear simple, but sometimes are actually complex. It involves placing one image over another then blending them in such a way that they don't really take away from the viewing experience but are very hard to remove. If you just have a white image at a low opacity, it actually would be possible, if someone cared enough, to remove it by re-darkening those areas. The best watermarks remove enough data from the original image (by layering something on top) that it isn't possible to recover it very well, and they also do so in a non-linear fashion.
Many watermarks use a technique similar to the "overlay" layer mode in Photoshop (and similar graphics programs), which, in short, uses an algorithm not of direct percentage mixing, but rather that calculates based on the pixel values of both layers what the next pixel should be like. It's not that it is particularly different to the eye, but once it isn't a linear combination, it's much harder to extract.
However, it's not like people are going to work that hard to steal images in general, so having anything may be enough. Using an images with more colors in it than just white, for example, might be one way to encourage them to leave the image alone more, though ;)

Now, as for doing it with the GD library, there are a few things to consider:
1. GD is incredibly resource intensive. When you can avoid using it, do so. It's generally a bad idea to run GD on the fly unless it's for a very specific purpose, or very small images. Processing many large images (let's say anything over 200x200px) will not only be slow but also cause a load to your server you probably don't want.
2. It is a convenience, though, so one way around this is to pre-process with GD-- that is you can dynamically run it on all of your images and then just load the now saved results. That's fine and won't be constantly taxing on the server. You can even setup a php script that will do that automatically if an image has no watermarked version yet to display (a new one) then save that for all future loads. I've done this with thumbnail galleries for images before-- it searches for all the images and grabs their thumbnails, but when it comes across one that has no thumbnail it generates that too then adds the thumbnail to the page just like all the others then will never need to generate it again.
3. Working with laying in GD can be a pain. It's possible, but probably not something you want to code yourself, at least in a pixel by pixel way. I believe there are some layering functions that may help you, so one option is to create your "watermark" on a black image (that is actually apply it to the black image) in photoshop or whatever, then save that as a file on your server. Your script can then take that watermark and layering it directly (using alpha channels, which are supported by GD) and it's no more work than just layering two images directly-- no complex calculations. Alternatively, you can do actual calculations, but that will be slower/more server-intensive.
4. This is really a time to use google and look for an existing script. "php gd watermark" should get you there and I'm sure it's something people use GD for somewhat often. This isn't a script you'd want to have to write yourself, nor is it particularly rewarding to have your own version-- it'll end up like what someone else has anyway.

So I hope that gets you started. Sorry for the vagueness above, but there are a few things you have to figure out to make it all work, like whether you'll pregenerate the images, or actually run it each time they load (usually a really bad idea, unfortunately).
In the end, one of the easiest answers here is to just process them in Photoshop (etc), even if you have a couple hundred or thousand, perhaps using actions, then upload them. Alternatively, make that script pregenerate them and then not load each time. But do be careful about running it too often/for too long at one time, because I was once using a fairly simple image generator (making a complex gradient, basically) and the hosting account was shut down for server overload. Of course they will probably be understanding, but that kind of server load is not the sort of thing they're used to seeing, and they'll probably ask you to not run it like that again. If you have your own server, that's not much of an issue, though you will still need to worry about time. One option around all of this would be to instead run it locally (either by using your computer as a server, or running php as an exe: http://www.bambalam.se/bamcompile/) then uploading after that.

sysout
02-06-2009, 12:10 PM
any ebook for PHP gd library?

thanks

djr33
02-06-2009, 09:24 PM
I haven't found anything particularly useful. Basically, aside from their potential (in theory they can do lots of cool stuff), the GD functions are a mess and usually worth avoiding, even if just for how confusing they can be if you're not used to them (and few people seem to be-- I've used them more than most around here at least, and it's still an uphill battle to get something to work, not that I'm claiming to be particularly good with them).
They aren't hard/complex as much as just very strict. The best way I have found to learn is to copy examples, and a lot can be found on php.net documenting each of the functions.

Nile
02-06-2009, 10:30 PM
Not that hard. Look at this (http://localhostr.com/files/631c6e/gdopac.tar).
Please Note: I've md5ed the original, this way users can't find the file unless their smart. ;)

techietim
02-06-2009, 10:59 PM
Nile's solution was a bit bugged. This is not perfect, but a little better.


<?php
header('Content-type: image/jpeg');
$img_src = '782f7448d47c544edc346b92ae12140a.jpg';
$message = 'Watermark Test';
$im = imagecreatefromjpeg($img_src);
$water_mark = imagecolorallocatealpha($im, 0, 0, 0, 60);
list($width, $height) = getimagesize($img_src);
imagefttext($im, $width / strlen($message), 0, 0, $height - 5, $water_mark, 'arial.ttf', $message);
imagejpeg($im);
imagedestroy($im);

sysout
02-11-2009, 09:08 AM
yeah, it works, niceee, thanks a lot :)

$img_src = "782f7448d47c544edc346b92ae12140a.jpg"; (the file's name)

Is it possible to detect the file's name using GDLibrary?

Ex :
FaceBook :
- Johan.jpg
- Micheal.jpg
- Christina.jpg

I wanna make a watermark, using it's file name from FaceBook folder, and then "REPLACE" the original :D

djr33
02-11-2009, 03:10 PM
I don't really understand what you are saying. You cannot reverse md5-- that's not possible, and that's the point of the algorithm-- it's one way. But you could store that filename while you're converting it and then delete the original with that name after it's been converted. However, I don't see the point-- just create a copy folder and only put those on the website. That way you'll always have a backup of the originals if you want to change the watermark, etc.

Dirt_Diver
02-11-2009, 04:19 PM
http://md5crack.com

Can hack them

JasonDFR
02-11-2009, 09:08 PM
http://md5crack.com

Can hack them

That might technically be hacking, but if so, you can do the same thing with any hash.

And that is thwarted by not using 'password' for your password. Or generally not hashing words from the dictionary in the first place.

Crack this: 5cea7395e83dcfc3bb87f2b45f5422d6

sysout
02-15-2009, 10:46 AM
sorry, what I meant is like this :

I have a folder and it's file like this :

Images
l- file1.jpg
l- file2.jpg
l- file3.jpg

----------------------------------------
what I wanna do is create a watermark on each of the files in Images Folder, which text is according to it's file name.

djr33
02-17-2009, 12:45 AM
You can use the functions opendir() and readdir() to run through all of the files in a certain folder, and the filename will be accessible in that process.

Dirt_diver, there are libraries for md5 available, but that is not the same as being breakable. Those libraries are based on pairs-- they encrypt input and look at the md5 output, then when you search the library it does the opposite-- it's generating all of it's results, and it cannot actually reverse the process, just mirror it and show you if it knows what the original input was. The algorithm is still secure, but it's output may, by brute force, be eventually cracked. Obviously a shorter/more common password will be less secure for this reason. Because of this, it is possible that md5 is becoming weak for passwords on websites (though it's still very common, just don't use something that's in the libraries-- make it random, not just not a dictionary word, but just something no one else would think of-- put two things together that don't belong together), but this will have no effect whatsoever on images, because none of those libraries can possibly actually contain images-- if it does have a match for the md5 string it decrypts, then it will be a password that happens to coincide with the md5 string of the image, but it is more likely that nothing will happen at all. For this reason, uploading an image/file as a password has been considered by some to be a good solution, aside from the hassle of actually doing that/coding it.
Plus, in this case, there is no real issue with it at all, as no one will be attempting to decrypt it, most likely, or at least they won't know where the actual image directory is, so it will be worthless to them.

As a side note, it'll be stronger if the image itself (as a file) is used as the md5 input, rather than just the filename. (Though it'll also be slower, but from what I've seen it's still plenty fast for most purposes, as long as it isn't looped many times, etc.)