Codeigniter multi-file upload and thumbnail implementation code

WBOY
Release: 2016-07-25 08:53:08
Original
797 people have browsed it
  1. class Upload extends Controller {

  2. function go() {
  3. if(isset($_POST['go'])) {
  4. //初始化
  5. $config['upload_path'] = 'album/source';
  6. $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
  7. $config['encrypt_name'] = TRUE;
  8. $config['remove_spaces'] = TRUE;
  9. $config['max_size'] = '0';
  10. $config['max_width'] = '0';
  11. $config['max_height'] = '0';

  12. $this->load->library('upload', $config);

  13. //170*170图片

  14. $configThumb = array();
  15. $configThumb['image_library'] = 'gd2';
  16. $configThumb['source_image'] = '';
  17. $configThumb['create_thumb'] = TRUE;
  18. $configThumb['maintain_ratio'] = TRUE; //保持图片比例
  19. $configThumb['new_image'] = 'album/thumb';
  20. $configThumb['width'] = 170;
  21. $configThumb['height'] = 170;
  22. //600*600图片
  23. $configLarge = array();
  24. $configLarge['image_library'] = 'gd2';
  25. $configLarge['source_image'] = '';
  26. $configLarge['create_thumb'] = TRUE;
  27. $configLarge['maintain_ratio'] = TRUE; //保持图片比例
  28. $configLarge['new_image'] = 'album/large';
  29. $configLarge['width'] = 600;
  30. $configLarge['height'] = 600;

  31. $this->load->library('image_lib');

  32. for($i = 1; $i < 6; $i++) {

  33. $upload = $this->upload->do_upload('image'.$i);
  34. if($upload === FALSE) continue;
  35. $data = $this->upload->data();//返回上传文件的所有相关信息的数组
  36. $uid = $this->session->userdata('uid');
  37. $uploadedFiles[$i] = $data;

  38. if($data['is_image'] == 1) {

  39. //初始化170*170
  40. $configThumb['source_image'] = $data['full_path']; //文件路径带文件名
  41. $this->image_lib->initialize($configThumb);
  42. $this->image_lib->resize();
  43. //初始化600*600
  44. $configLarge['source_image'] = $data['full_path']; //文件路径带文件名
  45. $this->image_lib->initialize($configLarge);
  46. $this->image_lib->resize();
  47. }

  48. //插入图片信息到album表,插入的文件名为source目录文件名

  49. $picture = array(
  50. 'filename' => $data['file_name'],
  51. 'albumID' => $this->uri->segment(4,0),
  52. 'uid' => $this->session->userdata('uid'),
  53. 'dateline' => time(),
  54. 'describe' => '',
  55. 'click' => 0
  56. );

  57. $this->load->model('album_model');

  58. $this->album_model->AddPic($picture);
  59. $picture = array();
  60. }
  61. }
  62. /* 转出 */
  63. $albumID = $this->uri->segment(4);
  64. $backurl = site_url() . 'photo/editpic/album/' .$albumID;
  65. $this->session->set_flashdata('msg','图片上传成功.');
  66. redirect($backurl,'refresh');
  67. }
  68. }

复制代码

2,views:new_pic.view文件:







复制代码

type="submit" name="go" value="Upload photos" class="button" />

Notes: 1. To upload several files at a time, just modify the parameters in the loop part of the form and controller. 2. album\source is the original image directory after uploading. large and thumb are the directories where thumbnails are stored after executing $this->image_lib->resize(); twice. 3. If the thumbnail file name needs to be consistent with the album\source directory, please add the parameter $config['thumb_marker'] = '';. 4. The $picture part of the array is what is saved to the database and does not need to be ignored.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!