Chippiewill
05-02-2012, 11:58 AM
I doubt this is good code or anything (I put it all together in less than an hour) but someone might find it useful. It's the image loader for my signature. The other part is just a basic image uploader from w3schools that I changed some params around on so it'd only take PNGs and it'd set the file name to their IP.
To get the forum to accept it I used a basic modrewrite so you can access "file.php" at "file.png". I had an earlier version which would accept jpegs but a browser would get confused if it received a JPEG with a .png.
A wierd thing was happening earlier where the session would expire and overwrite a person's image with the default.. I've now fixed this. fixing this.
<?PHP
session_start();
session_cache_limiter('private'); // Some function I don't know what it does, should probably look it up
session_cache_expire(10000); //Set session expire to ~a week
function give_image($ip) { //Check if image exists and then ask for it
if(file_exists ("./images/".$ip.".png")) {
return_image("./images/".$ip.".png");
}
}
function return_image($path) { //Render image handler stuff
$im = imagecreatefrompng($path);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
$ip = str_replace(".", "-", $_SERVER['REMOTE_ADDR']); //Change the dots to dashes, cleaner strings and that
if(isset($_SESSION['p_ip'])) { //If they have a session then lets give them their file
if($_SESSION['p_ip'] = $ip) { // If they're IP hasn't changed then just give them their image
give_image($ip);
}
else {
rename("./images/".$_SESSION['p_ip'].".png", "./images/".$ip.".png"); //If they're IP has changed then let's just move their old image to their new IP
$_SESSION['p_ip'] = $ip; // Let's set their session to their new IP
give_image($ip); //Return the image
}
} elseif(give_image($ip)) { // If they don't have a cookie then just try and give them their IP's cookie
$_SESSION['p_ip'] = $ip;
}
else { // IF they're entirely new (No sesssions and new IP..) then
$_SESSION['p_ip'] = $ip; //Give them a session
$im = imagecreatefrompng("./images/default.png"); //Give trhem the default image
header('Content-Type: image/png'); //Set headers blahy
imagepng($im); //Image giving
imagedestroy($im); //Clear memory
copy("./images/default.png", "./images/".$ip.".png"); //Copy default to their IP so they don't keep going through a load of elses.
}
?>
To get the forum to accept it I used a basic modrewrite so you can access "file.php" at "file.png". I had an earlier version which would accept jpegs but a browser would get confused if it received a JPEG with a .png.
A wierd thing was happening earlier where the session would expire and overwrite a person's image with the default.. I've now fixed this. fixing this.
<?PHP
session_start();
session_cache_limiter('private'); // Some function I don't know what it does, should probably look it up
session_cache_expire(10000); //Set session expire to ~a week
function give_image($ip) { //Check if image exists and then ask for it
if(file_exists ("./images/".$ip.".png")) {
return_image("./images/".$ip.".png");
}
}
function return_image($path) { //Render image handler stuff
$im = imagecreatefrompng($path);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
$ip = str_replace(".", "-", $_SERVER['REMOTE_ADDR']); //Change the dots to dashes, cleaner strings and that
if(isset($_SESSION['p_ip'])) { //If they have a session then lets give them their file
if($_SESSION['p_ip'] = $ip) { // If they're IP hasn't changed then just give them their image
give_image($ip);
}
else {
rename("./images/".$_SESSION['p_ip'].".png", "./images/".$ip.".png"); //If they're IP has changed then let's just move their old image to their new IP
$_SESSION['p_ip'] = $ip; // Let's set their session to their new IP
give_image($ip); //Return the image
}
} elseif(give_image($ip)) { // If they don't have a cookie then just try and give them their IP's cookie
$_SESSION['p_ip'] = $ip;
}
else { // IF they're entirely new (No sesssions and new IP..) then
$_SESSION['p_ip'] = $ip; //Give them a session
$im = imagecreatefrompng("./images/default.png"); //Give trhem the default image
header('Content-Type: image/png'); //Set headers blahy
imagepng($im); //Image giving
imagedestroy($im); //Clear memory
copy("./images/default.png", "./images/".$ip.".png"); //Copy default to their IP so they don't keep going through a load of elses.
}
?>