Jump to content

Help With Php Gd Font Change


toxic27
 Share

Recommended Posts

hey guys, i have this:<?$text = stripslashes($_GET['text']);header ("Content-type: image/png");$string = $text;$font = 2;$width = ImageFontWidth($font)* strlen($string) ;$height = ImageFontHeight($font) ;$im = ImageCreateFrompng("./gold.png");$x=imagesx($im)-$width ;$y=imagesy($im)-$height;$background_color = imagecolorallocate ($im, 255, 255, 255);$text_color = imagecolorallocate ($im, 255, 255, 255);imagestring ($im, $font, 52, 27, $string, $text_color);imagepng ($im);?>it pretty much just puts text on an image but i need to know how to change the font of the text (to like Arial or something). any help is appreciated!

Link to comment
Share on other sites

I don't think any GD functions have uppercase in them...Anyway, the answer is to use the imagettftext() or imagepstext() functions (depending on what font you want to use). Bear in mind you need additional extensions for these to work.

Link to comment
Share on other sites

I don't think any GD functions have uppercase in them...Anyway, the answer is to use the imagettftext() or imagepstext() functions (depending on what font you want to use). Bear in mind you need additional extensions for these to work.
Yeah, I saw those but was confused. I didn't understand why you need extensions to get fonts for that but not to get fonts for other code (say for instance a <p>'s font).Anyways, what extensions do I need?
Link to comment
Share on other sites

You need the FreeType library. It's because GD wasn't designed with text in mind, so it didn't include font handling natively.If you get fonts client-side then the browser handles it.

Link to comment
Share on other sites

I didn't understand why you need extensions to get fonts for that but not to get fonts for other code (say for instance a <p>'s font).
Because web browsers are configured to use certain fonts, and they use the OS's font system to render everything. The server needs a way to render the fonts also, so that's what the library does. The browser already has a way to render fonts.In addition to the library, you also need the font file. You'll find all the fonts that Windows uses in the Windows\fonts directory. Any file in there that ends with .ttf you can load using the truetype functions, such as imagettftext. You'll need to distribute the font files with your PHP script also.
Link to comment
Share on other sites

I hate the learning process for PHP so much. lolWell, I downloaded the FreeType and put it on my server along with pulling a font from my computer (not sure if this is needed) and put it on my server.Now I changed the file that's trying to use it to this:<?$text = stripslashes($_GET['text']);header ("Content-type: image/png");$string = $text;$font = 2;$font_type = 'arial.ttf';$width = ImageFontWidth($font)* strlen($string) ;$height = ImageFontHeight($font) ;$im = ImageCreateFrompng("./gold.png");$x=imagesx($im)-$width ;$y=imagesy($im)-$height;$background_color = imagecolorallocate ($im, 255, 255, 255);$text_color = imagecolorallocate ($im, 255, 255, 255);imagettftext($im, $font, 0, $text_color, $font_type, $string);imagepng ($im);?>It still doesn't work. I really don't know what I'm doing wrong so help is appreciated.. again.Here's the link to the file: http://www.kingps3.com/ct/trophy.php?text=This%20is%20text!Oh, and as you can see at the URL, the image is being cancelled for some reason. The path for it is fine though

Edited by toxic27
Link to comment
Share on other sites

Nevermind, got it figured out. Had to repeat the function twice in the code; one for the picture and one for the text.Thanks for all the help guys! Couldn't of done it without you!

Edited by toxic27
Link to comment
Share on other sites

Oh, and one more minor thing. I'm wondering if it's possible to make the image come out as a .GIF or .PNG image instead of a .PHP image. It might help you if you actually check out the feautre thing I made. Would it be possible to make it a .GIF or .PNG image instead?The feature: http://www.kingps3.com/ct/main.php

Link to comment
Share on other sites

I don't know what you mean by a .php image, .php files are not images, they're code. You use the imagepng, imagejpeg, imagegif etc functions to output as various image types. The image you are outputting is a PNG image.

Link to comment
Share on other sites

I don't know what you mean by a .php image, .php files are not images, they're code. You use the imagepng, imagejpeg, imagegif etc functions to output as various image types. The image you are outputting is a PNG image.
Well, would there be away to turn that code into a .PNG image?The reason I'm looking to do this is because there is a site's forums I'm trying to make an embed code to and since the file (http://www.kingps3.com/ct/trophy.php?type=b&text=test) ends in .PHP, it doesn't allow it. How would I make it to .PNG or .GIF or whatever?If you need more info, just say.Thanks. Edited by toxic27
Link to comment
Share on other sites

One way is to use htaccess to redirect requests for a certain PNG file to your PHP script. So even though the URL is pointing to a PNG file, it would actually be a PHP file that handles it on the server. For information about that, look up URL rewriting. Another way is set up the server to handle all PNG files as PHP files, but that's probably not the best idea.

Link to comment
Share on other sites

Alright, is there a way to integrate PHP into the .htaccess file? The PHP file with the "image code" in it has some.. umm.. I forget what it is called. Here's an example:http://www.kingps3.com/ct/trophy.php?type=b&text=Text goes here!Whatever that is. I forgot what it is called haha. Yeah well is there a way to pass that info through the .htaccess file and to the actual .PHP "image code" file?

Link to comment
Share on other sites

That's the querystring. You can capture it using RewriteCond, then use the %n variable to access it, e.g.

RewriteCond %{QUERY_STRING} (.*)

Edited by Synook
Link to comment
Share on other sites

I don't use htaccess a lot, but it looks like all you really need to do is change the extension. So you would change this link:http://www.kingps3.com/ct/trophy.png?type=b&text=Text goes here!to this:http://www.kingps3.com/ct/trophy.php?type=b&text=Text goes here!Like I said, I haven't used htaccess a lot, but you should be able to find a lot of information online about working with it.

Link to comment
Share on other sites

Alright, thanks, is there anyway you could write it for me? I know it sounds lazy but I literally searched all over Google for more on this and nothing worked.All I need is for 'http://www.kingps3.com/ct/trophy.png?type=b&text=abcdef' to be redirected to this 'http://www.kingps3.com/ct/trophy.php?type=b&text=abcdef'.BTW, justsomeguy, that wouldn't work because the variable changes everytime. It's for a feature that users on my site can use in which they pick what the 'text=' equals so I couldn't just put one statement (like 'Text goes here!') for it.Thanks for helping guys

Edited by toxic27
Link to comment
Share on other sites

  • 1 month later...
I hate the learning process for PHP so much. lolWell, I downloaded the FreeType and put it on my server along with pulling a font from my computer (not sure if this is needed) and put it on my server.Now I changed the file that's trying to use it to this:<?$text = stripslashes($_GET['text']);header ("Content-type: image/png");$string = $text;$font = 2;$font_type = 'arial.ttf';$width = ImageFontWidth($font)* strlen($string) ;$height = ImageFontHeight($font) ;$im = ImageCreateFrompng("./gold.png");$x=imagesx($im)-$width ;$y=imagesy($im)-$height;$background_color = imagecolorallocate ($im, 255, 255, 255);$text_color = imagecolorallocate ($im, 255, 255, 255);imagettftext($im, $font, 0, $text_color, $font_type, $string);imagepng ($im);?>It still doesn't work. I really don't know what I'm doing wrong so help is appreciated.. again.Here's the link to the file: http://www.kingps3.com/ct/trophy.php?text=This%20is%20text!Oh, and as you can see at the URL, the image is being cancelled for some reason. The path for it is fine though
In this example, is 'arial.ttf' in the same directory as this script? When you download fonts, do they come in .ttf files?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...