如何使用 PHP 在请求的图像中重复水印?
我正在尝试向目录中的所有图像添加水印,例如 www.example.com/private。其中一些图像具有很大的分辨率,而其他图像则相对正常,因此目前我的水印对于较小的图像效果很好。即使将水印居中,我仍然会留下较大图像的理想部分,容易被裁剪。
所以我的问题是我将如何编写一个 php 脚本来在整个图像中垂直和水平重复水印?我对后端开发不太了解,除了我知道它需要提供足够的水印保护之外,所以我一直在谷歌上四处寻找,只能找到这个 http://www.regardadesign.co.uk/blog/post/php-image-manipulation/15 , 哪个不起作用。
到目前为止,我已将以下 .htaccess 文件放入 /private 目录中:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(gif|jpeg|jpg|png)$ /admin/watermark.php [QSA,NC]
</ifModule>"
这是 watermark.php 文件中的脚本:
<?php
ini_set('memory_limit','200M');
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$image = imagecreatefromstring(file_get_contents($path));
$w = imagesx($image);
$h = imagesy($image);
$watermark = imagecreatefrompng('watermark.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);
imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
exit();
?>
I'm trying to add watermark to all the images in a directory, let's say www.example.com/private. Some of these images have massive resolutions, while others are relatively normal so at the moment my watermark is working fine for the smaller images. Even by centering the watermark, I'm still leaving desirable sections of the bigger images vulnerable to cropping.
So my question is how would I go about writing a php script to repeat the watermark throughout the image, both vertically and horizontally? I don't really know enough about back-end development except that I know it's required to provide adequate watermarking protection, so I've been looking around on google and could only find this http://www.regardadesign.co.uk/blog/post/php-image-manipulation/15, which doesn't work.
So far I've placed the following .htaccess file into the /private directory:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(gif|jpeg|jpg|png)$ /admin/watermark.php [QSA,NC]
</ifModule>"
And this is the script in watermark.php file:
<?php
ini_set('memory_limit','200M');
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$image = imagecreatefromstring(file_get_contents($path));
$w = imagesx($image);
$h = imagesy($image);
$watermark = imagecreatefrompng('watermark.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);
imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
exit();
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为您只插入水印一次。如果你沿着图像区域重复水印,它就会起作用。
替换你的 imagecopy 行:
This is happening because you are inserting the watermark only once. If you repeat the watermark along the image area it will do the trick.
Replace your imagecopy line for this: