【问题标题】:Watermark an Image using Text in PHP在 PHP 中使用文本为图像加水印
【发布时间】:2014-01-20 11:48:25
【问题描述】:

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

我已经完成了代码部分,但没有看到带有水印文本的图像。

下面是代码:

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

如果我错了告诉我。

谢谢。

【问题讨论】:

  • 你为什么不使用像intervention/image这样的包?
  • 啊,打错字了!我更新了我的答案...

标签: php watermark


【解决方案1】:

我可以立即看到的一个问题是 $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。 纠正这个,它应该可以工作。

【讨论】:

  • 我已经在本地办公桌上尝试了上述代码,但没有看到任何输出。我已将图像保存到本地桌面,但没有看到水印。你能帮帮我吗?
  • 是的。现在我可以看到变化了。谢谢拉德。
  • 我也没有得到任何输出...有人可以帮忙吗? :(
  • @ДимитърКлатуров - 最好在新问题中发布您的代码 =)
【解决方案2】:

您有一些拼写错误并且没有使用资源。在此更正:

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

【讨论】:

  • 您也可以测试不同的字体大小,为放置水印的图像边框留出更多边距并尝试另一种颜色。只是为了调试。在本地,它与上面的代码配合得很好。
  • 我已经在本地办公桌上尝试了上述代码,但没有看到任何输出。我已将图像保存到本地桌面,但没有看到水印。我也尝试过不同的字体大小。你能帮帮我吗?
  • 您是否将 TTF 文件复制到正确的位置?您的图像(尺寸)有多大?也许您将文本放在图像之外?
【解决方案3】:

您必须为字体 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;
}

【讨论】:

    【解决方案4】:

    试试这个。

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

    【讨论】:

    • 我已经在本地办公桌上尝试了上述代码,但没有看到任何输出。我已将图像保存到本地桌面,但没有保存图像。你能帮帮我吗?
    【解决方案5】:

    试试这个代码,它可以帮助你为图片提供水印。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 2017-07-24
      • 2018-12-02
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多