ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

Image manipulation with PHP & the GD library, Part 2

(Page 3 out of 3)

Dynamically watermarking images

Although we haven't discussed it yet, PHP's image functions can also be used to edit existing images. In a later part of this article series we'll see how to drastically edit images, but now we're going to see how to add a watermark to an existing image.

There are several different types of watermarks, and some are even invisible, but we're going to go with the most common type, which simply prints a piece of text in the bottom left corner.

As always, start by opening the image:

// Load image
$image = open_image('flower.jpg');
if ($image === false) { die ('Unable to open image'); }

// TODO: add the watermark

header('Content-type: image/jpeg');
imagejpeg($image);
die();

?>

Then we will have to add our watermark to the image with the following code:

// Get identifier for white
$white = imagecolorallocate($image, 255, 255, 255);

// Add text to image
imagestring($image, 3, 5, imagesy($image)-20, 'Image by YourWebsite.com', $white);

(View Live Demo)

In the above example we first use the imagecolorallocate() function to get the right identifier for the color white. Then we use the imagestring() function to add the text to the image.

The imagestring() function isn't the only function to add text to an image, and the more common function for this task is the imagettftext() function, but that requires a separate font file, which makes it more of a hassle to use it. That's why we're using the imagestring() function in this tutorial.

Now that we know how to watermark an image, we can also create an online watermark tool, similar to the resizer and converter tools. The online watermark tool works exactly the same, and makes it possible for anyone to upload their image and set a custom watermark. I'll refrain from posting the full code of the watermark tool, but you can click here to view a live demo or click here to view the full source code.

Conclusion

In this second part of the article series on image manipulation with PHP and the GD library I've shown you how to resize images, build an online image resizing tool, and how to dynamically add a watermark to your image.

In the next part we'll have another look at adding a watermark to your image, and using the imagettftext() function to solve all the problems with our current implementation. You will also learn how to build a CAPTCHA script to block spam bots.

If you have any questions or comments, please leave them in the comments below. Don't forget to join us at PHPit Forums for more PHP talk.

« Previous: Online Thumbnail Creator



5 Responses to “Image manipulation with PHP & the GD library, Part 2”

  1. vish Says:

    Hi
    i want to resample an image to half its original size but there are not properly done
    using the following code so please help me for solving the code :

    0) {
    $row = mysql_fetch_array ($result);
    $percent = 0.5; //ex

    $image_type = $row[”image_type”];
    $image = $row[”image”];
    $image_name = $row[”image_name”];

    header(’Content-type: $image_type’);

    // Get new dimensions
    $width = imagesx($image);
    $height = imagesx($image);

    $new_width = $width * $percent;
    $new_height = $height * $percent;

    // Resample
    $image_p = imagecreatetruecolor($new_width,$new_height);
    $imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    // Output
    header(’Content-type: image/jpeg’);
    imagejpeg($image_p);

    Regards ,
    vish

    }
    ?>

  2. Harry D Says:

    Hi vish,

    while watching your code I found header(’Content-type: $image_type’); and header(’Content-type: image/jpeg’);.
    You can only send one header at once, not two. Just use the last one to make sure everything gets done before you put the resized image on screen.

    Then, if you need to resize gif and png you need another header, so implement a switch case i.e. to get the type and use the right functions for these types.

    best regards,

    Harald Doderer
    Director Emedian Ltd.
    www.emedian.net

  3. Ramesh N Sharma Says:

    short and useful tutorial

  4. Jawad Shuaib Says:

    Thank you so much for this tutorial. This is exactly what I was looking for :) Thank you!

  5. intekhab alam Says:

    it help me to solve my problem so thanyou very much.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Resizing Images
  2. Online Thumbnail Creator
  3. Watermarking Images & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file