PDA

View Full Version : Signature Image Unique Loader thingy



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.
}
?>

Kasabian
05-02-2012, 04:27 PM
Yeah, looks great ;)

Dentafrice
05-02-2012, 08:17 PM
I prettied it up a little bit..

http://pastie.org/3323373


<?php
session_start();

#################
Settings
#################

$directory = "./images/";
$extension = ".png";
$session_name = 'image_ip';
$default_file = 'default';

##################################
Functions
##################################

function give_image($ip_address) {
$path = $directory . $ip_address . $extension;

if(file_exists($path)) {
return_image($path);
} else {
return false;
}
}

function return_image($path) {
$image = imagecreatefrompng($path);
header("Content-type: image/png");

imagepng($image);
imagedestroy($image);
}

##################################
Program Code
##################################

$ip = str_replace('.', '-', $_SERVER['REMOTE_ADDR']);

if(isset($_SESSION[$session_name])) {
if($_SESSION[$session_name] == $ip) {
give_image($ip);
} else {
rename($directory . $_SESSION[$session_name] . $extension, $directory . $ip . $extension);

$_SESSION[$session_name] = $ip;

give_image($ip);
}
} else {
if(give_image($ip)) {
$_SESSION[$session_name] = $ip;
} else {
$_SESSION[$session_name] = $ip;

$image = imagecreatefrompng($directory . $default_file . $extension);

header('Content-type: image/png');

imagepng($image, $directory . $ip . $extension);
imagepng($image);
imagedestroy($image);
}
}
?>

Chippiewill
05-02-2012, 09:11 PM
extension parameter is worthless unless you alter the content type. But you sure made it far more readable.

Also if you really wanted to clean it up you could just use give_image($default_file); for the default image return.

Dentafrice
05-02-2012, 09:26 PM
I couldn't be arsed with really looking at the logic of it.

As far as the extension, yeah it's worthless but makes it easier to read. You'd not only have to change the header, but also the imagepng. You'd do imagecreatefromstring(file_get_contents($path)). Then you'd have to decide whether to output using imagepng, imagejpeg, etc.

Want to hide these adverts? Register an account for free!