<?Php
header ("Content-type: image/jpg");
///// Create the image ////////
$im = @ImageCreate (100,40)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204); // Assign background color
$text_color = ImageColorAllocate ($im, 51, 51, 255); // text color is given
ImageString($im,5,14,0,'PLUS2NET',$text_color); // Text added
ImageJpeg ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
?>
$source_image : Image resource created before $int_font: Values from 1 to 5 for built in fonts $int_x: Position of font in Horizontal X axis, Leftmost point is 0 $int_y: Position of the font in Vertical Y Axis, Topmost point is 0 $text: Text string to be written on the image $int_color : Color created before using imagecolorallocate().
By using imagettftext() we can use font of any size ( more than 5 ),
whereas by using imagestring() maximum font size of 5 can be used.
How to diaply Image
We saved the above code in a PHP file as gd-imagestring-demo.php and displayed it as Image by using HTML code.
<img src=gd-imagestring-demo.php>
Aligning the text on the Image
Left top postion is X=0, Y=0. The top edge of the font will allign with Y coordinate value.
Example: If Y= 0 then top edge of the font will allign with top edge of the image canvas.
Align Top
The top edge of the text should allign with top edge of the canvas.
ImageString($im,5,14,0,'PLUS2NET',$text_color);
Allign Left Top
ImageString($im,5,0,0,'PLUS2NET',$text_color);
Font Height & Width
We can read the height of the font by using imagefontheight(X), here X can take value 1 to 5 for internal fonts. Similarly we can get width of the font by using imagefontwidth(X).
We will use above functions to know height and width of the fonts used. Based on this value we can allign Text to Right or bottom of the image.
Align Right
<?Php
header ("Content-type: image/jpg");
$height=40; // Canvas Height
$width=100; // Canvas Width
$font_width=imagefontwidth(5); // Width of each char used in text string
$text="PLUS2NET"; // Text string to be written on Image.
$no_text=strlen($text); // Number of chars in our text string.
$text_width=$no_text * $font_width; // Width of text string.
$x=$width-$text_width; // X - Postion of text string to allign Right.
///// Create the image ////////
$im = @ImageCreate ($width,$height)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204); // Assign background color
$text_color = ImageColorAllocate ($im, 51, 51, 255); // text color is given
ImageString($im,5,$x,10,$text,$text_color); // Random string from session added
ImageJpeg ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
?>
In the above code value of $x is found out after considering width of each character, number of chars present in the string and width of the canvas. By multiplying font width with number of chars present we get the total width of the text string. From the width of the canvas we subtract the width of the text string to get the X- Position ( $x ).
Align Bottom
<?Php
header ("Content-type: image/jpg");
$height=40; // Canvas Height
$width=100; // Canvas Width
$font_height=imagefontheight(5);
$font_width=imagefontwidth(5);
$text="PLUS2NET"; // Text to be written on Image.
$no_text=strlen($text); // Number of chars in our text string.
$text_width=$no_text * $font_width; // Width of text string.
$x=$width-$text_width; // X - Postion of text string to allign Right.
$y=$height-$font_height; // Y- Postion of text string to allign bottom.
///// Create the image ////////
$im = @ImageCreate ($width,$height)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204); // Assign background color
$text_color = ImageColorAllocate ($im, 51, 51, 255); // text color is given
ImageString($im,5,$x,$y,$text,$text_color); // Random string from session added
ImageJpeg ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
?>
Flexible Image height & width
In above code we have defined the height and width of the image. Now we will change this to arrive at height and width of the canvas based on the input text string.
<?Php
header ("Content-type: image/jpg");
///// Create the image ////////
$font_height=imagefontheight(5);
$font_width=imagefontwidth(5);
$text='PLUS2NET.COM'; // Text string can be changed to reflect width of canvas to change.
$width = (strlen($text)*$font_width)+10 ; // Canvas width = String with, added with some margin
$height=$font_height + 10; // Canvas height = String height, added with some margin
$im = @ImageCreate ($width,$height)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204); // Assign background color
$text_color = ImageColorAllocate ($im, 51, 51, 255); // text color is given
ImageString($im,5,5,$height/3,$text,$text_color); // Random string from session added
ImageJpeg ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
?>