php基于CodeIgniter实现图片上传、剪切功能

这篇文章主要为大家详细介绍了php基于CodeIgniter实现图片上传、剪切功能,具有参考价值,感兴趣的朋友可以参考一下。

本文实例为大家分享了codeigniter 图片上传、剪切,控制器类,供大家参考,具体内容如下:

  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class Index extends MY_Controller {
  4. function __construct(){
  5. parent::__construct();
  6. $this->load->helper(array('form', 'url'));
  7. }
  8. /**
  9. * 首页
  10. */
  11. public function index() {
  12. $this->load->view('upload_form', array('error' => ' ' ));
  13. }
  14. public function do_upload()
  15. {
  16. $config['upload_path'] = './data/uploads/';
  17. $config['allowed_types'] = 'gif|jpg|png';
  18. $config['max_size'] = 100;
  19. $config['max_width'] = 1024;
  20. $config['max_height'] = 768;
  21. $this->load->library('upload', $config);
  22. if ( ! $this->upload->do_upload('userfile'))
  23. {
  24. $error = array('error' => $this->upload->display_errors());
  25. $this->load->view('upload_form', $error);
  26. }
  27. else
  28. {
  29. $data = array('upload_data' => $this->upload->data());
  30. $this->load->library('image_lib');
  31. list($width, $height) = getimagesize($data['upload_data']['full_path']);
  32. $config['image_library'] = 'gd2';
  33. $config['source_image'] = $data['upload_data']['full_path'];
  34. $config['maintain_ratio'] = TRUE;
  35. if($width >= $height)
  36. {
  37. $config['master_dim'] = 'height';
  38. }else{
  39. $config['master_dim'] = 'width';
  40. }
  41. $config['width'] = 180;
  42. $config['height'] = 180;
  43. $this->image_lib->initialize($config);
  44. $this->image_lib->resize();
  45. $config['maintain_ratio'] = FALSE;
  46. if($width >= $height)
  47. {
  48. $config['x_axis'] = floor(($width * 180 / $height - 180)/2);
  49. }else{
  50. $config['y_axis'] = floor(($height * 180 / $width - 180)/2);
  51. }
  52. $this->image_lib->initialize($config);
  53. $this->image_lib->crop();
  54. $this->load->view('upload_success', $data);
  55. }
  56. }
  57. }