Jump to content

Uploading and resizing images for a user profile system


Recommended Posts

I am trying to upload files to a user profile system.

here is the profile page

 

<?php

include('core/init.inc.php');

if (isset($_POST['email'], $_POST['location'], $_POST['about']))
{
$errors = array();

if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false)

{
	$errors[] = "The email address you entered is not valid";	
}

if(preg_match('#^[a-z0-9 ]+$#i',$_POST['location'])===0)
{
		$errors[] = 'Your location must only contain A-Z 0-9 and spaces.';
}

if (empty($_FILES['avatar']['tmp_name']) === false)
{
	$file_ext = end(explode('.', $_FILES['avatar']['name']));

	if(in_array(strtolower($file_ext), array('jpg', 'jpeg', 'gif', 'png')) === false)
	{
		$errors[] = 'Your avatar must be an image.';	
	}
}	
if(empty($errors))
{
	print_r($_FILES);
	set_profile_info($_POST['email'],$_POST['location'],$_POST['about'], (empty($_FILES['avatar']['tmp_name'])) ? false : $_FILES['avatar']['tmp_name']);
}	

$userinfo = array(
'email'	=> htmlentities($_POST['email']),
'location'	=> htmlentities($_POST['location']),
'about'	=> htmlentities($_POST['about'])
);

}	

else
{
$userinfo = fetch_user_info($_SESSION['uid']);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit your Profile</title>
</head>

<body>
<div>
	<?php

	if(isset($errors) == false)
	{

		echo 'Click update to edit your profile.';	

	}
	else if(empty($errors))
	{

		echo 'Your profile has been updated.';

	}
	else
	{
		echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';	
	}
	?>

</div>

<form action="" method="post" enctype="multipart/form-data">
<div>
	<label for="email">Email: </label>
	<input type="text" name="email" id="email" value="<?php echo $userinfo['email']; ?>" />
</div>
    <div>
	<label for="location">Location: </label>
	<input type="text" name="location" id="location" value="<?php echo $userinfo['location']; ?>" />
</div>
    <div>
	<label for="about">About Me: </label>
	<textarea name="about" id="about" rows="14" cols="50"><?php echo strip_tags($userinfo['about']); ?></textarea>
</div>
    <div>
    	<label for="avatar">Avatar: </label>
        <input type="file" name="avatar" id="avatar"/>
    </div>
    <div>
	<input type="submit" value="Update" />
</div>
    </form>
</body>
</html>

 

here is the function taken from an external file

function set_profile_info($email, $location,$about,$avatar)
{
        $email          = mysql_escape_string(htmlentities($email));
        $about          = mysql_escape_string(nl2br(htmlentities($about)));
        $location       = mysql_escape_string($location);
       
        if (file_exists($avatar))
        {
                $src_size = getimagesize($avatar);
               
                if ($src_size['mime'] === 'image/jpeg')
                {
                        $src_img = imagecreatefromjpeg($avatar);       
                }
                else if ($src_size['mime'] === 'image/png')
                {
                        $src_img = imagecreatefrompng($avatar);
                }
                else if ($src_size['mime'] === 'image/gif')
                {
                        $src_img = imagecreatefromgif($avatar);
                }
                else
                {
                        $src_img = false;
                }
               
                if ($src_img !== false)
                {
                        $thumb_width= 200;
                       
                                if($src_size[0] <= $thumb_width)
                                {
                                        $thumb = $src_img;     
                                }
                                else
                                {
                                        $new_size[0] = $thumb_width;
                                        $new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;
                                       
                                        $thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
                                        imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
                                }
                               
                                imagejpeg($thumb, "{$GLOBALS['path']}/user_avatars/{$_SESSION['uid']}.jpg");
                }
        }
       
        $sql = "UPDATE `users` SET
                                `user_email` = '{$email}',
                                `user_about` = '{$about}',
                                `user_location` = '{$location}'
                               
                        WHERE `user_id` = {$_SESSION['uid']}";
       
        mysql_query($sql);
       
}

 

 

Below I have returned the array of files to check if its been uploaded correctly.

Array ( [avatar] => Array ( [name] => Sonic.jpg [type] => image/jpeg [tmp_name] => /var/tmp/php.waq8n [error] => 0 => 48477 ) )

 

But I get this error message.

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/tmp/php.waq8n' is not a valid JPEG file in /web/stud/u0963643/userprofilesection/finaluserprofile/core/inc/user.inc.php on line 71

 

If someone could point out where in this code I have made an error I would be very grateful

 

Thanks

Jamie

take a close look

if ($src_size['mime'] === 'image/jpeg')

and

Array ( [avatar] => Array ( [name] => Sonic.jpg [type] => image/jpeg [tmp_name] => /var/tmp/php.waq8n [error] => 0 [size] => 48477 ) ) 

there is no 'mime' element in the array, it's 'type'

Are you sure the file itself, before you upload it, is actually a valid jpg/jpeg image file? Have you tried a different type of image file?

 

@laffin, the $src_size data is from a getimagesize statement.

 

Edit: I just tried the code you posted for a known good .jpg source file and it did create an expected resized .jpg image in the appropriate destination folder. I would check to make sure your sonic.jpg is actually a .jpg file (browsers will open mis-named images correctly, php functions are not going to be as forgiving.)

Thanks. I have tried it with a few different .jpg's and it seems a bit hit and miss if I'm honest. But I have come across a problem in the actual code itself anyway. Since I will be using the code for an iphone web app I came across the problem that input type="file". Will not work with iphones. I have decided to remove this feature altogether since the work around involves each user downloading a specific iphone app which handles this.

 

Thanks again for your help

Jamie

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.