PHP Programming Exercises
- Write a php program to compare between things that are not integers
- Write a division table program in PHP using for loop
- Write a program in PHP to print prime numbers between 1 and 100
- Write a php program to print numbers from 10 to 1 using the recursion function
- Write a php program to store the username in cookie and check whether the user have successfully login or not
- Write a php program to convert the given string into an array
- Write a php program to loop over the json data
- Write a program in PHP to remove all html tags except paragraph and italics tags
- Write a program to loop through an associative array using foreach() or with each()
- Write a php program to differentiate between fgets, fgetss and fgetcsv
- There are two deals of an item to buy. The quantities and prices of the item are given below. Write a program in PHP to find the best deal to purchase the item
- Write a php program to set session on successful login
- Write a program in PHP to read from directory
- PHP create image from text and save
- How to get data from XML file in PHP
- PHP Create Word Document from HTML
- How to check whether a year is a leap year or not in PHP
- Fibonacci Series Program in PHP
- How to generate QR Code in PHP
- How does PHP store data in cache?
- How to detect a mobile device using PHP?
- How to send HTML form data to email using PHP?
- How to get location from IP address using PHP?
- How to lock a file using PHP?
- How to import a CSV file into MySQL using PHP
- How to fetch data from database in PHP and display in PDF
- How to insert image in database using PHP
- How to remove last character from string using PHP?
- Write a PHP program to reverse a string without predefined function
- Write a PHP program to calculate percentage of total
- How to sanitize input for MySQL using PHP?
- Write a program to calculate electricity bill in PHP
- How to send email with SMTP in PHP?
- How to Send Text Messages With PHP?
- How to convert stdClass object to Array in PHP?
- How do I import Excel data into MySQL database using PHP?
- How can I post a form without refreshing the page?
- How to sort table columns with PHP and MySQL?
- How to get current directory, filename and code line number in PHP
Solution:
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.
These are the some PHP predefined functions used for generating images
imagecreatetruecolor()
This function is used to create an image of the specified size. It takes width and height as parameters.
imageceatetruecolor(int $width, int $height);
imagecolorallocate()
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);
imagefilledrectangle()
This function is used to draw a filled rectangle with a given start and end coordinates. It takes six parameters. In first parameter, it takes image resource and in next four parameters, it takes the rectangle coordinates and in last parameter, it takes the color code to fill the rectangle.
imagefilledrectangle(resource $image, int $x1, int $y1, int $x2, int $y2, $fillcolor );
imagestring()
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 that can be from 1 to 5 (smaller to bigger), third and fourth parameters are the coordinates from the left corner and fifth parameter is the string to be written and sixth parameter contains string color.
imagestring(resource $image, int $font , int $x , int $y , string $string , int $color );
imagesetthickness()
The imagesetthickness() function is used to set the thickness of line drawing. In the first parameter, it takes image resource and in the second parameter it takes thickness size in pixels.
imagesetthickness(resource $image, int $size);
imagepng()
The imagepng() function is used to generate image in png format.
imagepng(resource $image);
imagedestroy()
The imagedestroy() function is used to at last to free the allocated memory associated with the image.
PHP Generate Image From Text Example
Here is a very simple example to generate an image from text and save the generated image in png format -
Output

need an explanation for this answer? contact us directly to get an explanation for this answer