473,779 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4744
On 6 Jun, 05:34, -Lost <maventheextraw o...@techie.com wrote:
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 <maventheextraw o...@techie.com wrote:
>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 <maventheextraw o...@techie.com wrote:
>>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 <maventheextraw o...@techie.com wrote:
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.comwro te:
On Thu, 07 Jun 2007 19:24:39 +0200, -Lost <maventheextraw o...@techie.com >
wrote:
-Lost wrote:
gbbulldog wrote:
On 6 Jun, 05:34, -Lost <maventheextraw o...@techie.com wrote:
Anyone know offhand how to center atextwithin the bounding box of an
image(widt h-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 = imagecreatetrue color(500,500);
$point_array = imagettftext($i m_tmp,
20,0,0,50,image colorallocate($ im0,0,0),'C:\Wi ndows\Fonts\Ver dana.ttf',
$text);
imagedestroy($i m_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**********@g mail.com
wrote:
On Jun 7, 7:49 pm, Rik <luiheidsgoe... @hotmail.comwro te:
>>>Anyone know offhand how to center atextwithin the bounding box of
an
>>>>image(wid th-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
dynamictextwit h 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($i m_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
1533
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 appreciated, micha
2
2929
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 a WebForm with some textboxes, dropdownlists, a panel, imagebutton and so on. When I click on the image button (which was created at design time) I dynamically build a table. In each of row of that new table I put several cells and one cell...
1
3414
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 value to the td.innerHTML property. The UI is done, and I now want to post back the user's changes and update my business object in .NET. But when I postback, I can't see any of my dynamically created HTML controls in VB .NET. How do I make them...
10
2583
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 is that it's not destroyed, but I'm not sure. I have the following program: #include<iostream>
1
2002
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 calendar. But the problem is that when I click on the button to create the image it automatically opoup the calendar before creating the image and giving an error "not Implemented" at newimg.onclick. I'll really appreciate if anyone can help here...
1
3437
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 foreach (Product p in manager.Products) { ASP.MasterDetailDropDown productMasterDetail; productMasterDetail = (ASP.MasterDetailDropDown)LoadControl("~/controls/MasterDetailDropDown.ascx"); ...
1
1768
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
1085
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 represent the coordinates of where the first character begins. But if there are new lines under it, I need to adjust that x and y by a certain amount. What to do? - Dave
1
4913
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 set text boxes and labels inside the table rows. I then added a button. All of these are done through code. The problem that i am having is i can get the value from a text box with resides inside the first panel (out side of panel that is...
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.