Jump to content

image generation (fonts)


Dusaro

Recommended Posts

I have set this up with a form on another page so that they can generate a runescape stat signature, although there is something wrong, if they do not select a font it comes up with an error.

 

Here is the Code:

<?php

header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache");
$user = "";
$img = ""; 
$fnt = ""; 

if(isset($_GET['user']))
{
if(!empty($_GET['user']))
{
	$user = $_GET['user'];
}
}

if(isset($_GET['img']))
{
if(!empty($_GET['img']))
{
$img = $_GET['img'];
}
else{$img='blue';}
}
else{$img='blue';}

if(isset($_GET['fnt']))
{
if(!empty($_GET['fnt']))
{
$fnt = $_GET['fnt'];
}
else{$fnt='arial';}
}
else{$img='blue';}

if(isset($_POST['user'],$_POST['img'],$_POST['fnt']))
{
if(!empty($_POST['user']))
{
$user=$_POST['user'];
}
if(!empty($_POST['img']))
{
$img = $_POST['img'];
}
if(!empty($_POST['fnt']))
{
$fnt=$_POST['fnt'];
}
}


//Skill Grabs
$order = array("Overall", 
                        "Attack", 
                        "Defence", 
                        "Strength", 
                        "Hitpoints", 
                        "Ranged", 
                        "Prayer", 
                        "Magic", 
                        "Cooking", 
                        "Woodcutting", 
                        "Fletching", 
                        "Fishing", 
                        "Firemaking", 
                        "Crafting", 
                        "Smithing", 
                        "Mining", 
                        "Herblore", 
                        "Agility", 
                        "Thieving", 
                        "Slayer", 
                        "Farming", 
                        "Runecraft", 
                        "Hunter", 
                        "Construction", 
                        "Summoning",
                        "Dungeoneering"); 

$get = file_get_contents("http://hiscore.runescape.com/index_lite.ws?player=$user");
$get = explode("\n", $get);
$i = 0;

foreach ($order as $key => $value)
{
        $value = strtolower($value);
        $temp = explode(",", $get[$i]);
        $temp = array("rank" => $temp[0], "level" => $temp[1], "exp" => $temp[2]);
        $stats[$value] = $temp;
        $eval = "\$$value = array(\$temp[\"rank\"], \$temp[\"level\"], \$temp[\"exp\"]);";
        eval($eval);
        $i++;
}

//End Skill Grabs


$image = imagecreatefrompng("http://slay2day.x10.mx/highscores/signatures/".$img.".png");
$font = "/fonts/".$fnt.".ttf";

$color = imagecolorallocate($image, 255,255,255);

ImageTTFText ($image, "7", 0, 280, 10, $color, "/fonts/arial.ttf","Slay2day");

ImageTTFText ($image, "12", 0, 240, 55, $color, $font,$user);
if(is_numeric($overall[0]))
{
if($overall[0]==-1)
{
ImageTTFText($image, "11", 0, 230, 105, $color, $font,"2000000+");
}
else
{
ImageTTFText($image, "11", 0, 240, 105, $color, $font,$overall[0]);
}
}
else
{
ImageTTFText($image, "11", 0, 230, 105, $color, $font,"2000000+");
}
ImageTTFText ($image, "10", 0, 27, 20, $color, $font,$attack[1]);
ImageTTFText ($image, "10", 0, 27, 42, $color, $font,$strength[1]);
ImageTTFText ($image, "10", 0, 27, 64, $color, $font,$defence[1]);
ImageTTFText ($image, "10", 0, 27, 88, $color, $font,$hitpoints[1]);
ImageTTFText ($image, "10", 0, 27, 114, $color,$font,$ranged[1]);

ImageTTFText ($image, "10", 0, 70, 20, $color, $font,$prayer[1]);
ImageTTFText ($image, "10", 0, 70, 42, $color, $font,$magic[1]);
ImageTTFText ($image, "10", 0, 70, 64, $color, $font,$cooking[1]);
ImageTTFText ($image, "10", 0, 70, 88, $color, $font,$woodcutting[1]);
ImageTTFText ($image, "10", 0, 70, 114, $color,$font,$fletching[1]);

ImageTTFText ($image, "10", 0, 117, 20, $color, $font,$fishing[1]);
ImageTTFText ($image, "10", 0, 117, 42, $color, $font,$firemaking[1]);
ImageTTFText ($image, "10", 0, 117, 64, $color, $font,$crafting[1]);
ImageTTFText ($image, "10", 0, 117, 88, $color, $font,$smithing[1]);
ImageTTFText ($image, "10", 0, 117, 114, $color,$font,$mining[1]);

ImageTTFText ($image, "10", 0, 162, 20, $color, $font,$herblore[1]);
ImageTTFText ($image, "10", 0, 162, 42, $color, $font,$agility[1]);
ImageTTFText ($image, "10", 0, 162, 64, $color, $font,$thieving[1]);
ImageTTFText ($image, "10", 0, 162, 88, $color, $font,$slayer[1]);
ImageTTFText ($image, "10", 0, 162, 114, $color,$font,$farming[1]);

ImageTTFText ($image, "10", 0, 212, 20, $color, $font,$runecraft[1]);
ImageTTFText ($image, "10", 0, 212, 42, $color, $font,$construction[1]);
ImageTTFText ($image, "10", 0, 212, 64, $color, $font,$hunter[1]);
ImageTTFText ($image, "10", 0, 212, 88, $color, $font,$summoning[1]);
ImageTTFText ($image, "10", 0, 212, 114, $color,$font,$dungeoneering[1]);

header("Content-type: image/png");
imagepng($image);
imagedestroy($image);

//Logging
$url = getenv("HTTP_REFERER"); 

// connect to the database
include('connect-db.php');

//variables
date_default_timezone_set('Pacific/Auckland');
$datepickerbox = $_POST['datepickerbox'];
$datepickerbox = date("Y-m-d H:i:s", time($datepickerbox));
echo $datepickerbox;

// save the data to the database
mysql_query("INSERT signature SET user='$user', img='$img', time='$datepickerbox', url='$url', font='$fnt'")
or die(mysql_error()); 
//End Logging

?>

 

if they do not have a font i get:

Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/slay2day/public_html/highscores/signatures/signature.php on line 130

 

but of they dont choose a font i want it to use arial...

Link to comment
Share on other sites

Your logic really needs some cleaning up.

 

You have this block which will set the default font if the font parameter is passed through the GET data.

if(isset($_GET['fnt']))
{
if(!empty($_GET['fnt']))
{
$fnt = $_GET['fnt'];
}
else{$fnt='arial';}
}

 

Then you have this block of code which defined font based upon POST data

if(isset($_POST['user'],$_POST['img'],$_POST['fnt']))
{
if(!empty($_POST['user']))
{
$user=$_POST['user'];
}
if(!empty($_POST['img']))
{
$img = $_POST['img'];
}
if(!empty($_POST['fnt']))
{
$fnt=$_POST['fnt'];
}
}

 

So, if the font variable is passed through POST data - but is empty - $fnt never gets defined. So, I have to assume that it is only passed through POST data (or perhaps either)? Why don't you just define the default when you first define $fnt at the top of the page?

$user = "arial";
$img = ""; 
$fnt = ""; 

 

In fact, you could do the same for the $img which seems it should have a default of "blue"

Link to comment
Share on other sites

Here is what I would use to define those three variables that should make the logic much easier

//Create function to get value from GET or POST
function getVarFromPostOrURL($var_name, $default=false)
{
    //Reverse if and elseif conditions based upon
    //whether POST or GET should take precedence
    $value = '';
    if(isset($_POST[$var_name]))
    {
        $value = trim($_POST[$var_name]);
    }
    elseif($_GET[$var_name]))
    {
        $value = trim($_GET[$var_name]);
    }
    return (!empty($value)) ? $value : $default;
}

$user = getVarFromPostOrURL('user');
$fnt  = getVarFromPostOrURL('fnt', 'arial');
$img  = getVarFromPostOrURL('img', 'blue');

if(!$user)
{
    exit('No user data available!');
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.