Inzamam Idrees
Inzamam Idrees

Reputation: 1981

How do I add the watermark image with different positions in PHP Laravel?

Hi I want to add watermark image with different positions like

top-left,
top-right,
bottom-left,
bottom-right,
center.

Admin select one of the above position in the setting tab. I am using the Orakuploader plugin and in that plugin the watermark occur is in the center of the uploaded image.

Here is the code of watermark image for center position:

function addWatermark($watermark, $imageDirectory, $imageName, $x = 0, $y = 0)
{
    if(file_exists($watermark))
    {
        $marge_right  = 0;
        $marge_bottom = 0;

        $stamp = imagecreatefrompng($watermark);

        $image_extension = @end(explode(".", $imageName));
        switch($image_extension)
        {
            case "jpg":
                $im = imagecreatefromjpeg("$imageDirectory/$imageName");
                break;
            case "jpeg":
                $im = imagecreatefromjpeg("$imageDirectory/$imageName");
                break;
            case "png":
                $im = imagecreatefrompng("$imageDirectory/$imageName");
                break;
        }

        $imageSize = getimagesize("$imageDirectory/$imageName");
        $watermark_o_width = imagesx($stamp);
        $watermark_o_height = imagesy($stamp);

        $newWatermarkWidth = $imageSize[0];
        $newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;


        if((int)$x <= 0)
            $x = $imageSize[0]/2 - $newWatermarkWidth/2;
        if((int)$y <= 0)
            $y = $imageSize[1]/2 - $newWatermarkHeight/2;

        imagecopyresized($im, $stamp, $x, $y, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($stamp), imagesy($stamp));

        switch($image_extension)
        {
            case "jpg":
                header('Content-type: image/jpeg');
                imagejpeg($im, "$imageDirectory/$imageName", 100);
                break;
            case "jpeg":
                header('Content-type: image/jpeg');
                imagejpeg($im, "$imageDirectory/$imageName", 100);
                break;
            case "png":
                header('Content-type: image/png');
                imagepng($im, "$imageDirectory/$imageName");
                break;
        }
    }
}

How to add watermark with rest of the positions like (top-left, top-right, bottom-left and bottom-right)?

Much appreciated for your help. Thanks

Upvotes: 3

Views: 3064

Answers (2)

Tayyab Roy
Tayyab Roy

Reputation: 513

Hey Dude try by replace this code with imagecopyresized function. I hope it can work for you. Thanks.

$sx = imagesx($stamp);
$sy = imagesy($stamp);

// top-left
imagecopy($im, $stamp, -45, -5, 0, 0, imagesx($stamp), imagesy($stamp));

// top-right
imagecopy($im, $stamp, imagesx($im) - $sx + 45, -5, 0, 0, imagesx($stamp), imagesy($stamp));

// bottom-left
imagecopy($im, $stamp, -45, imagesy($im) - $sy + 5, 0, 0, imagesx($stamp), imagesy($stamp));

// bottom-right
imagecopy($im, $stamp, imagesx($im) - $sx + 45, imagesy($im) - $sy + 5, 0, 0, imagesx($stamp), imagesy($stamp));

// center
imagecopy($im, $stamp, (imagesx($im) - $sx)/2, (imagesy($im) - $sy)/2, 0, 0, imagesx($stamp), imagesy($stamp));

Upvotes: 4

Afzal Hossain
Afzal Hossain

Reputation: 3338

I suggest that you use Intervention Image library for this purpose. It has a robust and expressive API

For watermarking with different positioning, you can refer to this page

http://image.intervention.io/api/insert

You should end up with something like this

$img = Image::make('public/foo.jpg');
$watermark = Image::make('public/watermark.png');
$img->insert('public/watermark.png', 'bottom-right', 10, 10);

Upvotes: 1

Related Questions