Jump to content

Profilepic in session, how to update?


B_CooperA

Recommended Posts

I know this might been asked before, but my problem is a bit different compared to the other related topic I found in here. So, I have a profile page where user can change his/her profile pic. Here's my function for that: 

public function createProfileThumb() {



if(!empty($_FILES['upload_profile_pic']['name'])) {

	$target_folder = 'images/thumbnails/'; // Tallennettava kansio
	$ext = pathinfo($_FILES['upload_profile_pic']['name'], PATHINFO_EXTENSION);
	$newimg_name = "userprofilepic".$_GET['id'].".".$ext;
	$upload_image = $target_folder.basename($_FILES['upload_profile_pic']['name']);

        // Uusi tiedostopaikka

	if(move_uploaded_file($_FILES['upload_profile_pic']['tmp_name'], $upload_image)) {

	$thumbnail = $target_folder.$newimg_name; // Thumbnailin nimi

	// Get new sizes
	list($width, $height) = getimagesize($upload_image);
	$newwidth = 300; // Thumbnailin leveys pikseleissä
	$newheight = 300; // Thumbnailin korkeus pikseleissä

		
        // Kuvan lataus
	$thumb = imagecreatetruecolor($newwidth, $newheight);

	if( $_FILES['upload_profile_pic']['type'] == "image/jpeg" && 
		$_FILES['upload_profile_pic']['size'] < 3000000) {

	$source = imagecreatefromjpeg($upload_image);
	imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
	imagejpeg($thumb, $thumbnail, 100);

	$query = "UPDATE users SET profilepic = :profilepic WHERE user_id = :user_id";
		$stmt = $this->connect->prepare($query);
		$stmt->execute(array(

		':profilepic' =>  $newimg_name,

		':user_id' => $_SESSION['user_id']

	));

		if($query) {

		// REGENERATE THE $_SESSION['profilepic'] SOMEHOW

} else {

	$this->error[] = "Kuvan tallennus epäonnistui";

}



} else {

	$this->error[] = "Kuva on väärässä muodossa (vain .jpg) tai liian suuri";

}
	// Poistetaan alkuperäinen kuva 
	unlink($upload_image);

	} else {

		return false;

	}

	} else {

		$this->error[] = "Kuvaa ei ole valittu";

	}

	return (count($this->error)) ? 0 : 1;

}

And here's a part of my login script which will save somer user information, including profilepic, in to sessions.

public function fetchInfo() {

	$query = "SELECT * FROM users 
        WHERE email = :email AND  activation_token IS NULL";
	
        $stmt = $this->connect->prepare($query);
	$stmt->execute(array(

		':email' => $this->email,

		)); 

	if($stmt->rowCount() == 0) {
		$this->error[] = "Väärä käyttäjätunnus tai salasana";
		return 0;

	} else {

		$row = $stmt->fetch(PDO::FETCH_ASSOC);

		$_SESSION['user_id'] = $row['user_id'];
		$_SESSION['email'] = $row['email'];
		$_SESSION['name'] = $row['name'];
		$_SESSION['profilepic'] = $row['profilepic'];

	}

	if (Register::cryptPass($this->password) != $row['password']) {
	$this->error[] = "Virheelliset kirjautumistiedot";

	} else {

	return true;

	}
	return count($this->error) ? 0 : 1;

	}

My main problem is that every time user does change his/her profilepic, it will show up as a blank image until I log my self out destroying the session and log in back. So how will I some sort of "Regenerate" the picture inside the session superglobal?

 

My thumbnail function will obviously create a thumbnail from that uploaded picture, but also deletes the original picture and renames the new thumbnail to match up the users id. So if my users auto incremented user_id value in users table is 1, it will rename it to userprofilepic1.jpg. That will save me a lot of memory, since there's only one picture per user saved each and every time user changes the profile picture.

Edited by B_CooperA
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.