Question

Please check my code -

<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
   list($width, $height) = getimagesize($SourceFile);
   $image_p = imagecreatetruecolor($width, $height);
   $image = imagecreatefromjpeg($SourceFile);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
   $black = imagecolorallocate($image_p, 0, 0, 0);
   $font = 'dum13d100.ttf';
   $font_size = 10;
   imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
   if ($DestinationFile<>'') {
      imagejpeg ($image_p, $DestinationFile, 100);
   } else {
      header('Content-Type: image/jpeg');
      imagejpeg($image_p, null, 100);
   };
   imagedestroy($image);
   imagedestroy($image_p);
}

$SourceFile = 'myimage.jpg';
$DestinationFile = 'images/myimage2.jpg';
$WaterMarkText = 'CHINMAY';
watermarkImage ($SourceFile,$WaterMarkText,$DestinationFile);
?>

Original Source code - http://www.learnphp.in/index.php?page=forum&op=viewThread&id=687

Above code is showing -

Image watermark shows in header

I want to -

enter image description here

The above image height is 280px

I have increased my text height 250($y angel) from top check below code-

imagettftext($image_p, $font_size, 0, 10, 250, $black, $font, $WaterMarkText);

It works fine but my question is if someone upload 800px or more size image the text will not showing in footer it comes 250px from top.

I need to my watermark image will show padding-button:20px from left of the image.

Please anybody help me how to make height dynamic?

Was it helpful?

Solution

Change your line

imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);

to

imagettftext($image_p, $font_size, 0, 10, $height - 20, $black, $font, $WaterMarkText);

OTHER TIPS

You can calculate the height of the image using getimagesize() (key index 1 being the height) then pad from the bottom by taking 20 away from the height.

$height = getimagesize($image)[1];
imagettftext($image_p, $font_size, 0, 10, $height - 20, $black, $font, $WaterMarkText);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top