473,246 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Centering text in a dynamically created image (GD or ImageMagick).

Anyone know offhand how to center a text within the bounding box of an
image (width-wise at least) without the Freetype library?

GD- or ImageMagick-specifc code would do nicely.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 6 '07 #1
6 4705
On 6 Jun, 05:34, -Lost <maventheextrawo...@techie.comwrote:
Anyone know offhand how to center a text within the bounding box of an
image (width-wise at least) without the Freetype library?

GD- or ImageMagick-specifc code would do nicely.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
1. Determine the width of the image.
2. Determine the width of the text, (I'm assuming you'll be outputting
text using TTF?), using imagettfbbox()
3. The left-hand position of the text should be : (image's width / 2)
- (text width / 2)

Jun 6 '07 #2
gbbulldog wrote:
On 6 Jun, 05:34, -Lost <maventheextrawo...@techie.comwrote:
>Anyone know offhand how to center a text within the bounding box of an
image (width-wise at least) without the Freetype library?

GD- or ImageMagick-specifc code would do nicely.
1. Determine the width of the image.
2. Determine the width of the text, (I'm assuming you'll be outputting
text using TTF?), using imagettfbbox()
3. The left-hand position of the text should be : (image's width / 2)
- (text width / 2)
Thanks!

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 7 '07 #3
-Lost wrote:
gbbulldog wrote:
>On 6 Jun, 05:34, -Lost <maventheextrawo...@techie.comwrote:
>>Anyone know offhand how to center a text within the bounding box of an
image (width-wise at least) without the Freetype library?

GD- or ImageMagick-specifc code would do nicely.
1. Determine the width of the image.
2. Determine the width of the text, (I'm assuming you'll be outputting
text using TTF?), using imagettfbbox()
3. The left-hand position of the text should be : (image's width / 2)
- (text width / 2)

Thanks!
Ooops! I have no clue why I said thanks.

I specifically said "without the Freetype library."

That function depends on its functionality to work as expected.

Thanks though.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 7 '07 #4
Rik
On Thu, 07 Jun 2007 19:24:39 +0200, -Lost <ma****************@techie.com>
wrote:
-Lost wrote:
>gbbulldog wrote:
>>On 6 Jun, 05:34, -Lost <maventheextrawo...@techie.comwrote:
Anyone know offhand how to center a text within the bounding box of an
image (width-wise at least) without the Freetype library?

GD- or ImageMagick-specifc code would do nicely.

1. Determine the width of the image.
2. Determine the width of the text, (I'm assuming you'll be outputting
text using TTF?), using imagettfbbox()
3. The left-hand position of the text should be : (image's width / 2)
- (text width / 2)
Thanks!

Ooops! I have no clue why I said thanks.

I specifically said "without the Freetype library."

That function depends on its functionality to work as expected.
Write your own variant of the freetypre library if it isn't available,
that's the only solution. There's no other way you can know the width of
dynamic text with an arbitrary font. Yes, it's a pain, so if the library
isn't available seriously consider changing hosts and/or change the server.

--
Rik Wasmus
Jun 7 '07 #5
On Jun 7, 7:49 pm, Rik <luiheidsgoe...@hotmail.comwrote:
On Thu, 07 Jun 2007 19:24:39 +0200, -Lost <maventheextrawo...@techie.com>
wrote:
-Lost wrote:
gbbulldog wrote:
On 6 Jun, 05:34, -Lost <maventheextrawo...@techie.comwrote:
Anyone know offhand how to center atextwithin the bounding box of an
image(width-wise at least) without the Freetype library?
>>>GD- orImageMagick-specifc code would do nicely.
>1. Determine the width of theimage.
2. Determine the width of thetext, (I'm assuming you'll be outputting
textusing TTF?), using imagettfbbox()
3. The left-hand position of thetextshould be : (image'swidth / 2)
- (textwidth / 2)
Thanks!
Ooops! I have no clue why I said thanks.
I specifically said "without the Freetype library."
That function depends on its functionality to work as expected.

Write your own variant of the freetypre library if it isn't available,
that's the only solution. There's no other way you can know the width of
dynamictextwith an arbitrary font. Yes, it's a pain, so if the library
isn't available seriously consider changing hosts and/or change the server.

--
Rik Wasmus
function getTextPosition($text,$center)
{
$im_tmp = imagecreatetruecolor(500,500);
$point_array = imagettftext($im_tmp,
20,0,0,50,imagecolorallocate($im0,0,0),'C:\Windows \Fonts\Verdana.ttf',
$text);
imagedestroy($im_tmp);
$left_x = $point_array[0];
$right_x = $point_array[2];
$length = $right_x - $left_x;
$text_position = $center - ($length/2);
return $text_position;
}
Sorry about the poor coding but this ought to take care of it. The
function will return the x coordinate that your text should be placed
at to be centered on $center. Just adjust the imagettftext function
to use your own font and size. All it does within the function is
place that text on a dummy image and read the points off of it. Let
me know if this works.

Eric

Jul 20 '07 #6
Rik
Hey, blast from the past:

On Fri, 20 Jul 2007 20:31:22 +0200, movement_eric <er**********@gmail.com
wrote:
On Jun 7, 7:49 pm, Rik <luiheidsgoe...@hotmail.comwrote:
>>>Anyone know offhand how to center atextwithin the bounding box of
an
>>>>image(width-wise at least) without the Freetype library?

Write your own variant of the freetypre library if it isn't available,
that's the only solution. There's no other way you can know the widthof
dynamictextwith an arbitrary font. Yes, it's a pain, so if the library
isn't available seriously consider changing hosts and/or change the
server.
function getTextPosition($text,$center)
{
....
$point_array = imagettftext($im_tmp,
http://www.php.net/imagettftext
"Notes
Note: This function requires both the GD library and the FreeType library."

So: no banana. Then again the FreeType library ought to be normally
available IMO.
--
Rik Wasmus
Jul 20 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: chotiwallah | last post by:
i have a dynamically created image represented by an imagehandler, but i don't wanna output to the browser or a file. is it possible to read that image directly into a string? any help...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
1
by: Marcus | last post by:
I have a problem maybe one of you could help me with. I've created a data entry screen with lots of dynamically-created client-side controls. I create HTML texboxes client-side by assigning a...
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
1
by: saikatbose2005 | last post by:
Hi, Ineed some help from anyone regarding an issue I'm facing. I've created an image dynamically on clicking a button. What I inetend to do is when I click on the created image it will popup a...
1
by: stimul8d | last post by:
okay; ASP. I have i listbox inside a user control which is dynamically created on page_init. I check for postback and only populate the datasource if it's false. regardless, i do this ...
1
by: helraizer1 | last post by:
Hi all, I have a dynamic image that picks out data from a dynamically created .line file. showimage.php(5) <?php include("linesfile.php5"); $linesDataFile = new DataFile("data.line");
0
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4. I have GD and freetype libraries. How do I center text within an image if that text contains new lines? According to the imagettftext docs, the x and y specified...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.