简体   繁体   English

在 PHP 中使用文本为图像添加水印

[英]Watermark an Image using Text in PHP

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.谢谢你。

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.我可以立即看到的一个问题是$imagetobewatermark变量以字符串开始,然后变成一个新的空白图像对象(不是现有图像),并且当您随后创建mark图像对象时,它不会起作用,因为$imagetobewatermark不再是字符串。

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 .编辑:我没有注意到你的文本变量$wartermarktext有一个错字,它应该是$watermarktext Correct this and it should work.纠正这一点,它应该可以工作。

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.你必须为字体 arial.ttf 提供公共/绝对路径。 Solution is tested in Laravel 5.8.解决方案在 Laravel 5.8 中进行了测试。

you can use this solution.您可以使用此解决方案。 here [ $SourceFile , $DestinationFile ] are Absolute Path of local directory.这里 [ $SourceFile , $DestinationFile ] 是本地目录的绝对路径。 $imgUrl is HTTP based URL. $imgUrl 是基于 HTTP 的 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.试试这个代码,它可以帮助您为图像提供水印。 Check Example here 在此处检查示例

<?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
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM