在 WP 内的图像中添加水印图章

Adding watermark stamp to image inside of WP

我有一个与 wordpress 的 AJAX 通话,我需要在其中为图像添加水印并将其发送到电子邮件中。我正在关注 this instructions(以及 stackoveflow 上的许多其他人)以达到预期的结果,但无济于事,我得到的只是黑色的小图像。

这是我的代码

    $im = imagecreatefromjpeg("https://s3-eu-west-1.amazonaws.com/pitchprint.io/previews/" . $project_id . "_1.jpg");
    $stamp = imagecreatefrompng(get_site_url() . "/wp-content/themes/porto-child/img/watermark.png");
    
    $marge_right = 10;
    $marge_bottom = 10;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);

    imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

    // Output and free memory
    header('Content-type: image/png');
    imagepng($im);
    imagedestroy($im);

图像路径正确,它们在浏览器中加载正常,但由于某种原因我无法使这段代码工作,非常感谢任何帮助。

$string = "My watermark text";
$color = imagecolorallocate($image, 255, 255, 255);
$fontSize = 3;
$x = 0;
$y = 0;
imagestring($image, $fontSize, $x, $y, $string, $color);

如果你要在图像上粘贴图像,请使用 imagecopymerge

代码也能运行,我已经在本地机器上测试过了。

  1. 使用静态图像检查代码 URL,确保它适用于静态图像。
  2. 将您的图片 URL 放入标签中以确保您的图片正常工作

我认为你的图片有问题。


编辑:您在云端的图像不是JPEG,而是PNG图像格式,它只有 jpg 扩展名。 所以,只需更改此代码

$im = imagecreatefromjpeg("https://s3-eu-west-1.amazonaws.com/pitchprint.io/previews/" . $project_id . "_1.jpg");

$im = imagecreatefrompng("https://s3-eu-west-1.amazonaws.com/pitchprint.io/previews/" . $project_id . "_1.jpg");