Click to See Complete Forum and Search --> : replaceable picture


stianzz
05-07-2007, 03:31 AM
Hello. i could really use some help. Me and a friend is trying to construct a homepage for our community back home. The page is turning out good but we want to add a function that we have no idea of making.
The idea is to have an image on our site that users can replace with another picture if they want. How is it possible to make such a function on our webpage?:confused: can anyone please give us an answer or maybe a direction to some guide? it would be much appreciated.;)

Thanks in advance.

MarcD
05-07-2007, 06:35 AM
Would that custom image be visible to only that specific user or would the new image need to be uploaded to the server and replace the old image permanently for every visitor to see?

That makes a major difference in the way such a function can be implemented.

WebJoel
05-07-2007, 07:19 AM
www.dynamicdrive.com has something like that... a 'background image switcher'. I used it here:

http://ca.geocities.com/tiling_background_images/

caveat: only works in IE and Opera. I suppose it could be altered to work more-browser, and easily converted to switch image-proper, not just switch 'background tiling image', as is my page..

stianzz
05-07-2007, 02:01 PM
Would that custom image be visible to only that specific user or would the new image need to be uploaded to the server and replace the old image permanently for every visitor to see?

That makes a major difference in the way such a function can be implemented.

Thanks for replying.
Im sorry MarcD for not being specific. I have not decided yet of what way i want to go with this. I think it will be enough for only the specific user too see the image, but i am intrested in the possibility to upload images permanently as well. Can you please tell me more about both ways of acheiving this?

Thanks Webjoel for the links. i will definetly check them out.

MarcD
05-08-2007, 01:29 PM
1. Permanent image for every visitor to see:
I think the easiest way of adding a image which a user can see is either renaming an uploaded image and replacing it with the previous image on the server or keeping all images on your server and simply adding their filenames to a database and go from there.
Both of these methods will be accomplished using PHP.

I will explain the first method below. Since you don't indicate if you have experience with PHP, i will try to explain it as complete as possible. Sorry if i point out the obvious.

1.1. The form
The form used to upload an image is relatively simple and suitable for all situations. A browse box for selecting the file and a submit button.

<form action="<?php echo $_SESSION['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<input type="file" name="uploadimage" id="uploadimage" value="" /><br />
<input type="hidden" name="max_file_size" value="2000" />
<input type="submit" value="Upload" />
</form>


The key to uploading files is the encoding type. This has to be multipart/form-data, to allow the user to upload a file.
The form action indicates what page it should go to, once the form is submitted. In this case, it reloads the same page the form is in, but could also be set to other pages like uploaded.php for example. Which action is taken after submission of the form isn't relevant.
We also want to limit the maximum filesize the uploaded file can be with the hidden field.

1.2. Uploading and replacing

<?php
if($_FILES['uploadimage']['error'] > 0){
echo 'An error has occured:<p />';

switch($_FILES['uploadimage']['error']){
case 1:
echo 'File exeeded upload_max_filesize.';
break;

case 2:
echo 'File exeeded max_file_size.';
break;

case 3:
echo 'File only partially uploaded.';
break;

case 4:
echo 'No file uploaded.';
break;
}

exit;
}

// Does the file have the right MIME type?
if(!($_FILES['uploadimage']['type'] == 'image/jpeg' || $_FILES['uploadimage']['type'] == 'image/pjpeg')){
echo 'An error has occured:<p />';
echo $_FILES['uploadimage']['type'] . ' is not a correct image.<br />';
echo $_FILES['uploadimage']['type'];
exit;
}

//Put the file in the right directory
$filename = "customimage.jpg"
$upfile = 'images/custom/' . $filename;

if(is_uploaded_file($_FILES['uploadimage']['tmp_name'])){
if(!move_uploaded_file($_FILES['uploadimage']['tmp_name'], $upfile)){
echo 'An error has occured<p />';
echo 'Could not move file to destination directory.';
exit;
}
}
else{
echo 'An error has occured:<p />';
echo 'Possible file upload attack.<br / >';
echo 'Filename: ' . $_FILES['uploadimage']['name'];
exit;
}

$chmodfile = chmod($upfile, 0777);
echo 'Image added successfully.';
?>


What the above code does:
After the user select the image and submitted the form, the page reloads and the file information is put in an array called $_FILES.
The array contains another array: 'uploadedfile' (because that was the name and id of the input browse box from the form) which contains a number of values of it's own.
Anyway, the file is checked for a number of conditions.
If the file was uploaded succesfully and not bigger than the value of the hidden field in the form, then it goes on to the second check, the Mime type.
The Mime types for jpg images are: image/jpeg and image/pjpeg.

If the uploaded image meets this criteria, the actual uploading begins.
Because we will replace an existing image, the file is renamed to customimage.jpg and will be moved to the $upfile directory, namely "images/custom/".
Last, the CHMOD is set to 0777, which allows the image to be moved to the $upfile directory. (You may have to set the set the directory to 777 by forehand using your FTP program).

Now to make the actual image show up in your page:

<?php
if(file_exists('images/custom/customfile.jpg')){
echo '<img src="images/custom/customfile.jpg" alt="" title="User uploaded file!" width="200" height="200" />';
}
else{
echo '<img src="images/custom/defaultfile.jpg" alt="" title="" width="200" height="200" />';
}
?>


Alternatively, you could also resize the uploaded image before moving them to the custom directory.

I hope this helps you out.