etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

PHP create image from text and save

In this article, you will learn how to create an image from the text using the PHP programming language.

PHP has several built-in functions that are used to create some advanced applications. It also has built-in functions for processing images. In many cases, we need to dynamically convert text to an image, such as when generating CAPTCHA, adding a watermark to an image, creating a logo, or generating some signatures.





Before we get started, we need to have GD extensions enabled. You can quickly check this by using the phpinfo() function. If the GD extension is not enabled, simply open php.ini and search for extension=gd2 and remove the semicolon in front of this.

How To Create Text Image Using PHP

These are some PHP predefined functions used for generating images.

PHP imagecreatetruecolor() function

This function is used to create an image of the specified size. It takes width and height as parameters.

imageceatetruecolor(int $width, int $height);


PHP imagecolorallocate() function

This function is used to fill the background color of the image, and it returns an identifier for that particular color. It takes four parameters. Image resource in the first parameter, and the other three parameters contain color code in RGB component form.

imagecolorallocate(resource $image, int $red, int $green, int $blue);




PHP imagefilledrectangle() function

This function is used to draw a filled rectangle with the given start and end coordinates. It takes six parameters. In the first parameter, it takes image resource and in the next four parameters, it takes the rectangle coordinates and in the last parameter, it takes the color code to fill the rectangle.

imagefilledrectangle(resource $image, int $x1, int $y1, int $x2, int $y2, $fillcolor );


PHP imagestring() function

The imagestring() function is used to write text to the image. In the first parameter, it takes the image source, second parameter is the font size, which can be from 1 to 5 (smaller to bigger), third and fourth parameters are the coordinates from the left corner and the fifth parameter is the string to be written and the sixth parameter contains string color.

imagestring(resource $image,  int $font , int $x , int $y , string $string , int $color );


PHP imagesetthickness() function

The imagesetthickness() function is used to set the thickness of line drawing. In the first parameter, it takes an image resource, and in the second parameter it takes the thickness size in pixels.

imagesetthickness(resource $image,  int $size);


PHP imagepng() function

The imagepng() function is used to generate image in png format.

imagepng(resource $image);


PHP imagedestroy() function

The imagedestroy() function is used at last to free the allocated memory associated with the image.

imagedestroy();




PHP Generate Image From Text Example

Here is a very simple PHP program to generate an image from text and save the generated image in png format.

<?php
// Set the content-type
header('Content-Type: image/png');

// Create the image
$img = imagecreatetruecolor(310, 60);

// Create some colors
$lightsky = imagecolorallocate($img, 135, 206, 250);
$blue = imagecolorallocate($img, 25, 25, 112);
imagefilledrectangle($img, 0, 0, 319, 59, $lightsky);

// The text to draw
$text = 'Welcome to etutorialspoint!';

imagestring( $img, 5, 30, 20, $text, $blue ); 
imagesetthickness( $img, 10 ); 

// Using imagepng() results in clearer text compared with imagejpeg() 
imagepng($img);
imagedestroy($img); 
?> 

Output





Related Articles

PHP get IP address of visitor
Image popup on page load using HTML and jQuery
PHP Contact Form with CAPTCHA and Validation
PHP Graphics: Drawing Line, Rectangle, Polygon
How to insert image in database using PHP
Upload multiple files and store in MySQL database using PHP
multiple choice quiz in PHP and MySQL
Import Data Into MySQL From Excel File
CRUD operations in Python using MYSQL Connector
How to store image in database using PHP
How to send emojis in email subject and body using PHP
PHP7.3 New Features, Functions and Deprecated Functions
Create pie chart using google api
PHP upload multiple files
Most in demand programming languages for 2019
Most in demand NoSQL databases software for 2019
Best mvc PHP frameworks in 2019
PHP MySQL PDO Database Connection and CRUD Operations
Top Android App Development Languages in 2019




Most Popular Development Resources
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
PHP Create Word Document from HTML
-----------------
How to get data from XML file in PHP
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP code to send email using SMTP
-----------------
Characteristics of a Good Computer Program
-----------------
How to encrypt password in PHP
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
How to add multiple custom markers on google map
-----------------
How to get current directory, filename and code line number in PHP
-----------------
Fibonacci Series Program in PHP
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
How to generate QR Code in PHP
-----------------
Simple pagination in PHP
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
PHP MYSQL Advanced Search Feature
-----------------
PHP Server Side Form Validation
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
jQuery File upload progress bar with file size validation
-----------------
Simple PHP File Cache
-----------------
Simple File Upload Script in PHP
-----------------
Php file based authentication
-----------------
To check whether a year is a leap year or not in php
-----------------
Calculate distance between two locations using PHP
-----------------
PHP User Authentication by IP Address
-----------------
PHP Secure User Registration with Login/logout
-----------------
Simple way to send SMTP mail using Node.js
-----------------
How to print specific part of a web page in javascript
-----------------
Simple Show Hide Menu Navigation
-----------------
Detect Mobile Devices in PHP
-----------------
Polling system using PHP, Ajax and MySql
-----------------
PHP Sending HTML form data to an Email
-----------------
Google Street View API Example
-----------------
Get Visitor\'s location and TimeZone
-----------------
SQL Injection Prevention Techniques
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
Convert MySQL to JSON using PHP
-----------------
Set and Get Cookies in PHP
-----------------
CSS Simple Menu Navigation Bar
-----------------
PHP Programming Error Types
-----------------
Date Timestamp Formats in PHP
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
How to add google map on your website and display address on click marker
-----------------
Write a python program to print all even numbers between 1 to 100
-----------------
How to display PDF file in web page from Database in PHP
-----------------
PHP Getting Document of Remote Address
-----------------
File Upload Validation in PHP
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview







Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2023. All Rights Reserved.