php - 如何使用 PHP 和 GD 制作水印图像,如 envato photodune 预览图像?

标签 php gd image-manipulation watermark

我想使用 PHP 和 GD 库创建水印。

我想重复水印 Logo ,并在每个重复 Logo 之间设置自动边距(空格),也是锯齿形位置。

是否可以创建连接每个水印 Logo 的虚线水印

结果会是这样的:


我使用循环函数和奇偶子句完成了之字形。

/*
             * utils
                $widthWatermark = imagesx($logo);
                $heightWatermark = imagesy($logo);
                $widthPhoto = imagesx($output);
                $heightPhoto = imagesy($output);
             */

            // $xLogoPosition = 0;
            // $yLogoPosition = 0;


            $__xRepeat = ceil($widthPhoto / $widthWatermark);
            $__yRepeat = ceil($heightPhoto / $heightWatermark);
            $margin = (int)self::$option['margin'];

            for ($i = 0; $i <= $__xRepeat; $i++) {

                if ($i % 2 === 0) { 
                    $pre_ii = 1;
                } else {
                    $pre_ii = 0;
                }

                for ($ii = 0; $ii <= $__yRepeat; $ii++) {

                    $ii_zero = $ii - $pre_ii;

                    if ($ii_zero % 2 === 0) {       
                        $y_xindent = $widthWatermark;                        
                    }else{
                        $y_xindent = 0;  
                    }

                    $this->imagecopymerge_alpha($output, $logo, ($xLogoPosition + $widthWatermark * $i + $y_xindent), ($yLogoPosition + $widthWatermark * $ii), 0, 0, ImageSX($logo), ImageSY($logo), self::$option['opacity']);
                }
            }

现在我坚持如何创建具有连接到彼此 Logo 的对角位置的虚线。

我收到 http://php.net/manual/en/function.imagedashedline.php 的提示 但我不知道如何使用它并将其与我之前生成之字形 Logo 的代码结合起来

最佳答案

编辑

原来PHP/GD居然有一个函数——imagesettile() - 专门处理这种情况。

我修改了原来的答案以解决此问题:

<?php
// create php image of a 'dashed cross'.
$crossW = $crossH = 200;
$cross  = imagecreatetruecolor($crossW, $crossH);
imagefill($cross, 0, 0, 0x7fff00ff); // transparent magenta.
imagesetthickness($cross, 1);
imagesetstyle(
    $cross,
    array_merge(
        array_fill(0, 3, 0x7fff00ff), // transparent magenta.
        array_fill(0, 8, 0x60ffffff) // partially-transparent white.
    )
);
imageline($cross, 0, 0, $crossW, $crossH, IMG_COLOR_STYLED);
imageline($cross, $crossW, 0, 0, $crossH, IMG_COLOR_STYLED);

$imageFile = 'wm2.jpg';

// open the image file to be watermarked and store its height and width.
$image = imagecreatefromjpeg($imageFile);
$imWidth = imagesx($image);
$imHeight = imagesy($image);

// apply the cross pattern as a tile to the image file.
imagesettile($image, $cross);
imagefilledrectangle($image, 0, 0, $imWidth, $imHeight, IMG_COLOR_TILED);

header('Content-type: image/png');
imagepng($image);
imagedestroy($cross);
imagedestroy($image);
exit;

输入:

enter image description here

结果:

enter image description here

关于php - 如何使用 PHP 和 GD 制作水印图像,如 envato photodune 预览图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32005595/

相关文章:

php - 使用 PHP 检测图像中的主要颜色

python - cv2.approxPolyDP() , cv2.arcLength() 这些是如何工作的

php - 如何根据特定字段的值更改行的颜色?

php - Symfony2 : Spaces in image when using GD

image - 将阿拉伯文本导出为图像

php - 使用 PHP GD 库将 webp 转换为 jpeg

php - 最常用的 PHP 图像处理类有哪些?

php - 如何在 laravel Eloquent 中按相关表的列排序?

php - 从 DATETIME 列 MySQL/PHP 中检索时间间隔的数据

php - MYSQL : select count with union three table