Add custom text to image in PHP with Example

Add custom text to image in PHP with Example

In this post, I will let you know how to add custom text while uploading image in PHP using GD Library.

By adding text over the image, you declare copyrights or define title of the image.

Here in this example, we will create a new image from given file or url using imagecreatefromjpeg method.

 	  $im = imagecreatefromjpeg($file_name);

Once image has been created then you can allocate a color in following way :

      $backgroundColor = imagecolorallocate ($im, 255, 255, 255);

Now using imagestring method we will draw a string horizontally.

      imagestring ($im, $font, $x, $y, $string, $textColor);

imagejpeg method is used to create jpeg file from the given image.

      $a=imagejpeg($im);
Complete code for adding custom text to image

  1. <?php
  2. if(isset($_POST['submit'])){
  3. $file_name = $_FILES['image']['name'];
  4. $file_size = $_FILES['image']['size'];
  5. $file_tmp = $_FILES['image']['tmp_name'];
  6. $file_type = $_FILES['image']['type'];
  7. move_uploaded_file($file_tmp,$file_name);
  8. header ("Content-type: image/jpeg");
  9. $string = $_POST['caption'];
  10. $font = 12;
  11. $width = imagefontwidth($font) * strlen($string) ;
  12. $height = imagefontheight($font) ;
  13. $im = imagecreatefromjpeg($file_name);
  14. $x = imagesx($im) - $width ;
  15. $y = imagesy($im) - $height;
  16. $backgroundColor = imagecolorallocate ($im, 255, 255, 255);
  17. $textColor = imagecolorallocate ($im, 0, 0,0);
  18. imagestring ($im, $font, $x, $y, $string, $textColor);
  19. $a=imagejpeg($im);
  20. }
  21. ?>
  22. <!DOCTYPE html>
  23. <html>
  24. <head>
  25. <title>PHP : Add Custom Text to Image</title>
  26. </head>
  27. <body>
  28. <form enctype="multipart/form-data" method="POST" action="">
  29.     <input type="file" name="image">
  30.     <input type="text" name="caption">
  31.     <input type="submit" name="submit">
  32. </form>
  33. </body>
  34. </html>

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort kurtköy escort kartal escort pendik escort ataşehir escort
şişli escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort