Uploading Profile Photo of a member

Members can add or update their photo to their membership account. This is popularly known as profile photo. We will learn how to add profile photo by a member.

This script is a part of membership management script so profile photo can be added or updated by the member after login. Here there is a membership table where all details of the member is stored. You can see the structure of the membership table here.
Structure of Membership Table
Watch the last column where we store the file name.

Unique file name

Each member profile photo is stored in a common folder so the file names are to be unique. Each member has one unique member id and user id. Hence we will store the file name along with the member id. For example for user id plus2net member id is 4. Hence the file name will be 4.jpg

You can add userid also to the file name. like plus2net.jpg

Where to store the profile pictures.

In the root of the script there is a folder profile-photo. Here we are uploading all images and storing profile photo. If you want to change the folder then it can be done inside memadmin area two different files used for uploading photo.

You must give write permission to this folder to enable file upload.

How members can add profile photo

After login member can go to memadmin area saying settings. From memadmin area there are two links, one is to update profile and one more to update or add profile photo.
Upload buttons for Profile picture
On visiting the profile photo page , the old profile photo page if already added will be displayed below the upload photo button. Here user can use the upload button to select a new profile photo from its computer. On Clicking the choose file button user can browse local computer files and select an image of JPG or GIF file type. Other file type are not allowed.

Once uploaded the file extension is checked for gif or jpg type images otherwise file is deleted.

After uploading the script check the dimensions of image and they are reduced proportionately to the required size.
$n_width=100; // Fix the width of the thumb nail images
$n_height=100; // Fix the height of the thumb nail image
you can read more on resizing images after uploading here.

Once the resizing of images are over it is time to update the record with new file name. In our member table we store details of each member. For profile picture we have a column profile_photo. We will update this column with our new file name.

We will take member id which is unique and is present as session variable for updating record in where clause.

Here is the code for updating record with profile photo. Note that we are not storing the photo in the table but storing the file name against member record.
$sql=$dbo->prepare("update mem_signup set profile_photo=:profile_photo 
where mem_id=$_SESSION[mem_id] and userid='$_SESSION[userid]'");
$sql->bindParam(':profile_photo',$profile_file_name,PDO::PARAM_STR, 199);
if($sql->execute()){
echo "<br>Successfully updated Profile photo<br>";
echo "<img src=$tsrc>";
}// End of if profile is ok 
else{
print_r($sql->errorInfo());
$msg=" <br>Database problem, please contact site admin <br>";
}

How to check script or add profile photo

Download the script and install the database file. Inside Memadmin directory there are two files, profile-photo.php & profile-photock.php.
This is a part of Membership Management Script.

Script part

There are two files, one is profile-photo.php and other one is profile-photock.php.

profile-photo.php

This file show the upload button and handle the form. Here is the code
$profile_photo_path="../profile-photo/";

$count=$dbo->prepare("select profile_photo from mem_signup
 where mem_id=:mem_id");
$count->bindParam(":mem_id",$_SESSION['mem_id'],PDO::PARAM_INT,1);

if($count->execute()){
$row = $count->fetch(PDO::FETCH_OBJ);
}else{
print_r($dbo->errorInfo());
}

echo "<FORM ENCTYPE=\"multipart/form-data\" 
ACTION=\"profile-photock.php\" METHOD=POST>
 <INPUT NAME=\"userfile\" TYPE=\"file\">
<INPUT TYPE=\"submit\" VALUE=\"Upload Photo\"></FORM>
";
if(strlen($row->profile_photo) > 1 ){
	// Path where thumb nail image will be stored
$tsrc=$profile_photo_path.$row->profile_photo; 
echo "<img src=$tsrc>";
}
/////////////////////////////

profile-photock.php

This file upload the photo and place them in a directory after resizing it. Here is the code.
$profile_photo_path="../profile-photo/";
// To display file name, temp name and file type , 
//use them for testing your script only//////
//echo "File Name: ".$_FILES['userfile']['name']."<br>";
//echo "tmp name: ".$_FILES['userfile']['tmp_name']."<br>";
//echo "File Type: ".$_FILES['userfile']['type']."<br>";
//echo "<br><br>";
///////////////////
// the path with the file name where the file will be stored. 
$add=$profile_photo_path.$_FILES['userfile']['name']; 
//echo $add;
if(move_uploaded_file ($_FILES['userfile']['tmp_name'],$add)){
//echo "<br>Successfully uploaded the image<br>";
chmod("$add",0777);

}else{echo "<br>Failed to upload file Contact Site admin to fix the problem<br>";
@unlink($add);
exit;}
/////////////////////////
if (!($_FILES['userfile']['type'] =="image/jpeg" OR $_FILES['userfile']['type']=="image/gif")){
echo "<br>Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
unlink($add);
exit;}
//////////////////

///////// Start the thumbnail generation//////////////
$n_width=100; // Fix the width of the thumb nail images
$n_height=100; // Fix the height of the thumb nail image


if($_FILES['userfile']['type']=="image/gif")
{
$im=ImageCreateFromGIF($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
$profile_file_name=$_SESSION['mem_id'].".gif";
 // Path where thumb nail image will be stored
$tsrc=$profile_photo_path.$profile_file_name;
if (function_exists("imagegif")) {
Header("Content-type: image/gif");
ImageGIF($newimage,$tsrc);
}
elseif (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage,$tsrc);
}
chmod("$tsrc",0777);
}////////// end of gif file thumb nail creation//////////

////////////// starting of JPG thumb nail creation//////////
if($_FILES['userfile']['type']=="image/jpeg"){
$im=ImageCreateFromJPEG($add); 
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height); 
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
$profile_file_name=$_SESSION['mem_id'].".jpg";
// Path where thumb nail image will be stored
$tsrc=$profile_photo_path.$profile_file_name; 
ImageJpeg($newimage,$tsrc);
chmod("$tsrc",0777);
}

$sql=$dbo->prepare("update mem_signup set profile_photo=:profile_photo where mem_id=$_SESSION[mem_id] and userid='$_SESSION[userid]'");
$sql->bindParam(':profile_photo',$profile_file_name,PDO::PARAM_STR, 199);
if($sql->execute()){
echo "<br>Successfully updated Profile photo<br>";
echo "<img src=$tsrc>";
}// End of if profile is ok 
else{
print_r($sql->errorInfo());
$msg=" <br>Database problem, please contact site admin <br>";
}
unlink($add);
echo $msg;
download Membership Management script
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Chinedum

    07-03-2018

    Nice script. Please what does the $dbo stand for?

    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer