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

时间:2014-01-20 11:48:26

标签: php watermark

我有一张图片,我正在尝试使用文字为图像添加水印。

我已完成代码部分,但没有使用水印文本查看图像。

以下是代码:

$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);

告诉我如果我错了。

谢谢。

5 个答案:

答案 0 :(得分:4)

我可以立即看到的一个问题是$imagetobewatermark变量以字符串形式开始,然后变为新的空白图像对象(不是现有图像),并在您随后创建mark时图像对象,它不起作用,因为$imagetobewatermark不再是字符串。

尝试:

$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);

编辑: 我没有注意到文字变量$wartermarktext中的拼写错误,该错误应为$watermarktext。 纠正这个,它应该工作。

答案 1 :(得分:1)

你有一些拼写错误,并没有使用该资源。在这里纠正:

$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);

答案 2 :(得分:1)

您必须为arial.ttf提供公共/绝对路径。 解决方案已在Laravel 5.8中进行了测试。

您可以使用此解决方案。这里[$ SourceFile,$ DestinationFile]是本地目录的绝对路径。 $ imgUrl是基于HTTP的URL。水印将放置在图像的顶部。

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;
}

答案 3 :(得分:0)

试试这个。

$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);

答案 4 :(得分:0)

尝试此代码,它可以帮助您为图像提供水印。查看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
?>