Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. **Profile Controller (/application/controllers/profile.php)**
  2.  
  3. function picture() {
  4.  
  5. /* Loads the Tank Auth "users" model, and assigns it the alias "foo" */
  6. $this->load->model('tank_auth/users','foo');
  7.  
  8. /* Gets the logged-in user's ID, from preloaded Tank Auth library */
  9. $user_id = $this->tank_auth->get_user_id();
  10.  
  11. /* Queries profile information for the user who's logged in ...
  12. ... including the profile pictures (see the table structure below) */
  13. $data = $this->foo->get_profile_by_id($user_id);
  14.  
  15. /* Passes that data to the header, menu, form, and footer views */
  16. $this->load->view('templates/header', $data);
  17. $this->load->view('templates/menu', $data);
  18. /* Loads the upload_form view */
  19. $this->load->view('upload_form', array('error' => ' ' ));
  20. $this->load->view('templates/footer', $data);
  21.  
  22. }
  23. }
  24.  
  25. function do_upload() {
  26.  
  27. /* Upload Settings */
  28. $config['upload_path'] = './uploads/';
  29. $config['allowed_types'] = 'gif|jpg|png';
  30. $config['max_size'] = '1024';
  31. $config['max_width'] = '1024';
  32. $config['width'] = '128';
  33. $config['max_height'] = '768';
  34. /* Encrypting helps prevent the file name from being discerned once its saved */
  35. $config['encrypt_name'] = 'TRUE';
  36.  
  37. /* Load the CodeIgniter upload library, feed it the config from above */
  38. $this->load->library('upload', $config);
  39.  
  40. /* Checks if the do_upload function has been successfully executed ...
  41. ... and if not, shows the upload form and any errors (if they exist) */
  42. if (!$this->upload->do_upload()){
  43.  
  44. /* Loads the model (predefined database instructions, see below) ...
  45. ... and assigns it a nickname, I went with 'foo' */
  46. $this->load->model('tank_auth/users','foo');
  47.  
  48. /* Makes the logged-in user's id a nice, clean variable */
  49. $user_id = $this->tank_auth->get_user_id();
  50.  
  51. /* Use the model to gather all user profile information for that user_id */
  52. $profile_data = (array) $this->foo->get_profile_by_id($user_id);
  53.  
  54. /* Pass that data into the data variable (for the views) */
  55. $data = $profile_data;
  56.  
  57. /* Process errors if they exist */
  58. $error = array('error' => $this->upload->display_errors());
  59.  
  60. /* Pass everything into the views */
  61. $this->load->view('templates/header', $data);
  62. $this->load->view('templates/menu', $data);
  63. $this->load->view('upload_form', $error);
  64. $this->load->view('templates/footer', $data);
  65.  
  66. /* ... if the file passes validation ... */
  67. } else {
  68.  
  69. /* Load the users model, assign it alias 'foo' (or whatever you want) */
  70. $this->load->model('tank_auth/users','foo');
  71.  
  72. /* Assign logged-in user ID to a nice, clean variable */
  73. $user_id = $this->tank_auth->get_user_id();
  74.  
  75. /* Assign the upload's metadata (size, dimensions, destination, etc.) ...
  76. ... to an array with another nice, clean variable */
  77. $upload = (array) $this->upload->data();
  78.  
  79. /* Assign's the user's profile data to yet another nice, clean variable */
  80. $profile_data = (array) $this->foo->get_profile_by_id($user_id);
  81.  
  82. /* Uses two upload library features to assemble the file name (the name, and extension) */
  83. $filename = $upload['raw_name'].$upload['file_ext'];
  84.  
  85. /* Same with the thumbnail we'll generate, but with the suffix '_thumb' */
  86. $thumb = $upload['raw_name']."_thumb".$upload['file_ext'];
  87.  
  88. /* Set the rules for the upload */
  89. $config['image_library'] = 'gd2';
  90. $config['source_image'] = "./uploads/".$filename;
  91. $config['create_thumb'] = TRUE;
  92. $config['maintain_ratio'] = TRUE;
  93. $config['width'] = 128;
  94. $config['height'] = 128;
  95.  
  96. /* Load "image manipulation library", see CodeIgniter user guide */
  97. $this->load->library('image_lib', $config);
  98.  
  99. /* Resize the image! */
  100. $this->image_lib->resize();
  101.  
  102. /* Assign upload_data to $data variable */
  103. $data['upload_data'] = $this->upload->data();
  104.  
  105. /* Assign profile_data to $data variable */
  106. $data['profile_data'] = $profile_data;
  107.  
  108. /* Runs the users model (update_photo function, see below) and ...
  109. ... loads the location of the photo new photo into user's profile */
  110. $this->foo->update_photo($user_id, $filename, $thumb);
  111.  
  112. /* Load "success" view with all the data! */
  113. $this->load->view('upload_success', $data);
  114.  
  115. }
  116. }
  117.  
  118. **The Upload Form View (/application/views/upload_form.php)**
  119.  
  120. <h1>Change Profile Picture</h1>
  121. <?php echo $error;?>
  122. <?php echo form_open_multipart('profile/do_upload');?>
  123. <input type="file" name="userfile" size="20" />
  124. <input type="submit" value="Upload New Picture" />undefined</form>
  125.  
  126. **User Model (/application/models/tank_auth/users.php)**
  127. function update_photo($user_id, $filename, $thumb){
  128. $this->db->where('user_id', $user_id);
  129. $arr = array(
  130. 'photo'=> $filename,
  131. 'thumb' => $thumb
  132. );
  133. $this->db->update($this->profile_table_name, $arr);
  134. }
  135.  
  136. **Displaying the Profile Picture (/application/views/menu.php)**
  137.  
  138. <div style='width: 128px; height:
  139. <?php echo $size[1]; ?>px; margin-bottom: 10px; border: solid 1px #ccc; background: url("http://thc.fiu.edu/uploads/
  140. <?php echo $thumb; ?>");'/>
  141. <?php $size = getimagesize("http://www.yourwebsite.com/uploads/".$thumb); ?>
  142. <a href="/profile/picture" class='editprofile' style="display: block; width: 128px; height:
  143. <?php echo $size[1]; ?>px">&nbsp;
  144. </a>
  145. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement