如何使用PHP在所请求的图像中重复水印? [英] How can I repeat a watermark throughout a requested image using PHP?

查看:133
本文介绍了如何使用PHP在所请求的图像中重复水印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为目录中的所有图像添加水印,例如www.example.com/private。这些图像中的一些具有大量分辨率,而其他图像相对正常,因此目前我的水印对于较小的图像工作正常。即使以水印为中心,我仍然会让较大图像的理想部分容易被裁剪。

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.

所以我的问题是如何编写php脚本到在整个图像中重复水印,包括垂直和水平?关于后端开发我真的不太了解,除了我知道它需要提供足够的水印保护,所以我一直在谷歌四处看看,只能找到这个 http://www.regardadesign.co.uk/blog/post/php-image-manipulation/15 ,这是行不通的。

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.

到目前为止,我已将以下.htaccess文件放入/ private目录:

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

这是watermark.php文件中的脚本:

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();
?>


推荐答案

这种情况正在发生,因为你只插入一次水印。如果你重复沿着图像区域的水印它将起到作用。

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.

替换你的imagecopy线:

Replace your imagecopy line for this:

$img_paste_x = 0;
while($img_paste_x < $w){
    $img_paste_y = 0;
    while($img_paste_y < $h){
        imagecopy($image, $watermark, $img_paste_x, $img_paste_y, 0, 0, $ww, $wh);
        $img_paste_y += $wh;
    }
    $img_paste_x += $ww;
}

这篇关于如何使用PHP在所请求的图像中重复水印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆