Anyway, i coded a great image upload script years ago and honestly i totally forgot about it and its just lying around wasted. I thought i could bring that script back and can be easily integrated to php login script. You can use it as avatar or profile feature.
Goal
To let the users upload a picture or avatar to their profile page. We automatically shrink the uploaded image to desired dimensions. I am keeping it as simple as possible.
Features
- Just upload to any picture and the script will auto shrink it to your desired dimensions (100x100px for avatars) or you can specify the sizes.
- Added some security features as well to image upload script.
Requirements
1. You will need php login script
2. Download the
PHP Thumbnailer class. You can refer to documentation there.
Installation
Before you mess around the code, carefully backup the mysettings.php
Step 1
First create a database field
avatar in users table. It can be varchar(220)
Step 2
unzip the phpthumbnailer class, name it as
phpthumb and place it within phplogin folder.
Step 3
Create uploads folder within your php login script folder and you have to make it as 777 writable permissions to store images. To disable dangerous script executions you will need to create .htaccess file and place the following code in .htaccess file
Quote:
Options -Indexes
Options -ExecCGI
AddHandler cgi-script .php .php3 .php4 .phtml .pl .py .jsp .asp .htm .shtml .sh .cgi
|
Step 4
Open
mysettings.php and place the following html code. The form should be separately just like update passwords.
PHP Code:
<form action="mysettings.php" method="post" enctype="multipart/form-data" name="form2" id="form2">
<p>
Image Upload
<input type="file" name="ifile">
<input type="hidden" name="MAX_FILE_SIZE" value="500000" />
<input name="Submit" type="submit" id="Submit" value="Upload">
</p>
<p><strong>Max 100 Kb, JPEG/PNG only (100x100 pixels maximum) </strong></p>
</form>
You can specify the MAX_FILE_SIZE value to restrict maximum file upload size. In this case we restrict the size to 500kb
5. Open mysettings.php,place this php code somewhere on top.
PHP Code:
<?php
/*************** AVTAR UPLOAD SCRIPT ******************************/
//Requirements: PHP Thumbnail class (http://phpthumb.gxdlabs.com/)
// License: Free
/***********************************************/
if($_POST['Submit'] == 'Upload')
{
// This is the unique user_id
$id = $_SESSION['user_id'];
if (!empty ($_FILES['ifile']['tmp_name']))
{
/* Thumbnail class is required */
include_once('phpthumb/ThumbLib.inc.php');
/* GetImageSize() function pulls out valid info about image such as image type, height etc. If it fails
then it is not valid image. */
if (!getimagesize($_FILES['ifile']['tmp_name']))
{
die("Error - Invalid Image File.");
}
$imgtype = array('1' => '.gif', '2' => '.jpg' , '3' => '.png');
// extract the width and height of image
list($width, $height, $type, $attr) = getimagesize($_FILES['ifile']['tmp_name']);
// Extract the image extension
switch ($type)
{
case 1: $ext='.gif'; break;
case 2: $ext = '.jpg';break;
case 3: $ext='.png'; break;
}
// Dont allow gif files to upload as it may contain harmful code
if ( $ext == '.gif') {
die("Sorry - GIF not allowed. Please use only PNG or JPEG formats");
}
/* Specify maximum height and width of users uploading image */
if ($width > 1000 || $height > 1000)
{
die("ERROR: Maximum width and height exceeded. (max 1000x1000 pixels)");
}
/* Specify maximum file size here in bytes */
if ($_FILES['ifile']['size'] > 500000 )
{
die("Error: Large File size. (max 500kb)");
}
/******** IMAGE RESIZING *********************/
// Before we start resizing, we first have to move the image file to server
// save it there under a unique name and then do the final resizing and save the resized image.
// Specify which directory you want to upload. It should be a subfolder where the script is present
// We also generate a unique name for picture FILE-USERID-XXX where xxx is random number
// The uploads folder must have writable permissions.
$uploaddir = 'uploads/';
$secondname = rand(100,99);
$uploadfile = $uploaddir . "img-$id-$secondname". $ext;
if (!move_uploaded_file($_FILES['ifile']['tmp_name'], $uploadfile ))
{
die("Error moving the uploaded file");
}
$thumb = PhpThumbFactory::create($uploadfile);
//specify the height and width of avatar image to resize
$thumb->resize(100,100);
$thumb->save($uploadfile);
//$thumb->show();
//MySQL query to update avatar filename in the database. You need to create a field avatar
mysql_query("update users set avatar='$uploadfile' where id='$id'");
//$thumb->destruct();
}
}
?>
The default resize is 100x100. You can change it by altering this line
We only allow JPG, PNG files and GIF not allowed from security point of view and it can contain harmful code. If you want to allow GIF, just remove these lines.
PHP Code:
// Dont allow gif files to upload as it may contain harmful code
if ( $ext == '.gif') {
die("Sorry - GIF not allowed. Please use only PNG or JPEG formats");
}
I have shown the very basic usage and you may to alter the die() commands specific to your needs.
If you closely observe, we are using this code to update the avatar filename to the user database. This is a simple mysql query which updates it. We are storing the image by generating a unique name IMG-USERID-xx and where xx are random nos.
Quote:
//MySQL query to update avatar filename in the database. You need to create a field avatar
mysql_query("update users set avatar='$uploadfile' where id='$id'");
|
Step 5:
Finally to showup avatar in mysettings.php, place this html code anywhere you want.
PHP Code:
<img src="<?php echo $row_settings['avatar']; ?>">
Go through the commented php code, to tweak this script more. If upload is successful you will see the images in uploads folder.