Page 1 of 2

imageftbbox() function

Posted: Mon Oct 27, 2003 9:09 am
by vigge89
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....

Posted: Tue Oct 28, 2003 1:59 am
by vigge89
noone? 8O :(

Posted: Tue Oct 28, 2003 2:14 am
by vigge89
ok, found a tut, someone posted it at another topic, ill check it now...

Posted: Tue Oct 28, 2003 3:39 am
by JAM

Posted: Tue Oct 28, 2003 5:01 am
by vigge89
now i just have to wait for my host to reply with the answer to if he could install GDL :wink:

Posted: Tue Oct 28, 2003 5:05 am
by Nay

Posted: Tue Oct 28, 2003 6:28 am
by vigge89
I tested GDL on my local server, and when i run this script;

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);
?>
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.

Posted: Tue Oct 28, 2003 6:33 am
by vigge89
oh, found out what got it not to work, the

Code: Select all

<?php
header ("Content-type: image/png");
?>
tag was missing ;)

Posted: Tue Oct 28, 2003 7:22 am
by vigge89
is it possible to make the text written by "ImageTTFText" placed center?
The only thing ive come up to is

Code: Select all

<?php
$txt_pos = $width_of_img / 2;
?>
but that wont work becuase the text itselft is not centered.

Posted: Wed Oct 29, 2003 9:36 am
by vigge89
no answer?
what i wanna know is how you can set the text to be centered in the image?

Posted: Wed Oct 29, 2003 10:01 am
by volka
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.

Posted: Wed Oct 29, 2003 10:51 am
by vigge89
when running this

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);

?>
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."

Posted: Wed Oct 29, 2003 11:33 am
by volka
imagettfbox() does not return the width directly but the corner points' coordinates.

Code: Select all

$img_width = $text_bbox[2]-$text_bbox[0];
I also suggest something like

Code: Select all

$txt = @$_GET['txt'];
if(empty($txt)) { $txt = "vigge89"; };
to avoid possible warning output.
For debugging it might be helpful to place

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
at the top of the script and mark the content-type directly before sending the image data.

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);
?>
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

Posted: Wed Oct 29, 2003 12:03 pm
by vigge89
ok, thanks for the advices :)
BTW, what does the @ (at) do?

Posted: Wed Oct 29, 2003 12:11 pm
by volka
@ supress' a possible warning.
$txt = @$_GET['txt'];
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 ;) )
@ 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';
?>
0 is considered emtpy as well.