Suabahasa
Suabahasa

Reputation: 66

How do I to make watermark image with PHP and GD like envato photodune preview image?

I would like to create watermark with PHP and GD library.

I would like to do repeating the watermark logo with auto margin(space) between each repeated logo, also zigzag position.

Is it possible to create dash line watermark that connect each watermark logo?

The result would be like this:


I have finished the zigzag by using loop function and odd even clause.

/*
             * 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']);
                }
            }

now I stuck at how to create dashed line that have diagonal position that connect to each other logo.

I have a hint from http://php.net/manual/en/function.imagedashedline.php but I don't how to use and combine it with my previous code that generate zigzag logo

Upvotes: 2

Views: 658

Answers (1)

timclutton
timclutton

Reputation: 13004

Edit

It turns out that PHP/GD actually has a function - imagesettile() - specifically to handle this situation.

I've modified my original answer to account for this:

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

Input:

enter image description here

Result:

enter image description here

Upvotes: 2

Related Questions