Write a program to display text in a image in php

Adding custom text over image or pictures is a good way to declare copyrights or give a simple title information to that graphical image or picture without editing the image itself. PHP has made it very easy to add custom text over image or picture by using some basic functions from PHP GD.

Complete code for adding text over image

Customizing the text for your usage:

  • Change the $imgPath to image path you want to add text over it.
  • In “$color = imagecolorallocate[$image, 255 , 255, 255]” change the color code to color that suites with the image. for example, if the image background is white then change [255, 255, 255] to [0, 0, 0] which is code for black color.
  • $string is the text will be added over the image.
  • $fontSize is the font size number.
  • $x is the text margin from left.
  • $y is the text margin from top

Resources:

  • imagecreatefromjpeg [Create a new image from file or URL and returns an image identifier representing the image obtained from the given filename].
  • imagecolorallocate [Allocate a color for an image and returns a color identifier representing the color composed of the given RGB components].
  • imagestring [Draw a string horizontally]
  • imagejpeg [Output image to browser or file]

The image used in this article is taken from //vectorialpx.net

As always, any comments or suggestion would be really appreciated.

The post Write Text Over Image or Picture with PHP appeared first on Recent Solutions.

This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Last Updated : Apr 15, 2022

IN - PHP PHP GD

In this tutorial we will show you how to add text to images using PHP GD library.The text can be any text like your name, web address, email or your number.You may also like add watermark to an image using PHP.

Join With 27,000+ Members In Our Google Group & Get Latest Tutorials

Get our latest tutorials, How-To guides on web development every day right into your inbox.

To Add Text To Image It Takes Only One Step:-

  1. Make a PHP file and define scripting

Step 1. Make a PHP file and define scripting

We make a PHP file and save it with a name text_to_image.php

In this step we first set the image header to return image after processing.Then we load the image from directory and create that image using imagecreatefromjpeg[] function then we allocate the color.

We use white color in text so we use RGB value of white you can use any color RGB value, then we specify font path it is very important to add font path because without using font your text will not be visible you can use any font for your text.

Then we set the text and use imagettftext[] function to add text to images you can also customise the size, angle and position of the text as per your need.You may also like display text over image.

Thats all, this is how to add text on images using PHP and php gd library.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Latest Tutorials

  • Create Animated Skill Bar Using jQuery, HTML And CSS
  • Simple And Best Responsive Web Design Using CSS Part 2
  • Show Button On Hover Using HTML And CSS
  • Material Design Login Form Using jQuery And CSS
  • Animated Progress Bar Using jQuery And CSS
  • Dynamic Drop Down Menu Using jQuery, Ajax, PHP And MySQL
  • Get Website Statistics Using PHP, jQuery And MySQL
  • HTML5 Login And Signup Form With Animation Using jQuery
  • Facebook Style Homepage Design Using HTML And CSS
  • View Webpage In Fullscreen Using jQuery

How to put text On image in PHP?

To add text to an image in PHP, we can use the GD extension..
$img = imagecreatefromjpeg["IMG. JPG"];.
$size = 24; $x = 5; $y = 30; $angle = 0; $quality = 100;.
$color = imagecolorallocate[$img, 255, 255, 255];.
imagettftext[$img, $size, $x, $y, $angle, $color, "PATH/FONT. TTF", "TEXT"];.
imagejpeg[$img, "SAVE..

How can you display text in PHP?

PHP allows us to display the text in various formats using various inbuilt methods. Using echo command: The echo command can be used to display text, including numerical, strings and arrays.

How to Add text in PHP?

Answer: Use the PHP String Operators There is no specific function to append a string in PHP. But you can use the PHP concatenation assignment operator [ . = ] to append a string with another string.

How do I put text over a picture in laravel?

if you are use laravel and you want to add text on image or resize, crop image then we can do with "intervention/image" pakacke.

Chủ Đề