imageftbbox() function

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

User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

imageftbbox() function

Post 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....
Last edited by vigge89 on Tue Nov 04, 2003 2:03 pm, edited 3 times in total.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

noone? 8O :(
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ok, found a tut, someone posted it at another topic, ill check it now...
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

now i just have to wait for my host to reply with the answer to if he could install GDL :wink:
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

oh, found out what got it not to work, the

Code: Select all

<?php
header ("Content-type: image/png");
?>
tag was missing ;)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

no answer?
what i wanna know is how you can set the text to be centered in the image?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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."
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ok, thanks for the advices :)
BTW, what does the @ (at) do?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply