1

I have a website I monetize with numerous original pictures on it, I want people to visit the website to see the original pictures and have search engines only show the pictures with transparent watermarks.

The following is an example of what I mean by a transparent watermark. Except of course, I replace "Test!" with the company name.

bumpmap

Here's the PHP code I created so far:

<?php
$txt = "TEST!";
header( "Content-type: image/jpeg", true );

$w  = imagefontwidth(5) * ( strlen( $txt ) + 1 );
$h  = imagefontheight(5) * 2;
$i2 = imagecreatetruecolor( $w,$h );

imagesavealpha( $i2, true );
imagealphablending( $i2, false );
imagefill( $i2, 0, 0, imagecolorallocatealpha( $i2, 255, 255, 255, 127 ) );
imagestring( $i2, 3, 10, 10, $txt, imagecolorallocate( $i2, 255, 0, 0) );
$i = imagecreatefromjpeg( "someimage.jpg" );
imagecopyresized( $i, $i2, 2, 2, 0, 0, $w * 5, $h * 7, $w, $h );
imagejpeg( $i, null, 100 );
imagedestroy( $i );
imagedestroy( $i2 );
?>

$i2 is the resource variable for the image box in which I added the text, and $i1 is for the large image to place the text on. $w and $h represent width and height of the text image box. This code is able to produce the text on top of the image without the background box showing, but it doesn't produce the bump-map effect like what is shown in the above image.

Can anyone guide me as to what functions, mathematics or code I need to use to create the bump-map effect?

I feel my solution requires special manipulation of a block of pixels but I don't know how to do it for this effect.

I also want to stick with the PHP GD Image library.

Dai
  • 141,631
  • 28
  • 261
  • 374
Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37
  • This might be interesting [gd-text](https://github.com/stil/gd-text). You can apply text shadows and add font-face. – DavidDomain Jul 23 '15 at 22:36

1 Answers1

1

I'd recommend using a semi-transparent PNG and adding it as a watermark rather than using the imagestring functions.

With the imagestring functions you'd theoretically needs to create two strings, one slightly higher and to the left than the other, and then somehow calculate the pixels that where both the strings are present and make that transparent. This will then mean the left-over parts would be black / white and give that 3d effect.

Example 1

If the watermark is the same for each image, then you can use a semi-transparent PNG. The benefit of this is that it allows for far more effects, and as a result a better result.

<?php

// Watermark will take up 80% of the photo's width
$size = 80;

$im = imagecreatefromjpeg(dirname(__FILE__) . '/photo.jpg');
$oi = imagecreatefrompng(dirname(__FILE__) . '/watermark.png');
$w1 = imagesx($im);
$h1 = imagesy($im);
$w2 = imagesx($oi);
$h2 = imagesy($oi);

$w = $w1 - (($w1 / 100) * (100 - $size));
$h = ($w * $h2) / $w2;

imagecopyresampled($im, $oi, $w1 / 2 - $w / 2, $h1 / 2 - $h / 2, 0, 0, $w, $h, $w2, $h2);

header('Content-type: image/jpg');
imagejpeg($im, null, 100);
imagedestroy($im);

Will produce the following output:

Output

Original Photo / Watermark

Example 2

If the watermark needs to be different for each image, then you can use imagettftext to create the text using a font of your choosing (much better than imagestring but limited as to what effects you can do).

<?php

$size = 55;
$angle = 15;
$text = uniqid();
$font = 'BebasNeue.otf'; // http://www.dafont.com/bebas-neue.font

$im = imagecreatefromjpeg(dirname(__FILE__) . '/photo.jpg');
$w = imagesx($im);
$h = imagesy($im);

$text_colour_1 = imagecolorallocatealpha($im, 0, 0, 0, 99);
$text_colour_2 = imagecolorallocatealpha($im, 255, 255, 255, 90);
$box = imagettfbbox($size, $angle, dirname(__FILE__) . '/' . $font, $text);
imagettftext($im, $size, $angle, (($w - $box[4]) / 2) - 1, (($h - $box[5]) / 2) - 1, $text_colour_1, dirname(__FILE__).'/' . $font, $text);
imagettftext($im, $size, $angle, ($w - $box[4]) / 2, ($h - $box[5]) / 2, $text_colour_2, dirname(__FILE__).'/' . $font, $text);

header('Content-type: image/jpg');
imagejpeg($im, null, 100);

Output:

Output

Community
  • 1
  • 1
Jamie Bicknell
  • 2,306
  • 17
  • 35
  • Its an idea but I want to make the image stand out better as well. I tried the two-string approach once, but maybe I'll try it again to see what I can come up with. The less noticeable the watermark is to the general public, the better, but I need it there as backup in case I have to sue an image thief in the future. – Mike -- No longer here Jul 23 '15 at 22:31
  • @Mike I've updated my answer with a working example of how a semi-transparent watermark can look. – Jamie Bicknell Jul 23 '15 at 23:02
  • Thanks but I'm wondering about how to generate one. For example, if I stamped each photo with a time as a watermark, it would be not feasible to draw a picture of the time as a transparent watermark. I am dealing with over 500,000 photos. – Mike -- No longer here Jul 23 '15 at 23:23
  • "Except of course, I replace "Test!" with the company name" - From what you've said here I've assumed that the watermark will be the same for each photo. – Jamie Bicknell Jul 23 '15 at 23:25
  • @Mike I've added an example that would use a TTF font and you can specify the watermarked text on the fly. – Jamie Bicknell Jul 23 '15 at 23:57
  • Is there a way I can do it with the standard text rather than TTF because I don't believe I have that installed. – Mike -- No longer here Jul 24 '15 at 00:23
  • The Imagestring function has 5 fonts built in, the largest being about 16 pixels tall, which wouldn't be suitable as a watermark. – Jamie Bicknell Jul 24 '15 at 00:26