Hello DaniWeb! I know this is my first post, but I don't want to be taken as a "n00b" or whatnot. I know my way around PHP and MySQL fairly well, so to make up for the fact that I'm only 15 and don't know everything yet, I'll try and describe my dilemma to the best of my ability.

I'm trying to make a PHP script, executed as a JPEG (to be embeddable with BBCode) that will return the post count, user group, etc. of a given user out of my forum's database. Unfortunately, this JPEG execution of a PHP script doesn't allow for GET functions to be passed to it, or else it'll break the BBCode. Can anyone offer a solution to this?

Member Avatar for diafol

Are you trying to use the GD2 or ImageMagick libraries to create dynamic images? Sorry, didn't quite get what you're after.

Are you trying to create an image from the text output by the db?

Are you trying to use the GD2 or ImageMagick libraries to create dynamic images? Sorry, didn't quite get what you're after.

I was trying to use gd(confusing), but i've never heard of ImageMagick

Are you trying to create an image from the text output by the db?

Yessir :)

That's weird. Are you sure that you're declaring the variables properly?
I just wrote this simple code, based off the link that ardav gave...

<?php

// Create a new blank image 
$image = imagecreate(200, 30); 

// Assign the background color 
imagecolorallocate($image, 0x00, 0x00, 0xFF); 

// Assign the text color to a variable, to use it later 
$set_text_color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); 

$text1 = $_GET["text1"];
$text2 = $_GET["text2"];

$text = $text1." are ".$text2;

// Write on the image 
imagettftext($image, 14, 0, 5, 20, $set_text_color, '04b24', $text); 

// Assign the correct Header 
header('Content-Type: image/PNG'); 

// Show the picture 
imagepng($image); 

// Destroy the temp file 
imagedestroy($image); 

?>

At the url = "helpperson.php?text1=computers&text2=awesome", you get an image like this:
[img]http://www.pixelpuppy.net/otherfiles/helpperson.php?text1=computers&text2=awesome[/img]
So it should work. :)

Hmm... Is there any way that I could put a forum username into a form and have it create a .jpg file that will be run as a PHP file?

Yep. :)
Just have a form, and make the action "imagecreate.php" or whatever, and that should work. x3 Or you can put it in the post variable, and just show the image. I'd write some code, but I'm sort of busy. Usually a bit of experimenting is enough. ;)

Member Avatar for diafol

The only problem you'll probably face is "font location". You can force GD to look for a particular font-face in a certain folder (if you can copy the ttf file there -but you must have rights to do so - DO NOT copy calibri etc!).

If you're experimenting with GD on a local windows machine try this:

<?php
header("Content-type: image/png"); 
$imgHgt = 100; 
$imgWidth= 400; 
$imgColorHex="487239"; 
$txt = array('This is Text line 1','This is Text line 2','This is Text line 3','This is Text line 4'); 
$fntSize = array(18,14,12,10); 
$fntHex = array('00FF00','FFFFFF','000000','00FF00'); 
$fntShad = array(1,0,0,0);

$fnt = array('arial.ttf','verdana.ttf','SPETETRIAL.ttf','impact.ttf'); 

[B]//THIS IS MY LOCALHOST SETTING
$fontfld = "C:/Windows/Fonts/"; 
//change this or set to nothing ('') when on linux remote.
[/B]
$background = @imagecreate($imgWidth, $imgHgt) or die("Cannot Initialize new GD image stream"); 
$background_color = imagecolorallocate($background, 0, 0, 254); 

$t = count($txt); 
$totY = 5; 
$i = 0; 
while ($i < $t) 
{ 

if($fntShad[$i] == 1) 
{ 
$bbox = imageftbbox($fntSize[$i], 0, $fontfld.$fnt[$i], $txt[$i], array("linespacing" => 1)); 
$width = abs($bbox[0]) + abs($bbox[2]); 
$height = abs($bbox) + abs($bbox[5]); 
$xcoord = (($imgWidth - $width) / 2) + 2; 
$ycoord = $height + $totY + 2; 
$int = hexdec("000000"); 
$arr = array("red" => 0xFF & ($int >> 0x10), 
"green" => 0xFF & ($int >> 0x8), 
"blue" => 0xFF & $int); 
$tcolor = ImageColorAllocate($background, $arr["red"], $arr["green"], $arr["blue"]); 

imagettftext($background, $fntSize[$i], 0, $xcoord, $ycoord, $tcolor, $fontfld.$fnt[$i], $txt[$i]); 
$totY = $ycoord + 5; 
$i++; 
} 
else 
{ 

$bbox = imageftbbox($fntSize[$i], 0, $fontfld.$fnt[$i], $txt[$i], array("linespacing" => 1)); 
$width = abs($bbox[0]) + abs($bbox[2]); 
$height = abs($bbox[1]) + abs($bbox[5]); 
$xcoord = (($imgWidth - $width) / 2) + 2; 
$ycoord = $height + $totY + 2; 
$int = hexdec($fntHex[$i]); 
$arr = array("red" => 0xFF & ($int >> 0x10), 
"green" => 0xFF & ($int >> 0x8), 
"blue" => 0xFF & $int); 
$tcolor = ImageColorAllocate($background, $arr["red"], $arr["green"], $arr["blue"]); 

imagettftext($background, $fntSize[$i], 0, $xcoord, $ycoord, $tcolor, $fontfld.$fnt[$i], $txt[$i]); 
$totY = $ycoord + 5; 
$i++; 
} 
} 
imagepng($background); 
ImageDestroy($background); 
?>

I can't claim to have written this - I think it was from bigresource.com. I can't remember, otherwise I'd give you the url.

Notice the unusual SPETETRIAL.ttf in the font array - this is the actual filename of the stamPete font. Many windows fonts are named differently to the actual filename. Check the fonts properties:

The output from windows localhost can be seen in the second attachment.

Thanks for that... Although I still need a script that will take a user's username and create a PHP script and create a directory <username.jpg> with that script in the directory as index.php... This post might be a bit confusing so I'll elaborate a little later.

Thanks for all your help so far!

Member Avatar for diafol

Just a bit odd. I don't understand why you need to create a folder for the user if you're storing the details in a db and creating the image on the fly every time.

You can pass variables to the image without any problem at all, e.g. from my previous post, I used this to generate the static text:

$txt = array('This is Text line 1','This is Text line 2','This is Text line 3','This is Text line 4');

You could just as easily create this from db row vars:

$txt[0] = stripslashes($row['username']);
$txt[1] = stripslashes($row['user_rank']);
$txt[2] = stripslashes($row['no_posts']);
$txt[3] = stripslashes($row['avatar']);

...etc...

For IMG bbcodes to work, they need to have a proper extension (.jpg, .png, etc.), so you can't pass GET variables to it. I planned on having users generate a signature by typing their username into a form.

Member Avatar for diafol

OK I see. You can make the php script create a file if you want.
However, I'd have an avatars folder and save all the user images to that folder. The files would be named after the user id or at least have the user id in the name somewhere so that it could be referenced.
I assume you're creating some sort of forum/blog/comment board as you're using bbcode. Are the images going to be used in the body of the post or as an user avatar in a side-panel by the post? If the latter, no need for bbcode - but you probably know that.

Yeah, it's for the signature, which I guess could qualify as in-post.

Member Avatar for diafol

Doh! I just read the thread title again. What a fool!

OK, so the bbcode wouldn't allow something like:

[ img ] h t t p : // www . example . com / mypic . php?id=789&ret=90[/url] [ / img ]

Correct

Correct

If you have an apache web server and you have access to its configuration, you could set up a rewriterule, which would convert your request(ie. /myname.jpg) into something like this: /signature.php?username=myname , so it would seem that you're requesting an image file.

I think I'm gonna start out with just creating each file individually and then start screwing with my .htaccess ;) I'm not THAT good with PHP, lol :P

Member Avatar for diafol

Is this for your own forum app (that you're writing) or for use in other people's forums? If your own - you can code whatever you like. If for use in other forums - I understand why you're stuck.

Well, it's for my own forum (IPB) but I'm keeping in mind tht I might port it for someone else's forum someday

Member Avatar for diafol

I must be on drugs or something.

Have you thought about creating a custom BBCODE?

e.g. [sig=text1]text2[/sig] which would translate to <img src="sigimg.php?param1=text1&param2=text2" />

The above is just a simple idea of how this could be implemented.

Member Avatar for diafol

Right, I had a little play about:

<?php
// Create a new blank image 
$image = imagecreate(200, 30); 
 
// Assign the background color 
imagecolorallocate($image, 0x00, 0x00, 0xFF); 
 
// Assign the text color to a variable, to use it later 
$set_text_color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); 
 
$text1 = "BLANK";
$text2 = "BLANK";
 
//IF querystring 
if(isset($_GET['msg1']) && isset($_GET['msg2'])){
	//these should be 'treated' e.g. unencoded
	$text1 = $_GET['msg1'];
	$text2 = $_GET['msg2'];
} 
 
 
$text = $text1. " - ".$text2;

$font = "fts/verdana.ttf"; //this is totally illegal! Use free fonts or server fonts

// Write on the image 
imagettftext($image, 14, 0, 5, 20, $set_text_color, $font, $text); 
 
// Assign the correct Header 
header('Content-Type: image/PNG'); 
 
// Show the picture 
imagepng($image); 
 
// Destroy the temp file 
imagedestroy($image); 
?>

Produced the output (screenshot) on phpBB3 (in preview mode).

In other words in phpBB3 bbcode CAN use querystrings.

Thanks... You're a genius! However, I think I'll be implementing one file per user as I want my users to be able to use this signature on other forums.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.