简体   繁体   English

在PHP中添加水印

[英]Adding watermark in PHP

I have made a bespoke CMS for a customer.... 我已经为客户量身定制了CMS。

In the admin section, he adds photos for profiles of workers.... 在管理部分,他添加了用于工作者简介的照片。

I need to add watermarks to all photos that admin uploads.... 我需要为管理员上传的所有照片添加水印。

It can't be done as he uploads them (or, at least what I understand is that the image must be on the server BEFORE apply php/GD functions to it ?)... So it needs to be applied to images on their way out of the database... 当他上传它们时,无法完成此操作(或者,至少我了解的是,该图像必须在服务器上应用php / GD函数之前?)...因此需要将其应用于其上的图像出数据库的方式...

foreach($get_all_photos_profile_array AS $get_photos_profile_each)
{
    echo "<img class=\"profile_photo\"src=\"$get_photos_profile_each\" alt=\"profile image\" width=\"270px\" height=\"405px\"/>";
}

This is what it would be without adding watermark....outputting all images for that particular profile.... 这就是不添加水印的情况。...输出该特定配置文件的所有图像...。

I have tried replacing the stuff inside the loop with: 我试图用以下方法替换循环中的内容:

$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpg');

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

Where stamp.png is the watermark and photo.jpg is the ... you get it. 其中,stamp.png是水印,而photo.jpg是...。

I have spent sometime trying different things, but no luck... 我花了一段时间尝试不同的事情,但没有运气...

How do I place 'imagepng($im)' in the echo statement so that it does it for each photo? 如何在回显语句中放置“ imagepng($ im)”,以便对每张照片进行处理?

Also, is the header really important? 另外,标题真的很重要吗? struggling to rearange the code so that it is sent at the start of the file... 努力重新排列代码,以便在文件的开头发送代码...

I think what you are aiming for is: 我认为您的目标是:

<img src="addWatermark.php?image=photo.jpg">

Then put all your watermark code in addWatermark.php and change the file loading to: 然后将所有水印代码放入addWatermark.php中,并将文件加载更改为:

$im = imagecreatefromjpeg($_GET['image']);

The header is indeed important. 标头确实很重要。

As it's a CMS, ideally you only want to run the watermark code once on each image when it gets uploaded - and then save that new watermarked image to a separate file:- 由于它是CMS,理想情况下,您只希望在每个图像上载水印代码后将其运行一次-然后将该新水印图像保存到单独的文件中:-

$output_filename="path/to/savedfile.png";
imagepng($im,$output_filename);
imagedestroy($im);

Then you'll need to store the watermarked filename somewhere, and whenever you want to display the watermarked version of that image, simply output some html referencing the image you just saved:- 然后,您需要将带水印的文件名存储在某个位置,每当您想要显示该图像的带水印的版本时,只需输出一些引用刚刚保存的图像的html:

echo "<img class=\"profile_photo\"src=\"".$output_filename."\" alt=\"profile image\" width=\"270px\" height=\"405px\"/>";

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

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