PublicFileAction.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维p2p借贷系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. class PublicFileAction extends BaseAction
  10. {
  11. public function do_upload()
  12. {
  13. if (intval($_REQUEST['upload_type']) == 0) {
  14. $result = $this->uploadFile();
  15. } else {
  16. $result = $this->uploadImage($_REQUEST['thumb']);
  17. }
  18. if ($result['status'] == 1) {
  19. $list = $result['data'];
  20. if (intval($_REQUEST['upload_type']) == 0) {
  21. $file_url = "." . $list[0]['recpath'] . $list[0]['savename'];
  22. } else {
  23. $file_url = "." . $list[0]['bigrecpath'] . $list[0]['savename'];
  24. $thumb_url = "." . $list[0]['thumbpath'] . $list[0]['savename'];
  25. }
  26. $public_url = $file_url;
  27. if ($GLOBALS['distribution_cfg']['OSS_TYPE'] && $GLOBALS['distribution_cfg']['OSS_TYPE'] != 'NONE') {
  28. if ($_REQUEST['dir'] != 'file') {
  29. syn_to_remote_image_server($file_url);
  30. }
  31. }
  32. $avatar_data = json_decode(stripslashes($_POST['avatar_data']), true);
  33. if (!empty($avatar_data)) {
  34. $x = intval($avatar_data['x']);
  35. $y = intval($avatar_data['y']);
  36. $width = intval($avatar_data['width']);
  37. $height = intval($avatar_data['height']);
  38. $rotate = intval($avatar_data['rotate']);
  39. $file_url = get_cut_image($file_url, $x, $y, $width, $height, $rotate);
  40. }
  41. $response = array(
  42. 'state' => 200,
  43. 'message' => $result['info'],
  44. 'result' => $file_url,
  45. 'fullname' => get_spec_image($file_url),
  46. 'dst' => strim($_REQUEST['dst']),
  47. );
  48. ajax_file_return($response);
  49. } else {
  50. $response = array(
  51. 'state' => 0,
  52. 'message' => $result['info'],
  53. 'result' => null,
  54. 'dst' => strim($_REQUEST['dst']),
  55. );
  56. ajax_file_return($response);
  57. }
  58. }
  59. public function do_cut_upload()
  60. {
  61. require_once APP_ROOT_PATH . "system/utils/crop.php";
  62. $width = floatval($_REQUEST['w']);
  63. $height = floatval($_REQUEST['h']);
  64. $dst = strim($_REQUEST['dst']);
  65. $crop = new CropAvatar($_POST['avatar_src'], $_POST['avatar_data'], $_FILES['avatar_file'], $width, $height);
  66. $response = array(
  67. 'state' => 200,
  68. 'message' => $crop->getMsg(),
  69. 'result' => $crop->getResult(),
  70. 'dst' => $dst
  71. );
  72. echo json_encode($response);
  73. }
  74. /**
  75. * 上传图片的通公基础方法
  76. *
  77. * @return array
  78. */
  79. protected function uploadImage($thumb)
  80. {
  81. if (conf("WATER_MARK") != "") {
  82. $water_mark = get_real_path() . conf("WATER_MARK"); //水印
  83. } else {
  84. $water_mark = "";
  85. }
  86. $alpha = conf("WATER_ALPHA"); //水印透明
  87. $place = conf("WATER_POSITION"); //水印位置
  88. $upload = new UploadFile();
  89. //设置上传文件大小
  90. $upload->maxSize = conf('MAX_IMAGE_SIZE'); /* 配置于config */
  91. //设置上传文件类型
  92. $upload->allowExts = explode(',', conf('ALLOW_IMAGE_EXT')); /* 配置于config */
  93. $dir_name = to_date(get_gmtime(), "Ym");
  94. if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) {
  95. @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name);
  96. @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777);
  97. }
  98. $dir_name = $dir_name . "/" . to_date(get_gmtime(), "d");
  99. if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) {
  100. @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name);
  101. @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777);
  102. }
  103. $dir_name = $dir_name . "/" . to_date(get_gmtime(), "H");
  104. if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) {
  105. @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name);
  106. @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777);
  107. }
  108. $save_rec_Path = "/public/attachment/" . $dir_name . "/origin/"; //上传时先存放原图
  109. $savePath = APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/"; //绝对路径
  110. if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/")) {
  111. @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/");
  112. @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name . "/origin/", 0777);
  113. }
  114. $domain_path = get_domain() . APP_ROOT . $save_rec_Path;
  115. $upload->saveRule = "uniqid"; //唯一
  116. $upload->savePath = $savePath;
  117. if ($thumb == 1) {
  118. $upload->thumb = true;
  119. $upload->thumbMaxWidth = 40;
  120. $upload->thumbMaxHeight = 50;
  121. }
  122. if ($upload->upload()) {
  123. $uploadList = $upload->getUploadFileInfo();
  124. foreach ($uploadList as $k => $fileItem) {
  125. $file_name = $fileItem['savepath'] . $fileItem['savename']; //上图原图的地址
  126. //水印图
  127. $big_save_path = str_replace("origin/", "", $savePath); //大图存放图径
  128. $big_file_name = str_replace("origin/", "", $file_name);
  129. // Image::thumb($file_name,$big_file_name,'',$big_width,$big_height);
  130. @file_put_contents($big_file_name, @file_get_contents($file_name));
  131. if (file_exists($water_mark)) {
  132. Image::water($big_file_name, $water_mark, $big_file_name, $alpha, $place);
  133. }
  134. $big_save_rec_Path = str_replace("origin/", "", $save_rec_Path); //上传的图存放的相对路径
  135. $uploadList[$k]['recpath'] = $save_rec_Path;
  136. $uploadList[$k]['bigrecpath'] = $big_save_rec_Path;
  137. $last_index = strrpos($save_rec_Path, '/') + 1;
  138. $uploadList[$k]['thumbpath'] = substr($save_rec_Path, 0,
  139. $last_index) . 'thumb_' . substr($save_rec_Path, $last_index);
  140. // if(app_conf("PUBLIC_DOMAIN_ROOT")!='')
  141. // {
  142. // $origin_syn_url = app_conf("PUBLIC_DOMAIN_ROOT")."/es_file.php?username=".app_conf("IMAGE_USERNAME")."&password=".app_conf("IMAGE_PASSWORD")."&file=".get_domain().APP_ROOT."/public/attachment/".$dir_name."/origin/".$fileItem['savename']."&path=attachment/".$dir_name."/origin/&name=".$fileItem['savename']."&act=0";
  143. // $big_syn_url = app_conf("PUBLIC_DOMAIN_ROOT")."/es_file.php?username=".app_conf("IMAGE_USERNAME")."&password=".app_conf("IMAGE_PASSWORD")."&file=".get_domain().APP_ROOT."/public/attachment/".$dir_name."/".$fileItem['savename']."&path=attachment/".$dir_name."/&name=".$fileItem['savename']."&act=0";
  144. // @file_get_contents($origin_syn_url);
  145. // @file_get_contents($big_syn_url);
  146. // }
  147. }
  148. return array("status" => 1, 'data' => $uploadList, 'info' => L("UPLOAD_SUCCESS"));
  149. } else {
  150. return array("status" => 0, 'data' => null, 'info' => $upload->getErrorMsg());
  151. }
  152. }
  153. /**
  154. * 上传文件公共基础方法
  155. *
  156. * @return array
  157. */
  158. protected function uploadFile()
  159. {
  160. $upload = new UploadFile();
  161. $ext_arr = array(
  162. 'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
  163. 'flash' => array('swf', 'flv'),
  164. 'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb', 'mp4', 'flv'),
  165. 'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'txt', 'zip', 'rar', 'pdf'),
  166. );
  167. //设置上传文件大小
  168. $upload->maxSize = conf('MAX_IMAGE_SIZE'); /* 配置于config */
  169. //设置上传文件类型
  170. if (!empty($ext_arr[$_REQUEST['dir']])) {
  171. $upload->allowExts = $ext_arr[$_REQUEST['dir']];
  172. } else {
  173. $upload->allowExts = explode(',', conf('ALLOW_IMAGE_EXT')); /* 配置于config */
  174. }
  175. $dir_name = to_date(get_gmtime(), "Ym");
  176. if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) {
  177. @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name);
  178. @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777);
  179. }
  180. $dir_name = $dir_name . "/" . to_date(get_gmtime(), "d");
  181. if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) {
  182. @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name);
  183. @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777);
  184. }
  185. $dir_name = $dir_name . "/" . to_date(get_gmtime(), "H");
  186. if (!is_dir(APP_ROOT_PATH . "public/attachment/" . $dir_name)) {
  187. @mkdir(APP_ROOT_PATH . "public/attachment/" . $dir_name);
  188. @chmod(APP_ROOT_PATH . "public/attachment/" . $dir_name, 0777);
  189. }
  190. $save_rec_Path = "/public/attachment/" . $dir_name . "/"; //上传时先存放原图
  191. $savePath = APP_ROOT_PATH . "public/attachment/" . $dir_name . "/"; //绝对路径
  192. $domain_path = get_domain() . APP_ROOT . $save_rec_Path;
  193. $upload->saveRule = "uniqid"; //唯一
  194. $upload->savePath = $savePath;
  195. if ($upload->upload()) {
  196. $uploadList = $upload->getUploadFileInfo();
  197. foreach ($uploadList as $k => $fileItem) {
  198. $uploadList[$k]['recpath'] = $save_rec_Path;
  199. if (app_conf("PUBLIC_DOMAIN_ROOT") != '') {
  200. $syn_url = app_conf("PUBLIC_DOMAIN_ROOT") . "/es_file.php?username=" . app_conf("IMAGE_USERNAME") . "&password=" . app_conf("IMAGE_PASSWORD") . "&file=" . $domain_path . $fileItem['savename'] . "&path=attachment/" . $dir_name . "/&name=" . $fileItem['savename'] . "&act=0";
  201. @file_get_contents($syn_url);
  202. }
  203. }
  204. return array("status" => 1, 'data' => $uploadList, 'info' => L("UPLOAD_SUCCESS"));
  205. } else {
  206. return array("status" => 0, 'data' => null, 'info' => $upload->getErrorMsg());
  207. }
  208. }
  209. }
  210. ?>