Watermark Outside The image in PHP?

58 views Asked by At

I would like to upload an image, and then automatically add a watermark (text or image), but not over the image, I want it outside the image, How can I do this with PHP? and at the same time if the image is too small adapt the watermark to always fit properly

Please see image below to see what I mean and the desired results

Original Image: https://i.stack.imgur.com/SSFEL.png

Uploaded Image: https://i.stack.imgur.com/GOIt4.png

Same image but last one with the watermark on it.

1

There are 1 answers

0
Rasclatt On

This is not the most automated in terms of determining text placement (you can do that), but it's a start:

<?php

    function create_cartoon($settings=false)
        {
            $img    =   (isset($settings['img']) && !empty($settings['img']))? $settings['img']:false;
            $txt    =   (isset($settings['text']) && !empty($settings['text']))? $settings['text']:'www.lunarbaboon.com';
            $offset =   (isset($settings['offset']) && !empty($settings['offset']))? $settings['offset']:12;

            if($img == false)
                return;

            header('Content-Type: image/jpeg');

            $mime       =   getimagesize($img);
            $or_w       =   $mime[0];
            $or_h       =   $mime[1];
            $type       =   $mime['mime'];

            switch ($type) {
                case ('image/png') :
                    $im =   imagecreatefrompng($img);
                    break;
                case ('image/gif') :
                    $im =   imagecreatefromgif($img);
                    break;
                default :
                    $im =   imagecreatefromjpeg($img);
            }

            $layer1     =   imagecreatetruecolor($or_w,($or_h+30));
            $bkg1       =   imagecolorallocate($layer1,255,255,255);
            $layer1s    =   getimagesize($layer1);

            imagecopy($layer1,$im,0,0,$layer1s[0],$layer1s[1],$or_w,$or_h);
            imagefilledrectangle($layer1,0,$or_h,$or_w,$or_h+30,$bkg1);
            $text_color =   imagecolorallocate($layer1, 100, 100, 100);
            imagestring($layer1, 8, $offset, $or_h+5, $txt, $text_color);
            imagejpeg($layer1);
            imagedestroy($img);
            imagedestroy($layer1);
        }

     // To use
     create_cartoon(array('img'=>'https://i.stack.imgur.com/SSFEL.png','offset'=>114));      
?>