简体   繁体   中英

Watermark Outside The image in PHP?

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: http://i.stack.imgur.com/SSFEL.png

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

Same image but last one with the watermark on it.

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'=>'http://i.stack.imgur.com/SSFEL.png','offset'=>114));      
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM