Bbwaycom Site's
Bbwaycom Site's

Reputation: 19

Placing an image watermark

I have this code for watermarking text onto an image

if($type==".jpg" or $type==".jpeg"){
  $im = imagecreatefromjpeg($uploaddir.$randnum);
}elseif($type==".gif"){
  $im = imagecreatefromgif($uploaddir.$randnum);   
}else{
  $im = imagecreatefrompng($uploaddir.$randnum); 
}

$imagesize = getimagesize($uploaddir.$randnum);

$x_offset = 7;
$y_offset = 8;

$textcolor  = imagecolorallocate($im, 0xCC, 0xCC, 0xCC);
$textcolor2 = imagecolorallocate($im, 0x00, 0x00, 0x00);
imagestring($im, 5, $x_offset, $y_offset, strtoupper($_POST['code']), $textcolor2);
if($type==".jpg" or $type==".jpeg"){
  imagejpeg($im,$uploaddir.$randnum,100);
}elseif($type==".gif"){
  imagegif($im,$uploaddir.$randnum,100);   
}else{
  imagepng($im,$uploaddir.$randnum,8);    
}

The code above is printing the watermark in the top left... But I want it to be written on the bottom in the right.

any help guys

regards

Upvotes: 0

Views: 186

Answers (3)

Crashspeeder
Crashspeeder

Reputation: 4311

Try using this where you're currently using imagestring().

$font_size = 5;
$margin = 7;
$text_width = imagefontwidth($font_size)*strlen($_POST['code']);
$text_height = imagefontheight($font_size); //assuming it's one line
imagestring($im, $font_size, $imagesize[0] - $text_width - $margin, $imagesize[1] - $text_height - $margin, strtoupper($_POST['code']), $textcolor2);

Change $margin and $font_size to meet your needs.

Upvotes: 0

cegfault
cegfault

Reputation: 6632

If you want to move it to the bottom, just change your y offset to be the bottom of the image, instead of '8' (which is probably near the top):

$y_offset = $imagesize['height'] - 7;

Upvotes: 0

amindfv
amindfv

Reputation: 8448

This is the line that actually places the watermark:

imagestring($im, 5, $x_offset, $y_offset, strtoupper($_POST['code']), $textcolor2);

The horizontal position of the watermark will depend on what you set $x_offset to be. Right now it's 7, which means 7 pixels from the left.

To get it to be 7 pixels form the right, set it to be the width of the whole image, minus (7 + the width of the watermark)

Find the width of the watermark with imagettfbbox.

The principles are the same for setting the vertical position.

Upvotes: 2

Related Questions