ImageController.class.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * 播播资源
  4. *
  5. * ==========================================================================
  6. * @link https://www.ttbobo.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.ttbobo.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Seller\Controller;
  15. class ImageController extends CommonController{
  16. //删除图片和缩略图
  17. public function del_image($dir,$image,$type){
  18. $image_dir=ROOT_PATH.'Uploads/image/'.$dir.'/';
  19. $thumb_dir=ROOT_PATH.'Uploads/image/cache/'.$dir.'/';
  20. list($base_name, $ext) = explode(".", $image);
  21. $image = explode("/", $base_name);
  22. $image_name=end($image);
  23. if($type=='product'){
  24. $del_image=array();
  25. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  26. $del_image[]=$thumb_dir.$image_name.'-50x50.'.$ext;
  27. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;
  28. $del_image[]=$thumb_dir.$image_name.'-255x255.'.$ext;
  29. }elseif($type=='gallery'){
  30. $del_image=array();
  31. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  32. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;
  33. $del_image[]=$thumb_dir.$image_name.'-127x127.'.$ext;
  34. }elseif($type=='blog'){
  35. $del_image=array();
  36. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  37. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;
  38. $del_image[]=$thumb_dir.$image_name.'-280x140.'.$ext;
  39. }elseif($type=='blog_gallery'){
  40. $del_image=array();
  41. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  42. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;
  43. }
  44. if(!empty($del_image)){
  45. foreach ($del_image as $k => $v) {
  46. if(is_file($v)){
  47. @unlink($v);
  48. }
  49. }
  50. }
  51. }
  52. /**
  53. * 二进制上传数据
  54. */
  55. public function upload_binaryFile()
  56. {
  57. $data = file_get_contents("php://input");
  58. $get_data = I('get.');
  59. $dir = I('get.dir','goods');
  60. $type = I('get.type');
  61. $name = I('get.name');
  62. $image_dir = ROOT_PATH.'Uploads/image/'.$dir;
  63. $image_dir .= '/'.date('Y-m-d').'/';
  64. $file_path = C('SITE_URL').'/Uploads/image/'.$dir.'/'.date('Y-m-d').'/';
  65. RecursiveMkdir($image_dir);
  66. $file_name = md5($name.time()).'.png';
  67. switch($type)
  68. {
  69. case 'image/jpeg':
  70. $file_name = md5($name.time()).'.jpg';
  71. break;
  72. case 'image/png':
  73. $file_name = md5($name.time()).'.png';
  74. break;
  75. }
  76. $thumb_arr = explode('.',$file_name);
  77. $thumb_image_name = $thumb_arr[0].'_thumb.'.$thumb_arr[1];
  78. file_put_contents($image_dir.$file_name, $data);
  79. $image = new \Think\Image();
  80. $image->open($image_dir.$file_name);
  81. //按照原图的比例生成一个最大为400*400的缩略图并保存为thumb.jpg, 实际会按比例自动缩放
  82. $image->thumb(400, 400)->save($image_dir.$thumb_image_name);
  83. ////{"filePath":"\/Uploads\/image\/goods\/2017-07-05\/","fileName":"7e414de26624c0a5ac7cd5b9bd5edfe3.jpg"}
  84. $result = array('filePath' =>$file_path ,'fileName' => $file_name);
  85. echo json_encode($result);
  86. die();
  87. }
  88. /**
  89. *删除 旧的原图和缩略图,修改的情况下使用
  90. *
  91. */
  92. public function del_old_image(){
  93. $old_gallery_image=I('post.old_gallery_image');
  94. $old_product_image=I('post.old_product_image');
  95. if(!empty($old_gallery_image)){
  96. $old_image=I('post.old_gallery_image');
  97. $thumb_dir=ROOT_PATH.'Uploads/image/cache/gallery/';
  98. $image_dir=ROOT_PATH.'Uploads/image/gallery/';
  99. }elseif(!empty($old_product_image)){
  100. $old_image=I('post.old_product_image');
  101. $thumb_dir=ROOT_PATH.'Uploads/image/cache/product/';
  102. $image_dir=ROOT_PATH.'Uploads/image/product/';
  103. }
  104. if(!empty($old_image)){
  105. list($base_name, $ext) = explode(".", $old_image);
  106. $image = explode("/", $base_name);
  107. $image_name=end($image);
  108. $del_image=array();
  109. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  110. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;//100x100
  111. foreach ($del_image as $k => $v) {
  112. if(is_file($v)){
  113. @unlink($v);
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. *上传图片
  120. */
  121. public function upload_image(){
  122. $dir=I('get.dir');
  123. $org_dir = $dir;
  124. $dir .= '/'.date('Y-m-d');
  125. $this->del_old_image();
  126. $upload = new \Think\Upload();// 实例化上传类
  127. $image_dir=ROOT_PATH.'Uploads/image/'.$dir;
  128. RecursiveMkdir($image_dir);
  129. //1048576 1M
  130. $upload->autoSub = false;
  131. $upload->maxSize = 0;// 设置附件上传大小
  132. $upload->exts = array('jpg', 'gif', 'png', 'jpeg','mp4');// 设置附件上传类型
  133. $upload->rootPath = $image_dir.'/';
  134. $info = $upload->upload();
  135. if(!$info) {
  136. $data['code'] = 1;
  137. $this->ajaxReturn($data);
  138. }else if($org_dir == 'video')
  139. {
  140. $filename=$dir.'/'.$info['file']['savepath'].$info['file']['savename'];
  141. $data['image_thumb'] = '/Common/image/video.jpg';
  142. $data['image'] = $filename;
  143. $data['code'] = 0;
  144. $this->ajaxReturn($data);
  145. }
  146. else{// 上传成功
  147. $filename=$dir.'/'.$info['file']['savepath'].$info['file']['savename'];
  148. $data['image_thumb'] = resize($filename, 100, 100);
  149. $data['image'] = $filename;
  150. $data['code'] = 0;
  151. $this->ajaxReturn($data);
  152. }
  153. }
  154. //用于ckeditor图片上传
  155. function ckupload(){
  156. $upload = new \Think\Upload();// 实例化上传类
  157. $upload->maxSize = 3145728 ;// 设置附件上传大小
  158. $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  159. $upload->rootPath = ROOT_PATH.'Uploads/image/goods_description/';
  160. RecursiveMkdir($upload->rootPath);
  161. $info = $upload->upload();
  162. if(!$info) {
  163. // 上传错误提示错误信息
  164. echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$_GET['CKEditorFuncNum'].", '/', '上传失败," . $upload->getError() . "!');</script>";
  165. }else{// 上传成功
  166. $n=$_GET['CKEditorFuncNum'];
  167. $savepath=C('SITE_URL').'/Uploads/image/goods_description/'. $info['upload']['savepath'].$info['upload']['savename'];
  168. //下面的输出,会自动的将上传成功的文件路径,返回给编辑器。
  169. echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$n.",'$savepath','');</script>";
  170. }
  171. }
  172. }