AAO25.com
Community => Games & Programming => Topic started by: -Delta- on Monday, November 29, 2010, 13:43:01 PM
-
My PHP code isn't working and I don't know why. :'(
<?php
function StatsToImage($username)
{
$font = './fonts/times.ttf'; // Font to use to generate the image.
$fontSize = 8; // font size.
$transparent = true;
$fg_color = array(0, 0, 0);
$bg_color = array(255, 255, 255);
$msg = "";
$Points = "";
$Rank = "";
$lines = file('http://www.worldcommunitygrid.org/getDynamicImage.do?memberName='. $username. '&mnOn=true&stat=1&imageNum=1&rankOn=true&projectsOn=false&special=false');
foreach ($lines as $line_num => $line) {
$pos = strripos(htmlspecialchars($line), ";Points");
if ($pos) {
$Points = $line;
$pos = strripos($Points , "&");
$Points = substr($Points, 0, $pos);
$Points = trim($Points, " \t.");
}
$pos = strripos(htmlspecialchars($line), "(Rank");
if ($pos) {
$Rank = $line;
$pos = strripos($Rank , ";");
$Rank = substr($Rank, $pos+1, strlen($Rank));
$pos = strripos($Rank , ")");
$Rank = substr($Rank, 0, $pos);
$Rank = trim($Rank, " \t.");
}
}
$msg = "Username: ". $username. " | Points: ". $Points. " | Rank: ". $Rank;
$width = 0;
$height = 0;
$image = null;
// Determine font height
$bounds = imagettfbbox(8, 0, $font, "W");
echo $msg;
$font_height = abs($bounds[7]-$bounds[1]);
// Determine image size.
$bounds = ImageTTFBBox($fontSize, 0, $font, $msg);
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$image = imagecreate($width+1, $height+1); // Initialize type image
$background = ImageColorAllocate($image, $bg_color[0], $bg_color[1], $bg_color[2]);
$foreground = ImageColorAllocate($image, $fg_color[0], $fg_color[1], $fg_color[2]);
if ($transparent) {
ImageColorTransparent($image, $background);
}
// Build image from text.
ImageTTFText($image, $fontSize, 0, 0, $font_height, $foreground, $font, $msg);
// Save PNG to file.
imagepng($image, "stats.png");
}
StatsToImage("computerguy89"); // Call function
?>
It seems to crash right around when "imagettfbbox" is called, but php won't give me any debug information. This is also the same code I was using before, and nothing has changed. So, it doesn't really make sense that it is not working.
-
Well....
http://natescomp.com/delta.php
It looks to work but not use font. I changed the font to the one used for PostQuote sigs. Maybe I have the directory structure wrong, I don't know. What I do know is that for things like imagettfbbox and imagecolorallocate, don't capitalize anything. I also don't think you need the call function and a simple rewrite can fix that. I can look more into it later if you want.
*EDIT*
I also don't like $image = null; it just doesn't seem right.
-
Your not calling the headers in order to have an image.
-
Thanks for your help guys. When I get time to work on it I'll try what you said.
-
Delta, if you want, you can scavenge the code for PostQuote. Both me and Jared have contributed to it so keep that in mind if you share it with other people :)
<?php
$db = mysql_connect("localhost", "xxxxxxxxxxx", "xxxxxxxxxxx") or die ('There was a problem connecting to the database.');
mysql_select_db("natcom_quotes") or die ('There was a problem selecting database table.');
header("Content-type: image/png");
// Update Signatures Served
$sql = "SELECT * FROM signatures";
$rs_result = mysql_query ($sql);
while ($row = mysql_fetch_assoc($rs_result)) {
$sigtotal = $row['total'];};
$sigtotaladd = $sigtotal+1;
mysql_query("UPDATE signatures SET total='".$sigtotaladd."' WHERE total='".$sigtotal."'");
$urluser = $_GET['u'];
$sql_events = mysql_query("SELECT * FROM quotes ORDER BY servtime DESC limit 1")
or die(mysql_error());
while ($row = mysql_fetch_array($sql_events))
{
$username = $row['username'];
$quote1 = htmlspecialchars_decode($row['quote']);
$quote2 = mb_substr($quote1, 0, 500,'UTF-8');
//
$order = array("\r\n", "\n", "\r");
$replace = "\n";
// Processes \r\n's first so they aren't converted twice.
$quote = str_replace($order, $replace, $quote2);
$servtime = $row['servtime'];
$by = htmlspecialchars_decode($row['by']);
$id = $row['id'];
$ratingup = $row['voteup'];
$ratingdown = $row['votedown'];
}
mysql_close($db);
$image = imagecreatefrompng("sigbackground.png");
imagealphablending($image, true);
imagesavealpha($image, true);
$font = "./verdana.ttf";
//imagecolorallocate($image, R, G, B) in HEX values
$font_black = imagecolorallocate($image, 2, 1, 8);
//($image, fontsize, rightindent, downindent, data, txtcolour)
imagettftext($image, 8, 0, 5, 10, $font_black, $font, "Newest quote posted on PostQuote.us:");
imagettftext($image, 8, 0, 352, 10, $font_black, $font, "ID: $id");
/////
$srt = wordwrap($quote, 68, "\n");
$lines = explode("\n", $srt);
$y = 24;
$i = 0;
foreach($lines as $line=>$string){
imagettftext($image, 8, 0, 5, $y, $font_black, $font, $string);
$string = substr($string, 1);
$y += 12;
if(++$i == 6) break;
}
/////
// Display the rest of the info
imagettftext($image, 8, 0, 5, 97, $font_black, $font, "-$by");
imagettftext($image, 8, 0, 200, 112, $font_black, $font, $ratingup);
imagettftext($image, 8, 0, 240, 112, $font_black, $font, $ratingdown);
imagepng($image);
imagedestroy($image);
?>
You save that as index.php in a directory called "sig.png". This way whenever a browser asks for http://myhost.com/sig.png they actually get http://myhost.com/sig.png/index.php useful for forums and whatnot :)