具有png扩展名的通用动态图像

时间:2012-06-07 05:28:14

标签: php mysql dynamic gd

我正在尝试为论坛生成动态签名。一切都很好,我生成它,但后来我需要获得扩展名为.png的图像,因为目前图像的链接看起来像这样:

这是我的代码,我尝试保存图像,你可以在最后的我的标记行中看到,但我无法显示.png图片。只显示用php生成的那个。我需要的是具有正常扩展但是动态的图像。

我的代码:

<?  


$username="XXX"; //Your MySQL Username. 
$password="XXX"; // Your MySQL Pass. 
$database="XXX"; // Your MySQL database. 
$host="localhost"; // Your MySQL host. This is "localhost" or the IP specified by your hosting company. 

$player_name=$_GET['player_name']; // This gets the player his name from the previous page. 

/* Next, we will make a connection to the mysql.  
If it can't connect, it'll print on the screen: Unable to select database. Be sure the databasename exists and online is. */ 

mysql_connect($host,$username,$password); // Connection to the database. 
@mysql_select_db($database) or die( "Unable to select database. Be sure the databasename exists and online is."); //Selection of the database. If it can't read the database, it'll give an error. 

/* To protect MySQL injection. */ 
$player_name = stripslashes($player_name); 
$player_name = mysql_real_escape_string($player_name); 



$query="SELECT * FROM stats_dm WHERE account='".$player_name."' LIMIT 1;"; // Gets all the information about the player. 
$result=mysql_query($query); 
$i=mysql_num_rows($result); // Here we are counting how many rows this result gives us. 

/* We will now put the player's information into variables so we can use them more easily. */ 
/* DON'T FORGET: The names should be exact the same as in your mysql db.*/ 

if ($i == 1) // If the user has been correct, then it'll give us 1 row. If its 1 row, then it'll proceed with the code. 
{ 

    $Playername=mysql_result($result,0,"lastNick"); // Gets the username of the player and put it in the variable $Playername. 
    $Money=mysql_result($result,0,"Money"); // Gets the money of the player and put it in the variable $Money. 
    $Score=mysql_result($result,0,"points"); // Gets the score of the player and put it in the variable $Score. 
    $Mapsplayed=mysql_result($result,0,"MapsPlayed"); // Gets the played maps. 
    $Wins=mysql_result($result,0,"MapsWon"); // Gets the winned maps. 


    // Creating of the .png image.  
    header('Content-Type: image/png;'); 
    $im = @imagecreatefrompng('signature.png') or die("Cannot select the correct image. Please contact the webmaster."); // Don't forget to put your picture there. 
    $text_color_red = imagecolorallocate($im, 255,0,0); // RED, GREEN, BLUE --> Go to www.colorpicker.com, select a nice color. Copy the R/G/B letters provided by colorpicker and put them here. 
    $text_color_white = imagecolorallocate($im, 255,255,255); 
    $text_color_blue = imagecolorallocate($im, 0,100,255); 
    $text_color_black = imagecolorallocate($im, 30,30,30); 
    //$text_username = $Playername; // This gets the information about player name to be showed in the picture.
    $text_username = preg_replace("/#[a-f0-9]{6}/i", "", $Playername);
    $text_score = $Score; // Same as above ^^ 
    $text_money = $Money; // Same as above ^^ 
    $text_mapsplayed = $Mapsplayed; // Same as above ^^ 
    $text_wins = $Wins; // Same as above ^^


    // Set the enviroment variable for GD
    putenv('GDFONTPATH=' . realpath('.'));

    $font = 'impact'; //Upload your custum font to the directory where this file is placed. Then change the name here. 

    $dimensions = imagettfbbox(20, 0, $font, $text_username);
    $textWidth = abs($dimensions[4] - $dimensions[0]);
    $x = imagesx($im) - $textWidth;
    /* USAGE OF THE imagettftext: First ($im) shouldn't be changed. (16) is the text-size. (0) is the angle of your text. Change it, and you'll see what's going on. (20) is de X-coordinate of the text. 
    (36) is the Y-coordinate of the text. */ 
    //imagettftext($im, 16, 0, 230, 74, $text_color_red, $font, $text_username); // Prints the username in the picture.  
    //imagettftext($im, 14, 0, 301, 33, $text_color_blue, $font, $text_score); // Prints the score shadow in the picture.
    //imagettftext($im, 14, 0, 300, 32, $text_color_white, $font, $text_score); // Prints the score in the picture. 
    imagettfstroketext($im, 14, 0, 300, 33, $text_color_white, $text_color_blue, $font, $text_score, 1); // Print pretty points
    imagettftext($im, 10, 0, 50, 112, $text_color_white, $font, $text_money . "$"); // Prints the money in the picture. 
    imagettftext($im, 10, 0, 224, 112, $text_color_white, $font, $text_mapsplayed); // Prints maps played. 
    imagettftext($im, 10, 0, 328, 112, $text_color_white, $font, $text_wins); // Prints wins. 
    imagettfstroketext($im, 20, 0, $x, 74, $text_color_white, $text_color_black, $font, $text_username, 1);
    //imagepng($im); 
    //imagepng($im, "/tmp/hedede.png");
    //$save = strtolower($player_name) .".png";
    //imagepng($im, $save);
    //imagedestroy($im); 
    imagepng($im);
    //$save = strtolower($player_name) .".png";
    //chmod($save,0755);
    //imagepng($im, $save, 0, NULL);
    imagedestroy($im);
} else echo('Username is not in our database. Please try again.'); // If the username doesn't exist (so the row is 0) then it'll give en error. 

mysql_close(); 


function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {

    for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
        for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
            $bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);

   return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}

?>

1 个答案:

答案 0 :(得分:0)

使用.htaccess

mod_rewrite文件

有些事情应该这样做:

RewriteEngine on  
RewriteRule /([0-9a-z]+)\.png /yourscript.php?img=$1

然后你可以将人们指向png,它将被重定向到php脚本。

foobar.png会重定向到yourscript.php?img=foobar