PHP – Add text on an image with background color and save it on the server

About one and a half year ago, i published a blog post about adding text to image in PHP.
PHP – Add Text on an Image

The above example only display the image in browser, but it would not save the image on the server. Today i would like to show you another example which could save the image with text added on server.

What you need is 3 files.

  • index.php
  • kenshin.jpg (The image which you wanna add text on it)
  • arial.ttf (Or any other .ttf font file)

 

So here is the kenshin.jpg.

 

And this is the index.php.

<?php
$image_filepath = './kenshin.jpg';
saveImageWithText("Welcome to Eureka!", $color, $image_filepath);

function saveImageWithText($text, $color, $source_file) { 
  
  $public_file_path = '.';
  
  // Copy and resample the imag
  list($width, $height) = getimagesize($source_file);
  $image_p = imagecreatetruecolor($width, $height);
  $image = imagecreatefromjpeg($source_file);
  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height); 
  
  // Prepare font size and colors
  $text_color = imagecolorallocate($image_p, 0, 0, 0);
  $bg_color = imagecolorallocate($image_p, 255, 255, 255);
  $font = $public_file_path . '/arial.ttf';
  $font_size = 12; 
  
  // Set the offset x and y for the text position
  $offset_x = 0;
  $offset_y = 20;
  
  // Get the size of the text area
  $dims = imagettfbbox($font_size, 0, $font, $text);
  $text_width = $dims[4] - $dims[6] + $offset_x;
  $text_height = $dims[3] - $dims[5] + $offset_y;
  
  // Add text background
  imagefilledrectangle($image_p, 0, 0, $text_width, $text_height, $bg_color);
  
  // Add text
  imagettftext($image_p, $font_size, 0, $offset_x, $offset_y, $text_color, $font, $text);
  
  // Save the picture
  imagejpeg($image_p, $public_file_path . '/output.jpg', 100); 

  // Clear
  imagedestroy($image); 
  imagedestroy($image_p); 
}; 

 

In the webroot folder, put all the files inside.

 

Browse the link in browser and check the folder again.

 

So this is the output.jpg.


 

The above example also works with other image formats. For .png, update the following functions in index.php.

  • imagecreatefromjpeg() to imagecreatefrompng()
  • imagejpeg() to imagepng()

 

Done =)

Reference:

Advertisement

2 thoughts on “PHP – Add text on an image with background color and save it on the server”

  1. very nice script but i have one problem perhaps you can help me.
    I tested this on one server and it worked.

    But i want to use it on another server, it doesn’t work there.
    This is the the failure:
    “Warning: imagettfbbox() [function.imagettfbbox]: No character set found in /public/sites/www.mysite.com/index.php on line 36″ The fontfile is places on the server.
    Warning: imagettftext() [function.imagettftext]: No character set found in /public/sites/www.mysite.com/index.php on line 48”

    And again it worked perfectly on another server, does anyone know what could be the problem?

    kind regards
    Roy

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.