gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:33:55 27 4
gpt4 key购买 nike

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

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

下面是代码:

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

如果我错了请告诉我。

谢谢。

最佳答案

我可以直接看到的一个问题是 $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。更正此问题,它应该会起作用。

关于php - 在 PHP 中使用文本为图像添加水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21233255/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com