Question

I have an image and I'm trying to watermark the image using text.

I have done with code part, but no image see been viewed with watermark text.

Here is the code below:

$imagetobewatermark="images/muggu.png";
list($width,$height)=getimagesize($imagetobewatermark);
$imagetobewatermark=imagecreatetruecolor($width,$height);
$mark=imagecreatefrompng($imagetobewatermark);
imagecopy($imagetobewatermark,$mark,0,0,0,0,$width,$height);
$wartermarktext="Muggu";
$font="../font/century gothic.ttf";
$fontsize="15";
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255);
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);

Tell me If I'm wrong.

Thank you.

Was it helpful?

Solution

One problem I can see straight away is that the $imagetobewatermark variable starts off as a string, then becomes a new blank image object (not an existing image), and when you subsequently create the mark image object, it's not going to work because $imagetobewatermark is no longer a string.

Try:

$imagetobewatermark=imagecreatefrompng("images/muggu.png");
$watermarktext="Muggu";
$font="../font/century gothic.ttf";
$fontsize="15";
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255);
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);

EDIT: I failed to notice a typo in your text variable $wartermarktext, which should be $watermarktext. Correct this and it should work.

OTHER TIPS

You had some spellings wrong and did not use the ressource. Here corrected:

$imagetobewatermark = "muggu.png";
list ($width, $height) = getimagesize($imagetobewatermark);
$res = imagecreatetruecolor($width, $height);

$mark = imagecreatefrompng($imagetobewatermark);
//make sure here to use the ressource, not the filepath
imagecopy($res, $mark, 0, 0, 0, 0, $width, $height);

$watermarktext = "Muggu";
//$font = "../font/century gothic.ttf";
//I copied it to my local test folder
$font = "GOTHIC.TTF";
$fontsize = "15";
//make sure here to use the ressource, not the filepath
$white = imagecolorallocate($res, 255, 255, 255);
//make sure here to use the ressource, not the filepath
imagettftext($res, $fontsize, 0, 20, 10, $white, $font, $watermarktext);

header("Content-type:image/png");
//make sure here to use the ressource, not the filepath
imagepng($res);
//make sure here to use the ressource, not the filepath
imagedestroy($res);

you have to give Public/Absolute Path for Font arial.ttf. Solution is tested in Laravel 5.8.

you can use this solution. here [ $SourceFile , $DestinationFile ] are Absolute Path of local directory. $imgUrl is HTTP based URL. Watermark will be placed at the top of the image.

function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile,$imgUrl) {
  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, 255, 255, 255);
  $font = public_path('fonts/arial.ttf');
  $font_size = 8;
  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);
  return  $imgUrl;
}

Try this.

$imagetobewatermark = "images/muggu.png";
$watermarktext = "Muggu";

list($width, $height) = getimagesize($imagetobewatermark);

$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($imagetobewatermark);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);

$font = "../font/century gothic.ttf";
$font_size = 15;

$white = imagecolorallocate($image_p, 255, 255, 255);
imagettftext($image_p, $font_size, 0, 20, 20, $white, $font, $watermarktext);

header("Content-Type: image/png");
imagepng($image_p, null, 0);
imagedestroy($image);
imagedestroy($image_p);

Try this code, It helps you to provide the watermark for the images.

<?php
  header('Content-type: image/jpeg'); //SET THE FORMATE OF THE IMAGE
  $jpg_image = imagecreatefromjpeg('images.jpg'); //GIVE LINK FOR SPECIFIED IMAGE
  $color = imagecolorallocate($jpg_image, 500, 500, 500); //GIVE COLOR TO WATERMARK TEXT
  $font_location = 'BROADW.TTF'; //WATERMARK FONT
  $text = "http://www.sanwebtutorials.blogspot.in/"; //WATERMARK TEXT
  $x=200; //X CO-ORDINATE
  $y=800; //Y CO-ORDINATE
  $size=21; //SIZE OF TEXT
  imagettftext($jpg_image,$size, 0, $x, $y, $color, $font_location, $text); //PRINT TEXT ON IMAGE
  imagejpeg($jpg_image); //SEND IMAGE TO BROWSER
  imagedestroy($jpg_image); //DESTROY THE MEMORY
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top