ImageController.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Home\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. $kufile_path = $dir.'/'.date('Y-m-d').'/';
  66. RecursiveMkdir($image_dir);
  67. $file_name = md5($name.time()).'.png';
  68. switch($type)
  69. {
  70. case 'image/jpeg':
  71. $file_name = md5($name.time()).'.jpg';
  72. break;
  73. case 'image/png':
  74. $file_name = md5($name.time()).'.png';
  75. break;
  76. }
  77. $thumb_arr = explode('.',$file_name);
  78. $thumb_image_name = $thumb_arr[0].'_thumb.'.$thumb_arr[1];
  79. file_put_contents($image_dir.$file_name, $data);
  80. $image = new \Think\Image();
  81. $image->open($image_dir.$file_name);
  82. //按照原图的比例生成一个最大为400*400的缩略图并保存为thumb.jpg, 实际会按比例自动缩放
  83. $image->thumb(400, 400)->save($image_dir.$thumb_image_name);
  84. ////{"filePath":"\/Uploads\/image\/goods\/2017-07-05\/","fileName":"7e414de26624c0a5ac7cd5b9bd5edfe3.jpg"}
  85. $result = array('filePath' =>$file_path ,'kufile_path' => $kufile_path,'fileName' => $file_name);
  86. echo json_encode($result);
  87. die();
  88. }
  89. /**
  90. *删除 旧的原图和缩略图,修改的情况下使用
  91. *
  92. */
  93. public function del_old_image(){
  94. $old_gallery_image=I('post.old_gallery_image');
  95. $old_product_image=I('post.old_product_image');
  96. if(!empty($old_gallery_image)){
  97. $old_image=I('post.old_gallery_image');
  98. $thumb_dir=ROOT_PATH.'Uploads/image/cache/gallery/';
  99. $image_dir=ROOT_PATH.'Uploads/image/gallery/';
  100. }elseif(!empty($old_product_image)){
  101. $old_image=I('post.old_product_image');
  102. $thumb_dir=ROOT_PATH.'Uploads/image/cache/product/';
  103. $image_dir=ROOT_PATH.'Uploads/image/product/';
  104. }
  105. if(!empty($old_image)){
  106. list($base_name, $ext) = explode(".", $old_image);
  107. $image = explode("/", $base_name);
  108. $image_name=end($image);
  109. $del_image=array();
  110. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  111. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;//100x100
  112. foreach ($del_image as $k => $v) {
  113. if(is_file($v)){
  114. @unlink($v);
  115. }
  116. }
  117. }
  118. }
  119. /**
  120. *上传图片
  121. */
  122. public function upload_image(){
  123. $dir=I('get.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. $upload->autoSub = false;
  130. $upload->maxSize = 3145728 ;// 设置附件上传大小
  131. $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  132. $upload->rootPath = $image_dir.'/';
  133. $info = $upload->upload();
  134. if(!$info) {
  135. $data['result'] = false;
  136. $this->ajaxReturn($data);
  137. }else{// 上传成功
  138. $filename=$dir.'/'.$info['file']['savepath'].$info['file']['savename'];
  139. $data['image_thumb'] = resize($filename, 100, 100);
  140. $data['image'] = $filename;
  141. $this->ajaxReturn($data);
  142. }
  143. }
  144. //用于ckeditor图片上传
  145. function ckupload(){
  146. $upload = new \Think\Upload();// 实例化上传类
  147. $upload->maxSize = 3145728 ;// 设置附件上传大小
  148. $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  149. $upload->rootPath = ROOT_PATH.'Uploads/image/goods_description/';
  150. RecursiveMkdir($upload->rootPath);
  151. $info = $upload->upload();
  152. if(!$info) {
  153. // 上传错误提示错误信息
  154. echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$_GET['CKEditorFuncNum'].", '/', '上传失败," . $upload->getError() . "!');</script>";
  155. }else{// 上传成功
  156. $n=$_GET['CKEditorFuncNum'];
  157. $savepath=C('SITE_URL').'/Uploads/image/goods_description/'. $info['upload']['savepath'].$info['upload']['savename'];
  158. //下面的输出,会自动的将上传成功的文件路径,返回给编辑器。
  159. echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$n.",'$savepath','');</script>";
  160. }
  161. }
  162. }