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

本文实例为大家详细介绍了php实现图片上传、剪切功能的具体代码,供大家参考,具体内容如下:

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

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。