December 24, 2023 5:09 am

Add text to images with PHP

In this tutorial i will show you how you can add text using GD Library here is a very simple code to do that.

[wpdm_file id=8]

Write Text on image

In this file we use an image and write some text when we run that code it will write text on that image where ever you want to add in the image X and Y coordinates required.

<?php

header ("Content-type: image/jpeg");

$string = "This is phpGang.com";

$font = 4;

$width = imagefontwidth($font) * strlen($string) ;

$height = imagefontheight($font) ;

$im = imagecreatefromjpeg("gang.jpg");

$x = imagesx($im) - $width ;

$y = imagesy($im) - $height;

$backgroundColor = imagecolorallocate ($im, 255, 255, 255);

$textColor = imagecolorallocate ($im, 0, 0,0);

imagestring ($im, $font, $x, $y, $string, $textColor);

imagejpeg($im);

?>

After running this code you will get image like this.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

3 responses to “Add text to images with PHP”

  1. samfingcul says:

    hello.
    how can i replace the background image (gang.jpg) with a database images which will pick them randomly?
    i mean.. i want to replace gang.jpg with $random_images_from_a_path

    thank you

  2. Shahbaz Ahmed Bhatti says:

    i need some help in php can anybody help me please

Leave a Reply

Your email address will not be published. Required fields are marked *