Pathik Vejani Pathik Vejani - 5 months ago 34
PHP Question

Watermark text position bottom right

I am using following code to set watermark, but it sets watermark to the center, I want to set it bottom right position.

<?php
function watermarkImage($fileget, $watermarktext, $saveto)
{
list($width, $height) = getimagesize($fileget);
$image_p = imagecreatetruecolor($width, $height);
$size = getimagesize($fileget);
$dest_x = $size[0] - $width - 5;
$dest_y = $size[1] - $height - 5;
$image = imagecreatefromjpeg($fileget);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font = FONT_PATH;
$font_size = 15;
$bbox = imagettfbbox($font_size, 0, $font, $watermarktext);
$x = $bbox[0] + (imagesx($image) / 2) - ($bbox[4] / 2) - 25;
$y = $bbox[1] + (imagesy($image) / 2) - ($bbox[5] / 2) - 5;
imagettftext($image_p, $font_size, 0, $x, $y, $white, $font, $watermarktext);
if ($saveto<>'') {
imagejpeg ($image_p, $saveto, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
}
?>


I am referring this answer: answer-1

How to set the position to bottom right direction?

Answer

Don't divide the x and y position by 2. That's why it is in the middle. On the following lines:

$x = $bbox[0] + (imagesx($image) / 2) - ($bbox[4] / 2) - 25;
$y = $bbox[1] + (imagesy($image) / 2) - ($bbox[5] / 2) - 5;

This should work, since the position will be the bottom-right:

$x = $bbox[0] + (imagesx($image)) - ($bbox[4] / 2) - 25;
$y = $bbox[1] + (imagesy($image)) - ($bbox[5] / 2) - 5;

EDIT For bottom right:

 $x = $bbox[0] + (imagesx($image)) - ($bbox[4] / 2) - 130;
 $y = $bbox[1] + (imagesy($image)) - ($bbox[5] / 2) - 30;