Convert Text to Image in PHP

In many cases, we need to create a dynamic image on the fly and write text on the image in our web application. If you have a requirement like this, don’t worry we’ll provide a simple PHP script to convert text to image and save it as PNG or JPG format.

In this tutorial, we are going to show you the easiest way to create an image from text using PHP. To control the text to image creation process, a PHP class will be created. By using this PHP class you can easily create an image and add multiple line text string to the image.

GD library will be used to create and manipulate image file in our PHP script. So before you get started, make sure PHP GD is installed on your server.

Are you want to get the step-by-step guide to installing PHP GD library on the server? Go through the following tutorial links.

TextToImage Class

The TextToImage PHP class helps to generate an image and add text to image. TextToImage class contains 4 functions and the functions will do following.

  • createImage() – Creates image from text. The text string needs to be provided that want to write on the image. Also, you can specify the text font size, image width, and image height.
  • showImage() – Returns an image that created by the createImage() function.
  • saveAsPng() – Save image file as .png format. You can specify the filename and the location to save the image.
  • saveAsJpg() – Save image file as .jpg format. You can specify the filename and the location to save the image.
<?php
/**
 * TextToImage class
 * This class converts text to image
 * 
 * @author    CodexWorld Dev Team
 * @link    http://www.codexworld.com
 * @license    http://www.codexworld.com/license/
 */
class TextToImage {
    private 
$img;
    
    
/**
     * Create image from text
     * @param string text to convert into image
     * @param int font size of text
     * @param int width of the image
     * @param int height of the image
     */
    
function createImage($text$fontSize 20$imgWidth 400$imgHeight 80){

        
//text font path
        
$font 'fonts/the_unseen.ttf';
        
        
//create the image
        
$this->img imagecreatetruecolor($imgWidth$imgHeight);
        
        
//create some colors
        
$white imagecolorallocate($this->img255255255);
        
$grey imagecolorallocate($this->img128128128);
        
$black imagecolorallocate($this->img000);
        
imagefilledrectangle($this->img00$imgWidth 1$imgHeight 1$white);
        
        
//break lines
        
$splitText explode "\\n" $text );
        
$lines count($splitText);
        
        foreach(
$splitText as $txt){
            
$textBox imagettfbbox($fontSize,$angle,$font,$txt);
            
$textWidth abs(max($textBox[2], $textBox[4]));
            
$textHeight abs(max($textBox[5], $textBox[7]));
            
$x = (imagesx($this->img) - $textWidth)/2;
            
$y = ((imagesy($this->img) + $textHeight)/2)-($lines-2)*$textHeight;
            
$lines $lines-1;
        
            
//add some shadow to the text
            
imagettftext($this->img$fontSize$angle$x$y$grey$font$txt);
            
            
//add the text
            
imagettftext($this->img$fontSize$angle$x$y$black$font$txt);
        } return true;
    }
    
    
/**
     * Display image
     */
    
function showImage(){
        
header('Content-Type: image/png');
        return 
imagepng($this->img);
    }
    
    
/**
     * Save image as png format
     * @param string file name to save
     * @param string location to save image file
     */
    
function saveAsPng($fileName 'text-image'$location ''){
        
$fileName $fileName.".png";
        
$fileName = !empty($location)?$location.$fileName:$fileName;
        return 
imagepng($this->img$fileName);
    }
    
    
/**
     * Save image as jpg format
     * @param string file name to save
     * @param string location to save image file
     */
    
function saveAsJpg($fileName 'text-image'$location ''){
        
$fileName $fileName.".jpg";
        
$fileName = !empty($location)?$location.$fileName:$fileName;
        return 
imagejpeg($this->img$fileName);
    }
}

Convert Text String to Image in PHP

Include TextToImage.php file (TextToImage class) and create an object. Call the createImage() function and provide the text string. If you want to add multiple lines to the image, add “\n” before the new line.

//include TextToImage class
require_once 'TextToImage.php';

//create img object
$img = new TextToImage;

//create image from text
$text 'Welcome to CodexWorld.\nThe World of Programming.';
$img->createImage($text);

To display image use showImage() function.

//display image
$img->showImage();

To save the image file as PNG or JPG format use saveAsPng() or saveAsJpg() function. Specify the file name and location to save the image, otherwise, the image will be saved with the default name to the same directory where the script is located.

//save image as png format
$img->saveAsPng('codex-text-to-image','images/');

//save image as jpg format
$img->saveAsJpg('codex-text-to-image','images/');

Conclusion

In TextToImage class we’ve used THE UNSEEN font as the text font. If you wish to change the text font, place the font file in the fonts/ directory and specify the font path in createImage() function ($font) of TextToImage class.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

2 Comments

  1. Barala Said...
  2. Henk Said...

Leave a reply

keyboard_double_arrow_up