Forum Moderators: coopster

Message Too Old, No Replies

Using php to create image and save in Mysql

Using php to create image and save in Mysql

         

digi_duck

9:06 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



Hi

I have a script that lets users pick an image and add text to it using this php script:


<?php
header("Content-type: image/jpeg");

$name = stripslashes($_GET['name']);

$size = stripslashes($_GET['size']);

$centre = stripslashes($_GET['centre']);

$font = 'images/sig_maker/fonts/'.stripslashes($_GET['font']).'.ttf';

$fontcolor['r'] = stripslashes($_GET['color_r']); // font color - RED

$fontcolor['g'] = stripslashes($_GET['color_g']); // font color - GREEN

$fontcolor['b'] = stripslashes($_GET['color_b']); // font color - BLUE

$lines = stripslashes($_GET['lines']);

function arrow($im, $x1, $y1, $x2, $y2, $alength, $awidth, $color){
///

}

if(is_numeric($_GET['color']) && $_GET['color'] >= '1' && $_GET['color'] <= '54')
{
$bgpic = 'images/sig_maker/' . $_GET['color'] . '.jpeg';
}

$im = imagecreatefromjpeg($bgpic);

//Calculate, the centre:

for(;;){

list($image_width, $image_height) = getimagesize($bgpic);
list($left_x, , $right_x) = imagettfbbox($size, 0, $font, $name);
$text_width = $right_x - $left_x;
if($image_width > $text_width+5){

break;

}

$size = $size - .5;
if($size == 1){
die('Script not responding to decreasing font size, in other words: try using less letters.');
}

}
$hpadding = ($image_width - $text_width)/2;
$vpadding = ($image_height/2);

$textcolor = imagecolorresolve($im, $fontcolor['r'], $fontcolor['g'], $fontcolor['b']);
if($centre== 'y'){

imagettftext($im, $size, 0, $hpadding,$vpadding, $textcolor, $font, $name);

}else{

imagettftext($im, $size, $angle, $x, $y, $textcolor, $font, $name);

}

imagegif($im);
imagedestroy($im);
?> [/php]

I would like to add a feature that would show the users on the home page the last 5 sigs that have been created. I have limited php knowledge and no mysql knowledge so please be detailed.

I have been trying to do this for about a week and haven't been successful. I've tried adding code suvh as this after the above code but it didnt work.

[php]//
//Insert image into database

mysql_connect('example.com','username','') or die( 'Unable to connect to Mysql');
mysql_select_db("joshuacottom_db") or die( "Unable to select database");
mysql_query($query);
$img = "<img src=\"creation.php?color={$_GET[color]}&amp;name={$_GET[name]}
&amp;centre={$_GET[centre]}&amp;x={$_GET[x]}&amp;y={$_GET[y]}&amp;angle=$_GET[angle]
&amp;font={$_GET[font]}&amp;size={$_GET[size]}&amp;color_r={$_GET[color_r]}
&amp;color_g={$_GET[color_g]}&amp;color_b={$_GET[color_b]}\" />'>";
mysql_query("INSERT INTO sig(image) VALUES('".$img."') ");

//show signature

$query = mysql_query("SELECT * FROM sig");
while($a = mysql_fetch_assoc($query)
$img_path = $a['image'];
echo $img."<BR>";
}

mysql_close();
?>

I am using phpmyadmin to create the tables etc but haven't got a clue what im doing.

PLEASE HELP!

[edited by: eelixduppy at 11:13 pm (utc) on Mar. 30, 2007]
[edit reason] no URLs, please [/edit]

mcibor

2:29 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the creation.php:
...
imagegif($im);
imagedestroy($im);

//Insert image information into database. It's not an image you are inserting, just information on how to create it

mysql_connect('example.com','username','') or die( 'Unable to connect to Mysql');
mysql_select_db("joshuacottom_db") or die( "Unable to select database");
$img = "color={$_GET[color]}&name={$_GET[name]}
&centre={$_GET[centre]}&x={$_GET[x]}&y={$_GET[y]}&angle=$_GET[angle]
&font={$_GET[font]}&size={$_GET[size]}&color_r={$_GET[color_r]}
&color_g={$_GET[color_g]}&color_b={$_GET[color_b]}";
$sql = "INSERT INTO sig(user, image) VALUES('$username', '".mysql_real_escape_string($img)."')";
mysql_query($sql) or die("Error: ".mysql_error()."<br>SQL: $sql");
//it's good to differentiate the users. Otherwise you could only show last 5 created images.
//image field in sig table should be varchar.
mysql_close();
?>

And in the file, where you want to show the pictures:

//show signature
<?php
mysql_connect('example.com','username','') or die( 'Unable to connect to Mysql');
mysql_select_db("joshuacottom_db") or die( "Unable to select database");

$sql = "SELECT * FROM sig WHERE user='$username' LIMIT 5";
$query = mysql_query($sql) or die("Error: ".mysql_error()."<br>SQL: $sql");
while($a = mysql_fetch_assoc($query)
$img_path = $a['image'];
echo "<img src=\"creation.php?$img_path\"><br>";
}

mysql_close();
?>

hope this helps
Michal

PS. You couldn't put that into one file, cause in creation.php you send headers showing that it's a picture, so no <img is allowed inside it.

digi_duck

6:02 pm on Apr 13, 2007 (gmt 0)

10+ Year Member



it showed this error message on the page i tried to show the sigs on

Parse error: parse error, unexpected T_VARIABLE in /home/www/example.co.uk/testcreated.php on line 7

Also I dont want people to have to log in so is there a way of using a person ip address instead of login?

[edited by: eelixduppy at 7:08 pm (utc) on April 13, 2007]
[edit reason] exemplified error [/edit]

mcibor

12:58 pm on Apr 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, my mistake - I didn't close the brackets. It should be
//show signature
<?php
mysql_connect('example.com','username','') or die( 'Unable to connect to Mysql');
mysql_select_db("joshuacottom_db") or die( "Unable to select database");

$sql = "SELECT * FROM sig WHERE user='$username' LIMIT 5";
$query = mysql_query($sql) or die("Error: ".mysql_error()."<br>SQL: $sql");
while($a = mysql_fetch_assoc($query)){
$img_path = $a['image'];
echo "<img src=\"creation.php?$img_path\"><br>";
}

mysql_close();
?>

To discern users by IP you can use theoretically
$ip = $_SERVER['REMOTE_ADDR'];

but better, with proxy will be a function:

function true_IP(){

if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
//now check if everything is correct
$pattern = '@^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$@';
//it's not a very good pattern, but will disable all hacking attempts.
if(preg_match($pattern, $ip)) return $ip;
else return 0;
}


Hope this helps you

to call this function:
%ip = true_IP();

Michal

digi_duck

8:15 pm on Apr 20, 2007 (gmt 0)

10+ Year Member



Thank you so much it kinda worked.

However there are a few problems with it...

1) It shows the first 5 sigs correctly but when the 6th sig is created it still only shows the first 5 instead of replacing the earliest one. Any ideas how to solve?

2) probably to do with the last one but in my Mysql table how do i delete the record when it's gone past the 5th one?

2) I haven't tried the used ip thing yet as i dont know how to set it up. Which part of the code do i put it in?

Thanks again you've solved a problem i was totally confused about.

digi_duck

9:11 pm on Apr 23, 2007 (gmt 0)

10+ Year Member



bump