ImageController.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Admin\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. */
  56. public function del_old_image(){
  57. $old_gallery_image=I('post.old_gallery_image');
  58. $old_product_image=I('post.old_product_image');
  59. if(!empty($old_gallery_image)){
  60. $old_image=I('post.old_gallery_image');
  61. $thumb_dir=ROOT_PATH.'Uploads/image/cache/gallery/';
  62. $image_dir=ROOT_PATH.'Uploads/image/gallery/';
  63. }elseif(!empty($old_product_image)){
  64. $old_image=I('post.old_product_image');
  65. $thumb_dir=ROOT_PATH.'Uploads/image/cache/product/';
  66. $image_dir=ROOT_PATH.'Uploads/image/product/';
  67. }
  68. if(!empty($old_image)){
  69. list($base_name, $ext) = explode(".", $old_image);
  70. $image = explode("/", $base_name);
  71. $image_name=end($image);
  72. $del_image=array();
  73. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  74. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;//100x100
  75. foreach ($del_image as $k => $v) {
  76. if(is_file($v)){
  77. @unlink($v);
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. *上传图片
  84. */
  85. public function upload_image(){
  86. $dir=I('get.dir');
  87. $dir .= '/'.date('Y-m-d');
  88. $this->del_old_image();
  89. $upload = new \Think\Upload();// 实例化上传类
  90. $image_dir=ROOT_PATH.'Uploads/image/'.$dir;
  91. RecursiveMkdir($image_dir);
  92. $upload->autoSub = false;
  93. $upload->maxSize = 3145728 ;// 设置附件上传大小
  94. $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  95. $upload->rootPath = $image_dir.'/';
  96. $info = $upload->upload();
  97. if(!$info) {
  98. $data['result'] = false;
  99. $this->ajaxReturn($data);
  100. }else{// 上传成功
  101. $filename=$dir.'/'.$info['file']['savepath'].$info['file']['savename'];
  102. $data['image_thumb'] = resize($filename, 100, 100);
  103. $data['image'] = $filename;
  104. $this->ajaxReturn($data);
  105. }
  106. }
  107. //用于ckeditor图片上传
  108. function ckupload(){
  109. $upload = new \Think\Upload();// 实例化上传类
  110. $upload->maxSize = 3145728 ;// 设置附件上传大小
  111. $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  112. $upload->rootPath = ROOT_PATH.'Uploads/image/goods_description/';
  113. RecursiveMkdir($upload->rootPath);
  114. $info = $upload->upload();
  115. if(!$info) {
  116. // 上传错误提示错误信息
  117. echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$_GET['CKEditorFuncNum'].", '/', '上传失败," . $upload->getError() . "!');</script>";
  118. }else{// 上传成功
  119. $n=$_GET['CKEditorFuncNum'];
  120. $savepath=C('SITE_URL').'/Uploads/image/goods_description/'. $info['upload']['savepath'].$info['upload']['savename'];
  121. //下面的输出,会自动的将上传成功的文件路径,返回给编辑器。
  122. echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$n.",'$savepath','');</script>";
  123. }
  124. }
  125. }