Writing text on image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Writing text on image

Post by kpraman »

Code: Select all

<?php
    if(!isset($_GET['size'])) $_GET['size'] = 26;
    if(!isset($_GET['text'])) $_GET['text'] = "Hello";

    $size = imagettfbbox($_GET['size'], 0, "ARIAL", $_GET['text']);
    $xsize = abs($size[0]) + abs($size[2]);
    $ysize = abs($size[5]) + abs($size[1]);

    $image = imagecreatefromjpeg("3_1.jpg");
    $imagesize = getimagesize("3_1.jpg");
    $textleftpos = round(($imagesize[0] - $xsize) / 2);
    $texttoppos = round(($imagesize[1] + $ysize) / 2);
    $white = ImageColorAllocate($image, 255,255,255);

    imagettftext($image, $_GET['size'], 45, $textleftpos, $texttoppos, $white, "ARIAL", $_GET['text']);
    header("content-type: image/png");
    $img=imagejpeg($image);
    imagedestroy($image);
	
	
 
echo "<img src= $img >";

?>
WIth the above script i am able to write text on an image. How can i change font style(bold,italic..)?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

research the imagettftext function
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Also try imagestring
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

Post by kpraman »

Thanks for replying. Solved after i gave full path of .ttf
Post Reply