如何使用GD和PHP重复水印图像?

时间:2012-02-13 23:10:03

标签: php gd watermark

我创建了一个脚本,使用PHP在现有图像上添加水印。这一切都很好。我可以将它放在左上角,左下角,右上角,右下角和居中。如果我愿意的话,我无法弄清楚如何重复水印。

我想像这张图片一样重复水印:

enter image description here

代码:

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
    // creating a cut resource 
    $cut = imagecreatetruecolor($src_w, $src_h); 

    // copying relevant section from background to the cut resource 
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 

    // copying relevant section from watermark to the cut resource 
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);

    // insert cut resource to destination image 
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
} 


$imagesource    = $image['file_path'];
$watermarkPath  = $settings['watermark'];
$filetype       = substr($imagesource,strlen($imagesource)-4,4);
$filetype       = strtolower($filetype);
$watermarkType  = substr($watermarkPath,strlen($watermarkPath)-4,4);
$watermarkType  = strtolower($watermarkType);

// Let's pretend that $watermark and $image are now GD resources.

$imagewidth         = imagesx($image);
$imageheight        = imagesy($image);  
$watermarkwidth     = imagesx($watermark);
$watermarkheight    = imagesy($watermark);

switch ($settings['watermark_location'])
{
    case "tl": //Top Left
        $startwidth     = 20;
        $startheight    = 20;
        break;
    case "bl": //Bottom Left
        $startwidth     = 20;
        $startheight    = (($imageheight - $watermarkheight) - 20);
        break;
    case "tr": //Top Right
        $startwidth     = (($imagewidth - $watermarkwidth) - 20);
        $startheight    = 20;
        break;
    case "br": //Bottom Right
        $startwidth     = (($imagewidth - $watermarkwidth) - 20);
        $startheight    = (($imageheight - $watermarkheight) - 20);
        break;
    case "middle": //Middle/center
        $startwidth     = (($imagewidth - $watermarkwidth) / 2);
        $startheight    = (($imageheight - $watermarkheight) / 2);
        break;
    case "repeat":
        // not sure what to do here
        break;
    default:
        $startwidth     = (($imagewidth - $watermarkwidth) / 2);
        $startheight    = (($imageheight - $watermarkheight) / 2);
}

imagecopymerge_alpha($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight,$settings['watermark_opacity']);
imagejpeg($image,NULL,90);
imagedestroy($image);
imagedestroy($watermark);               

2 个答案:

答案 0 :(得分:2)

我不完全知道你的脚本是如何工作的,但是你不能只是以固定的间隔重复添加水印,直到覆盖整个图像宽度为止?

答案 1 :(得分:1)

我认为imagesettile功能可以提供帮助:

http://php.net/manual/en/function.imagesettile.php

查看该页面上的示例。