adeelahmad Posted June 3, 2007 Share Posted June 3, 2007 i need help how can i generate multi color text image with php gd lib like image.php?txt= Hello%20World $txt=$_GET['txt']; $txt=exploit(" ", $txt); what to do next i want $txt[0] in black and $txt[1] in red with white background can any one help me??? Link to comment https://forums.phpfreaks.com/topic/54089-php-gd-lib-help/ Share on other sites More sharing options...
Barand Posted June 3, 2007 Share Posted June 3, 2007 $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 More sharing options...
adeelahmad Posted June 3, 2007 Author Share Posted June 3, 2007 its not working for me. Link to comment https://forums.phpfreaks.com/topic/54089-php-gd-lib-help/#findComment-267427 Share on other sites More sharing options...
chigley Posted June 3, 2007 Share Posted June 3, 2007 Any errors? Most probably because your server doesn't have GD installed. Go directly to textimage.php to see errors. Link to comment https://forums.phpfreaks.com/topic/54089-php-gd-lib-help/#findComment-267432 Share on other sites More sharing options...
adeelahmad Posted June 3, 2007 Author Share Posted June 3, 2007 hey thankx it works i am sorry i was passing wrong perameter but still one problem how ca i use a font file with this?? Link to comment https://forums.phpfreaks.com/topic/54089-php-gd-lib-help/#findComment-267438 Share on other sites More sharing options...
Barand Posted June 3, 2007 Share Posted June 3, 2007 To get the width and height of the strings you have to get the bounding box coords as the text will be proportional and not fixed width www.php.net/imagettfbbox and to output the text use www.php.net/imagettftext Link to comment https://forums.phpfreaks.com/topic/54089-php-gd-lib-help/#findComment-267448 Share on other sites More sharing options...
adeelahmad Posted June 4, 2007 Author Share Posted June 4, 2007 i still have problem if i have long string like Recommended Buys it over write second word on first please help me. Link to comment https://forums.phpfreaks.com/topic/54089-php-gd-lib-help/#findComment-267671 Share on other sites More sharing options...
Barand Posted June 4, 2007 Share Posted June 4, 2007 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 More sharing options...
adeelahmad Posted June 5, 2007 Author Share Posted June 5, 2007 thankx for helping buts bold is there any option i can make this regular text? Link to comment https://forums.phpfreaks.com/topic/54089-php-gd-lib-help/#findComment-268204 Share on other sites More sharing options...
Barand Posted June 5, 2007 Share Posted June 5, 2007 it is regular text. Choose another font, maybe a light face. Link to comment https://forums.phpfreaks.com/topic/54089-php-gd-lib-help/#findComment-268620 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.