Img.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Img extends Admin_Controller {
  3. function __construct(){
  4. parent::__construct();
  5. $this->load->library('upload');
  6. }
  7. public function _remap($arg)
  8. {
  9. if($arg == 'textimg')
  10. {
  11. $this->_textimg();
  12. }
  13. else if($arg == 'thumb')
  14. {
  15. $this->_thumb();
  16. }
  17. else
  18. {
  19. $this->_index();
  20. }
  21. }
  22. //上传图片
  23. public function _index()
  24. {
  25. $dir = '/data/img/'.date('Ymd',time()).'/';
  26. $config['upload_path'] = '.'.$dir ;
  27. $config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
  28. $config['allowed_types'] = '*';
  29. $config['max_size'] = 51200;
  30. $this->load->library('upload', $config);
  31. $this->upload->initialize($config);
  32. if ($this->upload->do_upload('userfile'))
  33. {
  34. $data = $this->upload->data();
  35. // 获取原文件名和文件后缀
  36. $file_name = $data['file_name'];
  37. $file_ext = strtolower($data['file_ext']); // 将后缀变为小写
  38. // 生成新的文件名
  39. $new_file_name = $data['raw_name'] . $file_ext;
  40. // 重命名文件
  41. rename($data['full_path'], $data['file_path'] . $new_file_name);
  42. $full_path = $dir.$new_file_name;
  43. echo json_encode(array('ok'=>1,'lx'=>$file_ext,'filename'=>$full_path));
  44. }
  45. else
  46. {
  47. $cw = str_replace(array('<p>','</p>'),'',$this->upload->display_errors());
  48. echo json_encode(array('msg'=>$cw));
  49. }
  50. }
  51. //编辑器上传图
  52. public function _textimg()
  53. {
  54. $dir = '/data/img/'.date('Ymd',time()).'/';
  55. $config['upload_path'] = '.'.$dir ;
  56. $config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
  57. $config['allowed_types'] = 'gif|jpg|png';
  58. $config['max_size'] = 51200;
  59. $field_name = "upfile";
  60. $this->load->library('upload', $config);
  61. $this->upload->initialize($config);
  62. if ($this->upload->do_upload($field_name))
  63. {
  64. $name = $this->upload->data('file_name'); //重命名后的图片名称+后缀
  65. $client_name = $this->upload->data('client_name');//提交的图片名称+后缀
  66. $file_ext = $this->upload->data('file_ext');//图片后缀
  67. $file_size = $this->upload->data('file_size');//图片大小
  68. $full_path = $dir.$this->upload->data('file_name');//图片上传的完整路径+名称+后缀
  69. echo json_encode(array('errno'=>0,'data'=>array('url'=>'http://a.wepolicy.cn'.$full_path)));
  70. }
  71. else
  72. {
  73. echo json_encode(array('errno'=>1,'data'=>array('message'=>$this->upload->display_errors())));
  74. }
  75. }
  76. public function _thumb()
  77. {
  78. $this->load->library('thumb');
  79. $test=new thumb();
  80. $src = FCPATH.$this->input->get('src');
  81. $w = $this->input->get('w');
  82. $h = $this->input->get('h');
  83. $a = $this->input->get('a');
  84. $test->SetVar($src,'link');
  85. $test->PRorate($src,$w,$h);
  86. }
  87. }