RSS

Adding Watermarks To Images Using PHP

Tue, Nov 4, 2008

PHP

In this tutorial I’m going to show you how to add watermarks to images. The advantages to this script is that it will not effect the original image.

So this is the first bit of the code…

What this does is defines the functions and retrives the images.

$stamp = imagecreatefrompng(’stamp.png’);
$im = imagecreatefromjpeg(’photo.jpeg’);

Next…What this does is sets the size and the margins for the watermark.

$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

Now we need to calculate the position of the watermark

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

Now we need to tell the brower to display an image. And display the image we have created. Then destroy the image to reduce the server load.

header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

And thats everything.

Full Source

<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
 
// 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);
imagedestroy($im);
?>
Rating: 8.5/10 (6 votes cast)
Share and Enjoy:
  • Digg
  • Google
  • Technorati
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Furl
  • Live
  • Reddit
  • Spurl

  • Subscribe
    • XML
    • Google Reader or Homepage
    • Add to My Yahoo!
    • Subscribe with Bloglines
    • Subscribe in NewsGator Online
    • BittyBrowser
    • Add to My AOL
    • Convert RSS to PDF
    • Subscribe in Rojo
    • Subscribe in FeedLounge
    • Subscribe with Pluck RSS reader
    • Solosub
    • MultiRSS
    • R|Mail
    • Rss fwd
    • Blogarithm
    • Eskobo
    • gritwire
    • BotABlog
    • Simpify!
    • Add to Technorati Favorites!
    • Add to netvibes
    • Add this site to your Protopage
    • Subscribe in NewsAlloy
    • Subscribe in myEarthlink
    • Add to your phone
    • Get RSS Buttons

, , , , ,



This post was written by:

admin - who has written 41 posts on Unreal Media.


Contact the author

1 Comments For This Post

  1. Dbz Says:

    This is a usefull post, A demo would have been nice!
    Havnt used php before regarding imagary
    Thanks!

    Rating: 0.0/5 (0 votes cast)

Leave a Reply