Jump to content

[SOLVED] How would I Do This?


marcus

Recommended Posts

In terms of advertising a website using a banner and referral link.

 

I've written the mod rewrite for this.

 

My problem is: How would I go about using a premade banner and printing unique text on it for each users banner.

 

So:

 

http://website.org/userpics/marcus.png

 

Say I had: 800 forum posts

I wanted it to say: Username: marcus // Forum Posts: 800

 

Doing this by a SQL query.

 

$username = $_GET['username'];
protect($username); //pre-made function to protect variable
$sql = "SELECT * FROM `users` WHERE `username` ='$username'"; //username being marcus, atm
$res = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($res) == 0){
$text = "This user does not exist!";
}else {
$sql2 = "SELECT * FROM `replies` WHERE `username` ='$username'";
$res2 = mysql_query($sql2) or die(mysql_error());
$num = mysql_num_rows($res2);
$text = "Username: $username // Forum Posts: $num";
}

 

RewriteRule ^userpic/([^/]*)\.png$ /userpics.php?username=$1 [L]

 

How would I go about (probably using GD) to make the text ($text) print on the image?

Link to comment
https://forums.phpfreaks.com/topic/47601-solved-how-would-i-do-this/
Share on other sites

Ha. It looks very simple, but it looks confusing.

 

Is it possible to load an external image?

 

Yes, all you need to do is pass it an image resource created from the image. For a jpg image you could do this:

<?php

$image_resource = imagecreatefromjpeg ( 'filename/to/image.jpg' );
$text = "Hello World";

$textcolor = imagecolorallocate($im, 0, 0, 255);
$font = 5; // research imageloadfont() for options
$x_pos = 10;
$y_pos = 10;

imagestring($image_resource, $font, $x_pos, $y_pos, $text, $textcolor);

?>

<?php

function protect($input)
{
$escaped_input = mysql_real_escape_string(urldecode($_POST['input']));
// $sql = "INSERT INTO table VALUES ('$escaped_input')";
$input = mysql_real_escape_string($input);
$input = eregi_replace("%","",$input);
$input = eregi_replace("--","",$input);
$input = htmlspecialchars(mysql_real_escape_string($input));

return $input;
}
$username = $_GET['username'];
protect($username);
$font = $_GET['font'];
protect($font);
$color = $_GET['color'];
protect($color);
$sql = "SELECT * FROM `users` WHERE `username` ='$username'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
$text = "This user does not exist!";
}else {
$row = mysql_fetch_assoc($res);
$text = "Username: $username // Forum Posts: $row[post_count]";
}
// create a 100*30 image
$im = imagecreate(500, 98);

// white background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$array2 = array('black','red','blue','green','purple','grey');
if(in_array($color,$array2)){

switch($color){
case black:
$textcolor = imagecolorallocate($im, 0, 0, 0);
break;
case red:
$textcolor = imagecolorallocate($im, 255, 0, 0);
break;
case blue:
$textcolor = imagecolorallocate($im, 0, 0, 255);
break;
case green:
$textcolor = imagecolorallocate($im, 0, 128, 0);
break;
case purple:
$textcolor = imagecolorallocate($im, 128, 0, 128);
break;
case grey:
$textcolor = imagecolorallocate($im, 128,128,128);
break;
}
}else {
$textcolor = imagecolorallocate($im,0,0,0);
}

// write the string at the top left
$array = array(1,2,3,4,5);
if(in_array($font,$array)){
switch($font){
case 1:
$font = imageloadfont("andale12.gdf");
break;
case 2:
$font = imageloadfont("bmreceipt.gdf");
break;
case 3:
$font = imageloadfont("8x13iso.gdf");
break;
case 4:
$font = imageloadfont("bmcorrode.gdf");
break;
case 5:
$font = imageloadfont("bettynoir.gdf");
break;
}
}else {
$font = imageloadfont("andale12.gdf");
}
imagestring($im,$font, 5, 10, $text, $textcolor);

// output the image
header("Content-type: image/png");
imagepng($im);
?> 

 

This is my current file.

 

How would I be able to put a break in the text?

for multiple lines of text in an image

 

<?php
$text = "Line1\nLine2";
$vspace = 20;
$lines = explode("\n", $text);
$x = 10;
$y = 10;
foreach ($lines as $line) {
    imagestring($im, 5, $x, $y, $line, $textcolor);
    $y += $vspace;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.