Add custom text to image in PHP with Example

Add custom text to image in PHP with Example

In this post we will give you information about Add custom text to image in PHP with Example. Hear we will give you detail about Add custom text to image in PHP with ExampleAnd how to use it also give you demo for it if it is necessary.

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.


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,,,);
  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>


      $file_name = $_FILES['image']['name'];
      $file_size = $_FILES['image']['size'];
      $file_tmp = $_FILES['image']['tmp_name'];
      $file_type = $_FILES['image']['type'];
      header ("Content-type: image/jpeg");
      $string = $_POST['caption'];
      $font = 12;
      $width = imagefontwidth($font) * strlen($string) ;
      $height = imagefontheight($font) ;
      $im = imagecreatefromjpeg($file_name);
      $x = imagesx($im) - $width ;
      $y = imagesy($im) - $height;
      $backgroundColor = imagecolorallocate ($im, 255, 255, 255);
      $textColor = imagecolorallocate ($im, 0, 0,0);
      imagestring ($im, $font, $x, $y, $string, $textColor);
<!DOCTYPE html>
      <title>PHP : Add Custom Text to Image</title>

<form enctype="multipart/form-data" method="POST" action="">
	<input type="file" name="image"> 
	<input type="text" name="caption">
	<input type="submit" name="submit">

Show Demo

Hope this code and post will helped you for implement Add custom text to image in PHP with Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  13  =  22

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US