Switch to full style
Dynamic open source server-side web development
Post a reply

Change Image File Size (Height And Width) While Uploading

Thu Sep 18, 2014 11:42 am

Thanks for the support really appreciated.

I am newbie in PHP, and i heard that i can find my solution from those PHP expert who are here in CODEMILES.

I have bought this script a while ago and now the producer stopped offering support.
http://codecanyon.net/item/php-security-user-management/141801?WT.ac=solid_search_item&WT.seg_1=solid_search_item&WT.z_author=hunzonian

This is the Avatar upload form
http://i.stack.imgur.com/YO7PD.jpg

My Question

The script have ability to upload profile for every user but it doesn't resize the image.
If a user upload a 2 mb image so the script use 2 mb image in all over the website which makes my website to run slower.

I want that the script should resize the image to

([width=100px and height=auto] and

[width=19px and height=auto])

so i use a lighter image in size (like ~150 kb and ~55kb) and let my site run faster.

This is the avatar.php file that process the uploading

Code:
<?php
    // declare variables
    $msg = '';
    $f_avatar_image = '';
    // ------------------------------------------------------------
    // UPLOAD AVATAR
    // ------------------------------------------------------------
    if(isset($_POST['btnUploadAvatar']) && !empty($_FILES['fileUpload']['name']))
    {
       // create variables
       $avatar_directory = AVATAR_FILE_DIRECTORY;
       $file_name = $_FILES['fileUpload']['name'];
       $file_type = $_FILES['fileUpload']['type'];
       $file_size = $_FILES['fileUpload']['size'];
       $file_size_limit = AVATAR_FILE_SIZE;
       $calc_kilobites = 1024;
       $file_size_kb = round($file_size / $calc_kilobites, 2);
       $temp_file_name = $_FILES['fileUpload']['tmp_name'];
       $upload_error = $_FILES['fileUpload']['error'];
       
       
       // create unique file name
       $unique_file_name = $user_name.'-'.$file_name;
       $avatar_img_url = AVATAR_IMAGE_URL.$user_name.'-'.$file_name;
   
       // if upload error display error message
       if($upload_error > 0)
       {
          echo 'ERROR:' . $upload_error;
       }
   
       // if no upload error - check for file types
       if($upload_error == 0 &&
       $file_type == 'image/gif' ||
       $file_type == 'image/jpeg' ||
       $file_type == 'image/png' )
       {
          // if file size is within limits
          if($file_size <= $file_size_limit)
          {
             // move uploaded file to assigned directory
             if(move_uploaded_file($temp_file_name, $avatar_directory . $unique_file_name))
             
             {
                // get user id
                $get_user_id = mysqli_query($conn, "SELECT UserId FROM users WHERE UserName = '$user_name' Limit 1") or die($dataaccess_error);
   
                // if user id exist
                if(mysqli_num_rows($get_user_id) == 1 )
                {
                   $row = mysqli_fetch_array($get_user_id);
                   $user_id = $row['UserId'];
   
                   // check if user profile already exist
                   $check_user_profile = mysqli_query($conn, "SELECT UserId FROM profiles WHERE UserName = '$user_name' Limit 1") or die($dataaccess_error);
   
                   // if user profile exist - update
                   if(mysqli_num_rows($check_user_profile) == 1 )
                   {
                      // update profiles
                      $update_profile = mysqli_query($conn, "UPDATE profiles SET AvatarImage = '$avatar_img_url' WHERE UserName = '$user_name'") or die($dataaccess_error);
   
                      if(mysqli_affected_rows($conn) > 0)
                      {
                         echo 'Upload Success! <br/>';
                         echo 'File Name: '.$file_name.'<br/>';
                         echo 'File Type: '.$file_type.'<br/>';
                         echo 'File Size: '.$file_size_kb.' Kb <br/>';
                         $msg = $profile_update_success;
                      }
                      else
                      {
                         $msg = $profile_update_failed;
                      }
                   }
                   else
                   {
                      // create profile
                      $insert_profile = mysqli_query($conn, "INSERT INTO profiles(UserId,UserName,AvatarImage) VALUES($user_id,'$user_name','$avatar_img_url')") or die($dataaccess_error);
   
                      if(mysqli_affected_rows($conn) > 0)
                      {
                         echo 'Upload Success! <br/>';
                         echo 'File Name: '.$file_name.'<br/>';
                         echo 'File Type: '.$file_type.'<br/>';
                         echo 'File Size: '.$file_size_kb.' Kb <br/>';
                         $msg = $profile_update_success;
                      }
                      else
                      {
                         $msg = $profile_create_failed;
                      }
                   }
                }
                else
                {
                   // user id not found
                   $msg = $profile_update_failed2;
                }
             }
             else
             {
                $msg = $avatar_upload_failed;
             }
          }
          else
          {
             $msg = $avatar_file_too_large;
          }
       }
       else
       {
          $msg = $avatar_wrong_file_type;
       }
    }
    elseif(isset($_POST['btnUploadAvatar']) && empty($_FILES['fileUpload']['name']))
    {
       $msg = $avatar_empty;
    }
   
   
    // ------------------------------------------------------------
    // DISPLAY AVATAR ON PAGE LOAD
    // ------------------------------------------------------------
    if($user_name)
    {
       // get user id
       $get_avatar_image = mysqli_query($conn, "SELECT AvatarImage FROM profiles WHERE UserName = '$user_name' Limit 1") or die($dataaccess_error);
   
       if(mysqli_num_rows($get_avatar_image) == 1)
       {
          $row = mysqli_fetch_array($get_avatar_image);
          if($row['AvatarImage'] != 'NULL' && $row['AvatarImage'] != '')
          {
             $f_avatar_image = $row['AvatarImage'];
          }
          else
          {
             $f_avatar_image = AVATAR_IMAGE_URL.DEFAULT_AVATAR_IMAGE;
          }
       }
       else
       {
          $f_avatar_image = AVATAR_IMAGE_URL.DEFAULT_AVATAR_IMAGE;
       }
    }
    ?>


This is the avatar.html.php file form

Code:
<?php require_once(ROOT_PATH.'user/modules/accordion/avatar.php'); ?>
    <div class="profileWrap">
      <form name="frmAvatar" method="post" action="" enctype="multipart/form-data" class="htmlForm">
        <div class="infoBanner2">
          <p>REQUIREMENTS:  File Size: <?php echo AVATAR_FILE_SIZE / 1024 ?> kb max. File Type: gif, jpg, png</p>
        </div>
        <!-- error msgs -->
        <ul>
        <?php echo $msg; ?>
        </ul>
        <p><input name="selectFile" type="image" src="<?php echo $f_avatar_image; ?>" class="img"></p>
        <p><label for="fileUpload">Avatar Image:</label><input name="fileUpload" type="file" id="fileUpload" maxlength="255" ></p>
        <input name="btnUploadAvatar" type="submit" value="Upload" class="gvbtn btn" onclick="return confirm('Are You READY to UPLOAD?');"/>
      </form>
    </div>


The avatar.php file is linked to a configuration file (web.config.php) file

Code:
// ------------------------------------------------------------
    // 16. AVATAR IMAGE FILE
    // ------------------------------------------------------------
    define('AVATAR_FILE_SIZE', 2097152); // 50 Kb max. -> 1 kilobyte = 1024 bytes
    define('AVATAR_FILE_DIRECTORY', ROOT_PATH.'user/upload/avatars/'); // upload directory
    define('AVATAR_IMAGE_URL', SITE_URL.'user/upload/avatars/'); // default avatar url
    define('DEFAULT_AVATAR_IMAGE', 'default-avatar.png'); // default avatar image



If you needed to ask anything i am ready to answer.
Let me thank the one who answer it.



Attachments
Capture.JPG
Avatar Upload form
Capture.JPG (25.47 KiB) Viewed 17694 times

Post a reply
  Related Posts  to : Change Image File Size (Height And Width) While Uploading
 draws a big image scaled to its width-height as specified     -  
 Control HR tag width and height     -  
 Get Width and Height -Inner and Outer for HTML element     -  
 Set image size as a percentage of the page size     -  
 Downloading & Uploading File through Oracle Database     -  
 uploading text file to java program     -  
 change font size in JavaScript     -  
 How to use Javascript animation to change the size of a div?     -  
 Auto image size     -  
 show uploaded file name, size and type     -