PHP - Add Watermark To Image Using GD Library

Submitted by: 
Visitors have accessed this post 370 times.

In this tutorial we will create a Add Watermark To Image Using GD Library using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And this is the link for the jquery that i used in this tutorial

Lastly, this is the link for the bootstrap that i used for the layout design

Setting Up GD Library

First, we are going to enable GD Library in PHP.

1. Open localhost server folder XAMPP, etc and locate php.ini.
2. Open php.ini and enable gd library by removing the semicolon in the line.
3. Save changes and Restart Server.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Add Watermark To Image Using GD Library</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-2"></div>
  19. <div class="col-md-8">
  20. <form method="POST" enctype="multipart/form-data" action="save.php">
  21. <center>
  22. <div id = "preview" style = "width:200px; height:200px; border:1px solid #000;">
  23. <center id = "lbl">[Photo]</center>
  24. </div>
  25. </center>
  26. <input type = "file" id = "file" name = "img" style="margin-left:22%;"/>
  27. <br />
  28. <div id="hide_form">
  29. <label>Enter a Text</label>:
  30. <input type="text" name="img_name" required="required"/>
  31. <br />
  32. <br />
  33. <center><button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-upload"></span> Save</button></center>
  34. </div>
  35. </form>
  36. </div>
  37. </div>
  38. </body>
  39. <script src="js/jquery-3.2.1.min.js"></script>
  40. <script src="js/script.js"></script>
  41. </html>

Creating PHP Script

This code contains the main function of the application. This code will get the image file then will insert some text after saving. To do that just copy and write this block of codes inside the text editor, then save it as save.php.

  1. <?php
  2. if(ISSET($_POST['save'])){
  3. if($_FILES['img']['name'] != ""){
  4. $file = explode(".", $_FILES['img']['name']);
  5. $end = end($file);
  6. $text = $_POST['img_name'];
  7. if($file[1] == "png"){
  8. $file_name = $file[0].'.'.$file[1];
  9. $tmp_file = $_FILES['img']['tmp_name'];
  10. $location = "upload/".$file_name;
  11. $new_location = addslashes($location);
  12. $image = imagecreatefrompng($tmp_file);
  13. $color = imagecolorallocate($image, 0, 0, 0);
  14. $font = 'Loveland Personal Use.ttf';
  15. imagettftext($image, 20, 0, 20, 30, $color, $font, $text );
  16. imagepng($image, $location);
  17. imagedestroy($image);
  18. echo "<script>alert('Image Created!')</script>";
  19. echo "<script>window.location = 'index.php'</script>";
  22. }else{
  23. echo "<script>alert('Only png files allowed!')</script>";
  24. echo "<script>window.location = 'index.php'</script>";
  25. }
  26. }
  27. }
  29. ?>

Creating jQuery Script

This is where the function that display the image.This code handles the image preview everytime the user upload some images. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. $(document).ready(function(){
  2. $('#hide_form').hide();
  3. var pic = $('<img id = "image" width = "100%" height = "100%"/>');
  4. var lbl = $('<center id = "lbl">[Photo]</center>');
  5. $("#file").change(function(){
  6. $("#lbl").remove();
  7. var files = !!this.files ? this.files : [];
  8. if(!files.length || !window.FileReader){
  9. $("#image").remove();
  10. lbl.appendTo("#preview");
  11. $('#hide_form').hide();
  12. }
  13. if(/^image/.test(files[0].type)){
  14. var reader = new FileReader();
  15. reader.readAsDataURL(files[0]);
  16. reader.onloadend = function(){
  17. pic.appendTo("#preview");
  18. $("#image").attr("src", this.result);
  19. $('#hide_form').show();
  20. }
  21. }
  22. });
  23. });

There you have it we successfully created Add Watermark To Image Using GD Library using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Thank you for tutorial.

Have you considered adding way to choose between different fonts (like Google)? Would certainly make the script more useful:)


I'm glad that I could help...

Thanks for the suggestion I try to create a tutorial for this matter soon..

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.