imageftbbox() function
Moderator: General Moderators
imageftbbox() function
how do you use the GD library?
ive searched the web, but couldn't come up with any good tutorials on how to use it. Say, that i would like to create a image (png), which should have text loaded from a variable outputted using a font (tahoma etc., size 8 ).
How do I do that?
Check the 2nd page for the new question....
ive searched the web, but couldn't come up with any good tutorials on how to use it. Say, that i would like to create a image (png), which should have text loaded from a variable outputted using a font (tahoma etc., size 8 ).
How do I do that?
Check the 2nd page for the new question....
Last edited by vigge89 on Tue Nov 04, 2003 2:03 pm, edited 3 times in total.
Recommending viewtopic.php?t=13776&highlight=GD also.
I tested GDL on my local server, and when i run this script;
The output is some strange letters lot's of ?'s and ,¿ and .
i can take a ss, but i dont think thats needed :/
the font HOOG0550.TTF does exist in the same folder as the image.php file, GDL i enabled, freetype is enabled.
Code: Select all
<?php
$im = imagecreate (100, 20);
$bg = ImageColorAllocate ($im, 0, 0, 0);
$fg = ImageColorAllocate ($im, 255, 255, 255);
ImageTTFText ($im, 20, 0, 10, 20, $fg, "HOOG0550.TTF",
"vigge");
ImagePNG($im);
?>i can take a ss, but i dont think thats needed :/
the font HOOG0550.TTF does exist in the same folder as the image.php file, GDL i enabled, freetype is enabled.
oh, found out what got it not to work, the
tag was missing 
Code: Select all
<?php
header ("Content-type: image/png");
?>is it possible to make the text written by "ImageTTFText" placed center?
The only thing ive come up to is
but that wont work becuase the text itselft is not centered.
The only thing ive come up to is
Code: Select all
<?php
$txt_pos = $width_of_img / 2;
?>from array imagettfbbox ( int size, int angle, string fontfile, string text)
you can get the width and height of the space taken by string text. If you substract the half of it from the half of the width/height of your image (used as coordinates for imagettfstring) the text should be (more or less) centered.
you can get the width and height of the space taken by string text. If you substract the half of it from the half of the width/height of your image (used as coordinates for imagettfstring) the text should be (more or less) centered.
when running this
The image couldn't show up, where have i done something wrong?
I get
"The image “http://localhost/test/image.php” cannot be displayed, because it contains errors."
Code: Select all
<?php
header ("Content-type: image/png");
//Text to print
$txt = $_GET['txt'];
if($txt == "") { $txt = "vigge89"; };
//Font settings
$font = "HOOG0550.ttf";
$fsize = "6";
$h = 7; //Height
//Center
$text_bbox = ImageTTFBBox ($fsize, 0, $font, $txt);
$img_width = $text_bbox[0];
//Create image
$im = imagecreate ($img_width, $h);
//Color setup
$bc = ImageColorAllocate ($im, 65,65,65); //Background
$fc = ImageColorAllocate ($im, 150,150,150); //Text
//Write text from variables
ImageTTFText ($im, $fsize, 0, 1, 6, $fc, $font, $txt);
//Create image
ImagePNG ($im);
?>I get
"The image “http://localhost/test/image.php” cannot be displayed, because it contains errors."
imagettfbox() does not return the width directly but the corner points' coordinates.
I also suggest something liketo avoid possible warning output.
For debugging it might be helpful to placeat the top of the script and mark the content-type directly before sending the image data.This way you can see errors/warnings sent by php if you call the document directly (not nested in a <img src=....> element) even if you do not have access to the error.log
Code: Select all
$img_width = $text_bbox[2]-$text_bbox[0];Code: Select all
$txt = @$_GET['txt'];
if(empty($txt)) { $txt = "vigge89"; };For debugging it might be helpful to place
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);Code: Select all
//Write text from variables
ImageTTFText ($im, $fsize, 0, 1, 6, $fc, $font, $txt);
//Create image
header ("Content-type: image/png");
ImagePNG ($im);
?>@ supress' a possible warning.
)
@ will supress this warning, $txt is set to false and empty() will catch this. $txt == "" does as well, but I find it more clear using empty(). Only drawback (which is probably negligible for you) is0 is considered emtpy as well.
if 'txt' is not set within $_GET trying to assign its value to $txt raises a undefined index warning. Now if display_errors is enabled the warning text is sent to the client, making it either impossible to set another content-type or an invalid image (unlikely a browser will recognize "warning: ..." as a valid png-data-header$txt = @$_GET['txt'];
@ will supress this warning, $txt is set to false and empty() will catch this. $txt == "" does as well, but I find it more clear using empty(). Only drawback (which is probably negligible for you) is
Code: Select all
<?php
$txt = '0';
echo (empty($txt)) ? 'yes' : 'no';
?>