ImgUpload.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * 图片上传
  4. * User: Mike
  5. * Email: m@9026.com
  6. * Date: 2016/6/21
  7. * Time: 11:45
  8. */
  9. namespace App\Widget\Tools;
  10. use App\Models\CmsLinkModel;
  11. class ImgUpload
  12. {
  13. /**
  14. * 统一上传
  15. * @param $text 按钮文字
  16. * @param $folder 上传文件夹
  17. * @param $id id
  18. * @param $option 附加选项
  19. */
  20. public function upload($id, $name, $folder = '', $option = [])
  21. {
  22. $position = isset($option['position']) ? $option['position'] : 'local';
  23. $maxFileSize = isset($option['maxSize']) ? $option['maxSize'] : 10 * 1024;
  24. $class = isset($option['class']) ? $option['class'] : 1;
  25. $html = <<<EOF
  26. <div class="file-loading">
  27. <input id="$id" name="file[]" type="file" multiple>
  28. </div>
  29. <script>
  30. $('#$id').fileinput({
  31. showPreview: true,
  32. showClose: false,
  33. theme: 'fa',
  34. language: 'zh',
  35. uploadUrl: "/admin/Base/AttachmentInfo/upload",
  36. allowedFileExtensions: ['jpg', 'png', 'gif', 'jpeg'],
  37. enctype: 'multipart/form-data',
  38. maxFileSize: $maxFileSize,
  39. uploadExtraData:{'folder':'$folder','position':'$position','class':'$class'},
  40. });
  41. $('#$id').on("fileuploaded", function (event, data, previewId, index) {
  42. setTimeout(function() {
  43. window.location.reload()
  44. },2000)
  45. })
  46. </script>
  47. EOF;
  48. return $html;
  49. }
  50. /**
  51. * 单个图片上传
  52. * @param $folder
  53. * @param string $position
  54. * @param array $option
  55. */
  56. public function single($id, $name = "data[image]", $file = '')
  57. {
  58. $url = U("/Base/Photos/index");
  59. $imgHtml = "";
  60. if (!empty($file)) {
  61. $imgHtml .= "<div class='file-preview-frame krajee-default kv-preview-thumb file-preview-success'>
  62. <div class='kv-file-content'>
  63. <img src=\"{$file}\" style='width:100%;'/>
  64. <input type='hidden' name=\"{$name}\" value=\"{$file}\">
  65. </div>
  66. <div class='file-thumbnail-footer'>
  67. <div class='file-footer-caption'>
  68. <div class='file-caption-info'></div>
  69. <div class='file-size-info'></div>
  70. </div>
  71. <div class='file-actions'>
  72. <div class='file-footer-buttons'>
  73. <button type='button' onclick='$(this).parent().parent().parent().parent().remove()' class='kv-file-remove btn btn-sm btn-kv btn-default btn-outline-secondary' title='删除文件'><i class='glyphicon glyphicon-trash'></i></button>
  74. </div>
  75. </div>
  76. </div>
  77. </div>";
  78. }
  79. $idbutton = $id . '_button';
  80. $html = <<<EOF
  81. <div style="clear:both">
  82. <div id="$id" class="file-preview">
  83. <div class="file-preview-thumbnails">
  84. $imgHtml
  85. </div>
  86. <div class="clearfix"></div>
  87. </div>
  88. <button class="btn btn-primary" id="$idbutton" type="button">选择图片</button>
  89. </div>
  90. <script type="text/javascript">
  91. $("#$idbutton").click(function() {
  92. layer.open({
  93. type: 2,
  94. title: '选择图片',
  95. shadeClose: true,
  96. shade: 0.8,
  97. area: ['80%', '90%'],
  98. content: '$url',
  99. btn: ['确定'],
  100. yes:function(index, layero){
  101. var body = layer.getChildFrame('body',index);//建立父子联系
  102. var imglist = body.find('div.img-card span.checked');
  103. if(imglist.length > 1){
  104. layer.alert('只能选择单张图片')
  105. }else {
  106. $.each(imglist,function(i) {
  107. $("#$id .file-preview-thumbnails").html("<div class='file-preview-frame krajee-default kv-preview-thumb file-preview-success'><div class='kv-file-content'><img src=" + $(this).attr('data-src') + " style='width:100%' /> <input type='hidden' name='{$name}' value="+ $(this).attr('data-src')+"></div><div class='file-thumbnail-footer'><div class='file-footer-caption'> <div class='file-caption-info'></div><div class='file-size-info'></div></div><div class='file-actions'><div class='file-footer-buttons'><button type='button' class='kv-file-remove btn btn-sm btn-kv btn-default btn-outline-secondary' onclick='$(this).parent().parent().parent().parent().remove()' title='删除文件'><i class='glyphicon glyphicon-trash'></i></button></div></div></div></div>");
  108. })
  109. layer.close(index);
  110. }
  111. }
  112. });
  113. });
  114. </script>
  115. EOF;
  116. return $html;
  117. }
  118. public function single2($id, $name = "data[image]", $file = "", $folder, $option = [])
  119. {
  120. $folder = urlencode($folder);
  121. $position = isset($option['position']) ? $option['position'] : 'local';
  122. $option['width'] = isset($option['width']) ? $option['width'] : "";
  123. $option['height'] = isset($option['height']) ? $option['height'] : "";
  124. $maxFileSize = isset($option['maxSize']) ? $option['maxSize'] : 10 * 1024;
  125. $filename = explode('/', $file);
  126. $filename = end($filename);
  127. $class = isset($option['class']) ? $option['class'] : 1;
  128. $token = csrf_token();
  129. $html = <<<EOF
  130. <div class="form-group">
  131. <div class="file-loading">
  132. <input id="$id" type="file" name="file">
  133. </div>
  134. <input class="$id" type="hidden" name="$name" value="$file">
  135. </div>
  136. <script>
  137. $("#$id").fileinput({
  138. language: 'zh',
  139. showUpload: false,
  140. showCaption: false,
  141. showClose:false,
  142. showRemove:false,
  143. uploadAsync:true,
  144. dropZoneEnabled: false,
  145. uploadUrl: "/admin/Base/AttachmentInfo/upload",
  146. allowedFileExtensions: ['jpg', 'png', 'gif', 'jpeg'],
  147. enctype: 'multipart/form-data',
  148. maxFileSize: $maxFileSize,
  149. overwriteInitial: true,
  150. initialPreviewAsData: true,
  151. initialPreview: [
  152. "$file",
  153. ],
  154. initialPreviewConfig: [
  155. {caption: "$filename", url: "/admin/Base/AttachmentInfo/remove?file=$file"},
  156. ],
  157. uploadExtraData:{'folder':'$folder','position':'$position','class':'$class'},
  158. deleteExtraData:{'_token': '$token'},
  159. }).on("filebatchselected", function(event, files) {
  160. $(this).fileinput("upload");
  161. })
  162. .on("fileuploaded", function(event, data) {
  163. if(data.response)
  164. {
  165. if($('.$id').length){
  166. $('.$id').val(data.response.fileurl)
  167. }else{
  168. $("img[src='"+ data.reader.result +"']").after("<input class='$id' type=\"hidden\" name=\"{$name}\" value=\" "+data.response.fileurl + " \" />")
  169. }
  170. }
  171. })
  172. .on("filesuccessremove",function(event,id) {
  173. url = $('#'+ id + ' input').val();
  174. $.post("/admin/Base/AttachmentInfo/remove", { file: url, _token: "$token" } );
  175. })
  176. .on("filedeleted",function(event,key,xhr,data) {
  177. url = xhr.responseJSON.data
  178. $("input[value='"+ url +"']").remove()
  179. });
  180. </script>
  181. EOF;
  182. return $html;
  183. }
  184. /**
  185. * 统一多张图片
  186. * @param $folder
  187. * @param $id
  188. * @param string $name
  189. * @param array $imgs
  190. * @param null $callBackFun
  191. * @return string
  192. */
  193. public function multi($id, $name = "data[image]", $files = [], $option = [])
  194. {
  195. $url = U("/Base/Photos/index");
  196. $maxCount = isset($option['maxCount']) ? $option['maxCount'] : 0;
  197. $name = $name . '[]';
  198. $imgHtml = "";
  199. if (!empty($files)) {
  200. foreach ($files as $file) {
  201. $imgHtml .= "<div class='file-preview-frame krajee-default kv-preview-thumb file-preview-success'>
  202. <div class='kv-file-content'>
  203. <img src=\"{$file}\" style='width:100%;'/>
  204. <input type='hidden' class='$id' name=\"{$name}\" value=\"{$file}\">
  205. </div>
  206. <div class='file-thumbnail-footer'>
  207. <div class='file-footer-caption'>
  208. <div class='file-caption-info'></div>
  209. <div class='file-size-info'></div>
  210. </div>
  211. <div class='file-actions'>
  212. <div class='file-footer-buttons'>
  213. <button type='button' onclick='$(this).parent().parent().parent().parent().remove()' class='kv-file-remove btn btn-sm btn-kv btn-default btn-outline-secondary' title='删除文件'><i class='glyphicon glyphicon-trash'></i></button>
  214. </div>
  215. </div>
  216. </div>
  217. </div>";
  218. }
  219. }
  220. $idbutton = $id . '_button';
  221. $html = <<<EOF
  222. <div style="clear:both">
  223. <div id="$id" class="file-preview">
  224. <div class="file-preview-thumbnails">
  225. $imgHtml
  226. </div>
  227. <div class="clearfix"></div>
  228. </div>
  229. <button class="btn btn-primary" id="$idbutton" type="button">选择图片</button>
  230. </div>
  231. <script type="text/javascript">
  232. $("#$idbutton").click(function() {
  233. max = '$maxCount'
  234. current_num = $('.$id').length;
  235. if(max > 0 && max != current_num){
  236. max = max - current_num
  237. }
  238. console.log(current_num)
  239. layer.open({
  240. type: 2,
  241. title: '选择图片',
  242. shadeClose: true,
  243. shade: 0.8,
  244. area: ['80%', '90%'],
  245. content: '$url',
  246. btn: ['确定'],
  247. yes:function(index, layero){
  248. var body = layer.getChildFrame('body',index);//建立父子联系
  249. var imglist = body.find('div.img-card span.checked');
  250. if(imglist.length > max){
  251. layer.alert('只能选择'+max+'张图片')
  252. }else{
  253. $.each(imglist,function(i) {
  254. $("#$id .file-preview-thumbnails").append("<div class='file-preview-frame krajee-default kv-preview-thumb file-preview-success'><div class='kv-file-content'><img src=" + $(this).attr('data-src') + " style='width:100%' /> <input class='$id' type='hidden' name='{$name}' value="+ $(this).attr('data-src')+"></div><div class='file-thumbnail-footer'><div class='file-footer-caption'> <div class='file-caption-info'></div><div class='file-size-info'></div></div><div class='file-actions'><div class='file-footer-buttons'><button type='button' class='kv-file-remove btn btn-sm btn-kv btn-default btn-outline-secondary' onclick='$(this).parent().parent().parent().parent().remove()' title='删除文件'><i class='glyphicon glyphicon-trash'></i></button></div></div></div></div>");
  255. })
  256. }
  257. layer.close(index);
  258. }
  259. });
  260. });
  261. </script>
  262. EOF;
  263. return $html;
  264. }
  265. public function multi2($id, $name = "image", $files = "", $folder, $option = [])
  266. {
  267. $folder = urlencode($folder);
  268. $position = isset($option['position']) ? $option['position'] : 'local';
  269. $option['width'] = isset($option['width']) ? $option['width'] : "";
  270. $option['height'] = isset($option['height']) ? $option['height'] : "";
  271. $maxFileSize = isset($option['maxSize']) ? $option['maxSize'] : 10 * 1024;
  272. $maxCount = isset($option['maxCount']) ? $option['maxCount'] : 0;
  273. $class = isset($option['class']) ? $option['class'] : 1;
  274. $imgHtml = "";
  275. $preview = json_encode($files);
  276. $caps = [];
  277. foreach ($files as $k => $file) {
  278. $caps[$k]['caption'] = $file;
  279. $caps[$k]['url'] = "/admin/Base/AttachmentInfo/remove?file=" . $file;
  280. }
  281. $caps = json_encode($caps);
  282. $token = csrf_token();
  283. foreach ($files as $file) {
  284. $imgHtml .= "<input class='$id' type=\"hidden\" name=\"{$name}[]\" value=\"{$file}\" />";
  285. }
  286. $html = <<<EOF
  287. <div class="form-group">
  288. <div class="file-loading">
  289. <input id="$id" type="file" name="file[]" multiple>
  290. </div>
  291. $imgHtml
  292. </div>
  293. <link href="/base/plugins/bootstrap-fileinput/css/fileinput.css" media="all" rel="stylesheet" type="text/css"/>
  294. <script src="/base/plugins/bootstrap-fileinput/js/fileinput.js" type="text/javascript"></script>
  295. <script src="/base/plugins/bootstrap-fileinput/js/locales/zh.js" type="text/javascript"></script>
  296. <script>
  297. var max = '$maxCount';
  298. var c_num = $('.$id').length;
  299. if(max != 0 && max != c_num){
  300. max = max - c_num
  301. }
  302. $("#$id").fileinput({
  303. language: 'zh',
  304. showUpload: false,
  305. showCaption: false,
  306. showClose:false,
  307. showRemove:false,
  308. uploadAsync:true,
  309. dropZoneEnabled: false,
  310. showZoom:false,
  311. uploadUrl: "/admin/Base/AttachmentInfo/upload",
  312. allowedFileExtensions: ['jpg', 'png', 'gif', 'jpeg'],
  313. enctype: 'multipart/form-data',
  314. maxFileSize: $maxFileSize,
  315. maxFileCount: 0,
  316. overwriteInitial: false,
  317. initialPreviewAsData: true,
  318. initialPreview: $preview,
  319. initialPreviewConfig: $caps,
  320. uploadExtraData:{'folder':'$folder','position':'$position','class':'$class'},
  321. deleteExtraData:{'_token': '$token'},
  322. }).on("filebatchselected", function(event, files) {
  323. c_num = $('.$id').length;
  324. up_num = files.length
  325. if(max < Number(c_num) + Number(up_num)){
  326. layer.alert('最多只能上传' + max + '张图片')
  327. }else {
  328. $(this).fileinput("upload");
  329. }
  330. })
  331. .on("fileuploaded", function(event, data,extra,d) {
  332. if(data.response.code && data.response.code == 200)
  333. {
  334. $("img[src='"+ data.reader.result +"']").after("<input class='$id' type=\"hidden\" name=\"{$name}[]\" value=\" "+data.response.fileurl + " \" />")
  335. }
  336. })
  337. .on("filesuccessremove",function(event,id) {
  338. url = $('#'+ id + ' input').val();
  339. $.post("/admin/Base/AttachmentInfo/remove", { file: url, _token: "$token" } );
  340. })
  341. .on("filedeleted",function(event,key,xhr,data) {
  342. url = xhr.responseJSON.data
  343. $("input[value='"+ url +"']").remove()
  344. });
  345. </script>
  346. EOF;
  347. return $html;
  348. }
  349. }