eugeniu Posted July 18, 2008 Share Posted July 18, 2008 Is there any way to use GD to put a randomized string of text in the center of a generated png? Right now I have pretty much every thing covered except for centering text... Link to comment https://forums.phpfreaks.com/topic/115338-gd-putting-text-in-the-center-of-the-image/ Share on other sites More sharing options...
MasterACE14 Posted July 18, 2008 Share Posted July 18, 2008 grab the image height in pixels. divide it by 2. grab the image width in pixels. divide it by 2. Now you have the coordinates of the center Regards ACE Link to comment https://forums.phpfreaks.com/topic/115338-gd-putting-text-in-the-center-of-the-image/#findComment-592981 Share on other sites More sharing options...
eugeniu Posted July 18, 2008 Author Share Posted July 18, 2008 But with that, I can only start at the center. I want the center of my text to match the center of my image (200x30). The text width is always different because the text always keeps on changing. Link to comment https://forums.phpfreaks.com/topic/115338-gd-putting-text-in-the-center-of-the-image/#findComment-592983 Share on other sites More sharing options...
xtopolis Posted July 18, 2008 Share Posted July 18, 2008 Possibly build the text as a separate image, find the new width and height of your TEXTONLY image. From there determine the remaining space on your image, (Total width - textonly width) & (total heigh-textonly height). From there you could divide those each by 2, and offset from the center of the image. Meaning paste the textonly image onto the background 200x30 png, and position the text only using the values as coords as I said above. Link to comment https://forums.phpfreaks.com/topic/115338-gd-putting-text-in-the-center-of-the-image/#findComment-592988 Share on other sites More sharing options...
eugeniu Posted July 18, 2008 Author Share Posted July 18, 2008 Seriously?? There is NO alternative? Link to comment https://forums.phpfreaks.com/topic/115338-gd-putting-text-in-the-center-of-the-image/#findComment-592994 Share on other sites More sharing options...
MasterACE14 Posted July 18, 2008 Share Posted July 18, 2008 why would you need a alternative? our 2 suggestions work just fine... Link to comment https://forums.phpfreaks.com/topic/115338-gd-putting-text-in-the-center-of-the-image/#findComment-593005 Share on other sites More sharing options...
eugeniu Posted July 18, 2008 Author Share Posted July 18, 2008 why would you need a alternative? our 2 suggestions work just fine... Your suggestion would make the image like this: | | | hello world| | | Only starting at the center. Link to comment https://forums.phpfreaks.com/topic/115338-gd-putting-text-in-the-center-of-the-image/#findComment-593015 Share on other sites More sharing options...
xtopolis Posted July 18, 2008 Share Posted July 18, 2008 eugeniu, First off: Be grateful people are even trying to help you. No one here is under any obligation to do anything for you. The mere fact that you don't like what we suggest should never get in front of your courtesy and respect to people that try to help you. That being said, having never used GD before, I googled and phpmanualed for an hour and made this: GD centered image Refresh to see different strings. It's kinda funny, I followed my the suggestions by ACE and myself, and it worked out, go figure. All it took was a little elbow grease. Before you complain that this doesn't fit your exact needs, STOP. I am showing you that it can be done at a basic level, any additions you make need to be compensated for accordingly. It's not my job to code for you. I just happen to like challenges, like making this, for instance. relevant code <?php $width = 200; // SET IMAGE WIDTH $height = 30; // SET IMAGE HEIGHT $font_size = 2; // MY FONT SIZE $text_width = imagefontwidth($font_size) * $x; // TEXT WIDTH = FONTWIDTH * AMOUNT OF LETTERS IN STRING ($x = STRLEN($string)); $text_height = imagefontheight($font_size); // TEXT HEIGHT IS JUST THE FONT HEIGHT $im = imagecreate($width, $height);//create base image $bg = imagecolorallocate($im, 255, 255, 255);//white bg for base $txt = imagecolorallocate($im, 75, 75, 75);//black text $offsetH = ((($height-1)-$text_height) / 2); // my font is off by 1 pixel, that's why i have -1. I take the total height(30) -1= 29 - font height // I divide all that by 2, because we only offset from one side $offsetW = (($width-$text_width) / 2); // width is total width (200) subtract width of text put together // divide by two to offset from one side imagestring($im, $font_size, $offsetW, $offsetH, $string, $txt);//add string accordingly imagepng($im,'image.png');//create the image //Die image die. imagedestroy($im); ?> Link to comment https://forums.phpfreaks.com/topic/115338-gd-putting-text-in-the-center-of-the-image/#findComment-593058 Share on other sites More sharing options...
eugeniu Posted July 19, 2008 Author Share Posted July 19, 2008 Thank you, it works now. And sorry for my behavior, I was pretty frustrated with GD then. Link to comment https://forums.phpfreaks.com/topic/115338-gd-putting-text-in-the-center-of-the-image/#findComment-593936 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.