Jump to content

PHP images gd library shouldnt be too hard


ashton321

Recommended Posts

I am trying to create a "profile picture" for my users accounts but i do not really understand the php image functions nor uploading completely for that matter.  So far, I have an upload script that i found before and have used successfully in the past.  I tried to combine it with an image resize script that i found on about.com that uses the gd library.  My register script was working prior to adding this and now, it no longer works and i do not get an error either.  Any help or guidance in the right direction would be greatly apperciated.

 

here is my register script

<?php <?php
session_start();
$user_level = $_SESSION['user_level'];
    if(isset($_POST["fname"]))
        {
        
            <-- database connect -->
    
            //Values
                $title = $_POST['title'];
                $fname = $_POST['fname'];
                $lname = $_POST['lname'];
                $address = $_POST['address'];
                $city = $_POST['city'];
                $state = $_POST['state'];
                $zip = $_POST['zip'];
                $email = $_POST['email'];
                $email2 = $_POST['email2'];
                $phone = $_POST['phone'];
                $fax = $_POST['fax'];
                $username = $_POST['username'];
                $password = $_POST['password'];
                $password2 = $_POST['password2'];
                $type = $_POST['type'];
                
            //Make sure username is availble
                $usercheck = $_POST['username'];
                $check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'")
                or die(mysql_error());
                $check2 = mysql_num_rows($check);

            //If the name exists it gives an error
                if ($check2 != 0) {
                die('Sorry, the username '.$_POST['username'].' is already in use.');
                }

                
            //Check to make sure emails are the same
                if(!$email = $email2)
                    {
                        die('Please go back and make sure that the emails you entered match');
                    }
                
            //Check to make sure email is valid
                if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
                    {   
                        die('Invalid Email');
                    }
                    
            
            //Check to make sure passwords are the same
                if(!$password = $password2)
                    {
                        die('Please go back and make sure that the passwords you entered match');                       
                    }
                    
            //Convert paswword to md5
                $password = md5($password);
                
         //Picture
                mkdir("../webdev/images/users/$username", 0777, true);
                $uploaddir = "../webdev/images/users/$username/";
                $allowed_ext = "jpg";
                $max_size = "5000000";
                $max_height = "10000";
                $max_width = "10000";
        // Check Entension
                $extension = pathinfo($_FILES['profpic']['name']);
                $extension = $extension[extension];
                $allowed_paths = explode(", ", $allowed_ext);
                for($i = 0; $i < count($allowed_paths); $i++) {
                 if ($allowed_paths[$i] == "$extension") {
                 $ok = "1";
                 }
                }
        // Check File Size
                if ($ok == "1") {
                if($_FILES['profpic']['size'] > $max_size)
                {
                print "File size is too big!";
                exit;
                }
        // Check Height & Width
                if ($max_width && $max_height) {
                list($width, $height, $type, $w) = getimagesize($_FILES['profpic']['tmp_name']);
                if($width > $max_width || $height > $max_height)
                {
                print "File height and/or width are too big!";
                exit;
                }
                }
                
                // The Upload Part
                if(is_uploaded_file($_FILES['profpic']['tmp_name']))
                {
                move_uploaded_file($_FILES['profpic']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
                $Link = $_FILES['profpic']['name'];
                
                //Name you want to save your file as
                $save = '../images/users/'. $username .'/default.jpg';

                $file = $_FILES['profpic']['name'];
                header('Content-type: image/jpeg') ;
                list($width, $height) = getimagesize($file) ;
                $modwidth = 150;
                $modheight = 150;
                $tn = imagecreatetruecolor($modwidth, $modheight) ;
                $image = imagecreatefromjpeg($file) ;
                imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

                // Here we are saving the .jpg, you can make this gif or png if you want
                //the file name is set above, and the quality is set to 100%
                imagejpeg($tn, $save, 100) ;
                
            //Add to database
                $upload = "INSERT INTO users (title, fname, lname, address, city, state, zip, email, phone, fax, username, password, type) values('$title' ,'$fname' , '$lname' , '$address', '$city', '$state' ,'$zip' ,'$email', '$phone', '$fax', '$username', '$password', '$type')";
                $add = mysql_query($upload) or die('Error In Upload FIX IT!');

            //Upon Success return to index page
                if($add)
                    {
                        header('Location: http://www.crnfbla.com/other.php?message=You have been successfully registered');
                        exit;
                    }
        }}}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Council Rock High School North FBLA</title>
        <link href="css/style.css" media="all" rel="stylesheet" type="text/css" />


    </head>
    <body>
    <div id="container">
        <div id="header">
            <?php include('inc/navbar.php'); ?>     
        </div>
        
        <div id="maincontent">
            <h6>Please fill in the forms below to register</h6>
            <p>&nbsp </p>
                <table class="regform">
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?> " enctype="multipart/form-data" method="post">
                    <tr>
                        <td class="head" colspan="2" align="left">Personal Information</td>
                    </tr>
                    <tr>                        
                        <td>Title:</td>
                            <td><select name="title">
                                    <option value="Mr.">Mr.</option>
                                    <option value="Mrs.">Mrs.</option>
                                    <option value="Ms.">Ms.</option>
                                    <option value="Miss">Miss</option>
                                    <option value="Dr.">Dr.</option>
                                </select>
                            </td>
                    </tr>
                    <tr>
                        <td>First Name:</td>
                            <td><input name="fname" type="text" id="fname">
                        </td>
                    </tr>
                    <tr>
                        <td>Last Name:</td>
                            <td><input name="lname" type="text" id="lname">
                        </td>
                    </tr>
                    <tr>
                        <td>Address</td>
                            <td><input name="address" type="text" id="address">
                        </td>
                    </tr>
                    <tr>
                        <td>City:</td>
                            <td><input name="city" type="text" id="city">
                        </td>
                    </tr>
                    <tr>
                        <td>State:</td>
                            <td>
                                <select name="state" id="state">
                                    <option value="AL">AL</option>
                                    <option value="AK">AK</option>
                                    <option value="AZ">AZ</option>
                                    <option value="AR">AR</option>
                                    <option value="CA">CA</option>
                                    <option value="CO">CO</option>
                                    <option value="CT">CT</option>
                                    <option value="DE">DE</option>
                                    <option value="DC">DC</option>
                                    <option value="FL">FL</option>
                                    <option value="GA">GA</option>
                                    <option value="HI">HI</option>
                                    <option value="ID">ID</option>
                                    <option value="IL">IL</option>
                                    <option value="IN">IN</option>
                                    <option value="IA">IA</option>
                                    <option value="KS">KS</option>
                                    <option value="KY">KY</option>
                                    <option value="LA">LA</option>
                                    <option value="ME">ME</option>
                                    <option value="MD">MD</option>
                                    <option value="MA">MA</option>
                                    <option value="MI">MI</option>
                                    <option value="MN">MN</option>
                                    <option value="MS">MS</option>
                                    <option value="MO">MO</option>
                                    <option value="MT">MT</option>
                                    <option value="NE">NE</option>
                                    <option value="NV">NV</option>
                                    <option value="NH">NH</option>
                                    <option value="NJ">NJ</option>
                                    <option value="NM">NM</option>
                                    <option value="NY">NY</option>
                                    <option value="NC">NC</option>
                                    <option value="ND">ND</option>
                                    <option value="OH">OH</option>
                                    <option value="OK">OK</option>
                                    <option value="OR">OR</option>
                                    <option value="PA">PA</option>
                                    <option value="RI">RI</option>
                                    <option value="SC">SC</option>
                                    <option value="SD">SD</option>
                                    <option value="TN">TN</option>
                                    <option value="TX">TX</option>
                                    <option value="UT">UT</option>
                                    <option value="VT">VT</option>
                                    <option value="VA">VA</option>
                                    <option value="WA">WA</option>
                                    <option value="WV">WV</option>
                                    <option value="WI">WI</option>
                                    <option value="WY">WY</option>
                                </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Zip Code:</td>
                            <td><input name="zip" type="text" id="zip">
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp </td>
                    </tr>
                    <tr>
                        <td class="head"  colspan="2" align="left">Contact Information</td>
                    </tr>
                    <tr>
                        <td>E-mail:</td>
                            <td><input name="email" type="text" id="email">
                        </td>
                    </tr>
                    <tr>
                        <td>Re-enter E-mail:</td>
                            <td><input name="email2" type="text" id="email2">
                        </td>
                    </tr>
                    <tr>
                        <td>Phone:</td>
                            <td><input name="phone" type="text" id="phone">
                        </td>
                    </tr>
                    <tr>
                        <td>Fax:</td>
                            <td><input name="fax" type="text" id="fax">
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp </td>
                    </tr>
                    <tr>
                        <td class="head" colspan="2" align="left">Account Information</td>
                    </tr>
                    <tr>
                        <td>Username:</td>
                            <td><input name="username" type="text" id="username">
                        </td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                            <td><input name="password" type="password" id="password">
                        </td>                       
                    </tr>
                    <tr>
                        <td>Confirm Password:</td>
                            <td><input name="password2" type="password" id="password2">
                        </td>                       
                    </tr>
                    <tr>
                        <td>Profile Picture:</td>
                            <td><input name="profpic" type="file" id="profpic">
                        </td>
                    </tr>
                    <tr>
                        <td>
                        <input type="hidden" name="type" id="type" value="1">
                            <input type="submit" value="SUBMIT">
                        </td>
                    </tr>                   
                    </form>
                </table>
                    
        </div>
        
        <div id="rightsidebar">
            <?php include('inc/sidebar.php'); ?>
        </div>
    </div>
            
    </body>
</html>?>

In case i wasnt that clear i want a picture that is 150x150 to be made out of there image and uploadt to images/users/theireusername

 

heres is the picture part alone

<?php //Picture
                mkdir("../webdev/images/users/$username", 0777, true);
                $uploaddir = "../webdev/images/users/$username/";
                $allowed_ext = "jpg";
                $max_size = "5000000";
                $max_height = "10000";
                $max_width = "10000";
        // Check Entension
                $extension = pathinfo($_FILES['profpic']['name']);
                $extension = $extension[extension];
                $allowed_paths = explode(", ", $allowed_ext);
                for($i = 0; $i < count($allowed_paths); $i++) {
                 if ($allowed_paths[$i] == "$extension") {
                 $ok = "1";
                 }
                }
        // Check File Size
                if ($ok == "1") {
                if($_FILES['profpic']['size'] > $max_size)
                {
                print "File size is too big!";
                exit;
                }
        // Check Height & Width
                if ($max_width && $max_height) {
                list($width, $height, $type, $w) = getimagesize($_FILES['profpic']['tmp_name']);
                if($width > $max_width || $height > $max_height)
                {
                print "File height and/or width are too big!";
                exit;
                }
                }
                
                // The Upload Part
                if(is_uploaded_file($_FILES['profpic']['tmp_name']))
                {
                move_uploaded_file($_FILES['profpic']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
                $Link = $_FILES['profpic']['name'];
                
                //Name you want to save your file as
                $save = '../images/users/'. $username .'/default.jpg';

                $file = $_FILES['profpic']['name'];
                header('Content-type: image/jpeg') ;
                list($width, $height) = getimagesize($file) ;
                $modwidth = 150;
                $modheight = 150;
                $tn = imagecreatetruecolor($modwidth, $modheight) ;
                $image = imagecreatefromjpeg($file) ;
                imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

                // Here we are saving the .jpg, you can make this gif or png if you want
                //the file name is set above, and the quality is set to 100%
                imagejpeg($tn, $save, 100) ;
?>

 

Im thinking it may have to do ith the $_FILE['profpic']['name'] part but i dont really know

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.