In Image Creation From User Input in PHP, we use the PHP Graphics Draw library which is also known as PHP gd. To create images using PHP code we have to use Image processing and graphics library functions.
On the complete process, we go for user input first, where the user can input letters or words and their sizes to create an image from the character using PHP. Here we create two files, one is an HTML input page and another is taking the user input data to create an image.
- index.php
- imgCreate.php
Source Code of Image Creation From User Input in PHP
User Input Via HTML Form
index.php
Here we take input from users using HTML form. In the process of creation don’t forget to add enctype="multipart/form-data"
attribute. Here we take image text, image height, and width from the user via a form.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<form method="post" action="imgCreate.php" enctype="multipart/form-data"> <p> <label>Image Text</label> <input type="text" name="imageText"> </p> <p> <label>Image Width</label> <input type="number" name="imageWidth"> </p> <p> <label>Image Height</label> <input type="number" name="imageHeight"> </p> <p><input type="submit" name="createImage" value="Create Image"></p> </form> |
Convert User Input Text to Image Using PHP GD Library
imgCreate.php
After creating a form we first check the button is clicked or not using isset()
function. And takes all the inputs from the user using the POST method. Then put those values on the PHP GD functions as parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php if (isset($_POST['createImage'])) { // imagecreate ( int $width , int $height ) $width = $_POST['imageWidth']; $height = $_POST['imageHeight']; $imageText = $_POST['imageText']; $my_img = imagecreate( $width, $height ); // imagecolorallocate ( resource $image , int $red , int $green , int $blue ) $background = imagecolorallocate( $my_img, 42, 72, 97 ); $text_colour = imagecolorallocate( $my_img, 226, 226, 226 ); $line_colour = imagecolorallocate( $my_img, 128, 255, 0 ); // imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color ) imagestring( $my_img, 4, 30, 25, $imageText, $text_colour ); imagesetthickness ( $my_img, 5 ); imageline( $my_img, 30, 45, 165, 45, $line_colour ); header( "Content-type: image/png" ); imagepng( $my_img ); imagecolordeallocate( $background ); imagecolordeallocate( $text_color ); imagecolordeallocate( $line_color ); imagedestroy( $my_img ); ?> <img src="imgCreate.php" alt="Image created by a PHP script" width="<?php echo $width; ?>" height="<?php echo $height; ?>"> <?php } ?> |
Code Explanations of PHP Create Image From Text:
imagecreate(int $width , int $height)
Image create is a function that is used to create an image and if you did not give any input it returns a blank image. As a parameter, it takes the width and height of an image.imagecolorallocate ( resource $image , int $red , int $green , int $blue )
Image color allocate is also a PHP inbuilt function that is used to fill the background color on the image. It takes RGB color code as a parameter.imagestring( resource $image , int $font , int $x , int $y , string $string , int $color )
This function used to produce an image from the given text and also + icon for space.
Here are some important function explanations which we use to produce image from the text. And these functions are PHP inbuilt functions.
If you want to know more about the PHP Graphics library you can check here: https://www.php.net/manual/en/book.image.php
PHP Create Image From Text Output
Also Check:
- Get List of Holidays Using Google Calendar API
- 2 Ways To Check if Email already exists Using PHP
- Allow Only String and Numbers Using PHP
- Migrate WordPress Site To New Host Manually
2 Replies to “Image Creation From User Input in PHP”