Jump to content

php GD lib help


adeelahmad

Recommended Posts

$txt = 'Hello World';
echo "<img src='textimage.php?text=$txt'>";

 

where textimage.php is

<?php
$text = $_GET['text'];

$txt = explode(' ', $text);
/**
* calculate image size required
*/
$w = strlen($text) * imagefontwidth(5) + 40;
$h = imagefontheight(5);

$im = imagecreate($w, $h + 10);

/**
* define colours
*/
$bg = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); // white
$bl = imagecolorallocate($im, 0x00, 0x00, 0x00); // black
$rd = imagecolorallocate($im, 0xFF, 0x00, 0x00); // red

/**
* place the text
*/
$x1 = 20;
$x2 = 20 + strlen($txt[0]) * imagefontwidth(5);
$y = 5;

imagestring($im, 5, $x1, $y, $txt[0], $bl);
imagestring($im, 5, $x2, $y, ' ' . $txt[1], $rd);

imagerectangle($im, 0, 0, $w-1, $h+9, $bl);    // border

/**
* output image
*/
header ("content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

Link to comment
https://forums.phpfreaks.com/topic/54089-php-gd-lib-help/#findComment-267394
Share on other sites

try

<?php
$text = $_GET['text'];

$txt = explode(' ', $text);

/**
* calculate image size required
*/
$font = 'C:/windows/fonts/tahoma.ttf';
$size = 18;

$bbox = imagettfbbox($size, 0, $font, $text);
$bbox2 = imagettfbbox($size, 0, $font, $txt[0]);

$w = $bbox[4] - $bbox[0] + 20;
$h = $bbox[1] - $bbox[5];

$im = imagecreate($w, $h + 10);

/**
* define colours
*/
$bg = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); // white
$bl = imagecolorallocate($im, 0x00, 0x00, 0x00); // black
$rd = imagecolorallocate($im, 0xFF, 0x00, 0x00); // red


/**
* place the text
*/
$x1 = 10;
$x2 = 10 + $bbox2[4] - $bbox2[0];
$y = $h;

imagettftext($im, $size, 0, $x1, $y, $bl, $font, $txt[0]) ;
imagettftext($im, $size, 0, $x2, $y, $rd, $font, ' ' . $txt[1]) ;

imagerectangle($im, 0, 0, $w-1, $h+9, $bl);    // border

/**
* output image
*/
header ("content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

Link to comment
https://forums.phpfreaks.com/topic/54089-php-gd-lib-help/#findComment-267744
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.