doufu4333
2011-09-07 11:27
浏览 139
已采纳

如何使用PHP在所请求的图像中重复水印?

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

图片转代码服务由CSDN问答提供 功能建议

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

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

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

 &lt  ; IfModule mod_rewrite.c&gt; 
RewriteCond%{REQUEST_FILENAME} -f 
RewriteRule \。(gif | jpeg | jpg | png)$ /admin/watermark.php [QSA,NC] 
&lt; / ifModule&gt;“
   
 
 

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

 &lt;?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($ water) 标记); 
 $ 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(); 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dragon202076 2011-09-11 16:08
    已采纳

    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:

    $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;
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题