After the two posts on how to draw a string into an image with php and PHP: How to draw a string on a transparent/translucent png the time to create a more complex script, to apply a watermark to an image, has come.

A watermark is normally used to sign an image with the author of the image name or with a site name to show the site where the image was published. (Naturally you can do some other uses of a watermark)

The following script is based on the two previous posts, so to get more info you can give a look to them.

And, here you are the code:



    // reading parameters

    $text1          = $argv[1];

    $text2          = $argv[2];

    $angle          = $argv[3];

    $imagetochange  = $argv[4];

    $c1             = $argv[5];

    $c2             = $argv[6];

    $c3             = $argv[7];


    $text           = $argv[1]."\n".$argv[2];


    // Create the image

    $image = imagecreatetruecolor(800, 600);

    imagealphablending($image, false);


    //Create alpha channel for transparent layer



    //Create alpha channel for transparent layer

    $stringcolor=imagecolorallocatealpha($image, $c1, $c2, $c3, 80);


    //Create overlapping 100×50 transparent layer

    imagefilledrectangle($image, 0, 0, 800, 600,$col);


    //Continue to keep layers transparent




    // Replace path by your own font path

    $font = '/Library/Fonts/Apple Chancery.ttf';


    // Add the text

    imagettftext($image, 48, $angle, 200, 320, $stringcolor, $font, $text);


    //Keep transparent when saving



    //Save & output

    imagepng($image, "simpleimage.png", 1);


    $stamp = imagecreatefrompng('simpleimage.png');

    $im = imagecreatefromjpeg($imagetochange);


    // Set the margins for the stamp and get the height/width of the stamp image

    $marge_right = 10;

    $marge_bottom = 10;

    $sx = imagesx($stamp);

    $sy = imagesy($stamp);


    // Copy the stamp image onto our photo using the margin offsets and the photo

    // width to calculate positioning of the stamp.

    imagecopy($im, $stamp, imagesx($im) – $sx – $marge_right, imagesy($im) – $sy – $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));


    // Output and free memory

    header('Content-type: image/png');

    imagepng($im, 'watermarked_'.$imagetochange);




the result file will be named watermarked_<file>.

The following image is the one I want to modify


I simply run the script issueing the following command:

./createimageWithWatermark.php gigi bello 40 heliodore.jpg 255 10 10

and the result is the following