ImgController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\admin\controller;
  3. use cmf\controller\AdminBaseController;
  4. use think\Db;
  5. class ImgController {
  6. // public function initialize(){
  7. // parent::initialize();
  8. // $username = $_SESSION['think']['name'];
  9. // $userid = $_SESSION['think']['ADMIN_ID'];
  10. // //判断是否是编辑管理员
  11. // $iseditor = 0;
  12. // $iseditor = Db::name('role_user')->where('role_id',3)->where('user_id',$userid)->count();
  13. // $this->iseditor = $iseditor;
  14. // $this->uname = $username;
  15. // $this->assign('iseditor',$iseditor);
  16. // }
  17. /**
  18. * desription 压缩图片
  19. * @param sting $imgsrc 图片路径
  20. * @param string $imgdst 压缩后保存路径
  21. */
  22. public function compressedImage($imgsrc, $imgdst) {
  23. list($width, $height, $type) = getimagesize($imgsrc); //list可以将返回的数组值赋值给变量
  24. $new_width = $width;//压缩后的图片宽
  25. $new_height = $height;//压缩后的图片高
  26. // echo $new_width;echo "<br/>";echo $new_height;echo "<br/>";
  27. // if($width >= 600){
  28. // $per = 600 / $width;//计算比例
  29. // $new_width = $width * $per;
  30. // $new_height = $height * $per;
  31. // }
  32. // echo $new_width;echo "<br/>";echo $new_height;die();
  33. switch ($type) {
  34. case 1:
  35. $giftype = check_gifcartoon($imgsrc);
  36. if ($giftype) {
  37. header('Content-Type:image/gif');
  38. $image_wp = imagecreatetruecolor($new_width, $new_height);
  39. $image = imagecreatefromgif($imgsrc);
  40. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  41. //90代表的是质量、压缩图片容量大小
  42. imagejpeg($image_wp, $imgdst, 90);
  43. imagedestroy($image_wp);
  44. imagedestroy($image);
  45. }
  46. break;
  47. case 2:
  48. // header('Content-Type:image/png');echo 'fdsfsfs111';die();
  49. $image_wp = imagecreatetruecolor($new_width, $new_height);
  50. $image = imagecreatefromjpeg($imgsrc);
  51. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  52. //90代表的是质量、压缩图片容量大小
  53. imagejpeg($image_wp, $imgdst, 90);
  54. imagedestroy($image_wp);
  55. imagedestroy($image);
  56. break;
  57. case 3:
  58. // header('Content-Type:image/png');
  59. $image_wp = imagecreatetruecolor($new_width, $new_height);
  60. $image = imagecreatefrompng($imgsrc);
  61. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  62. //90代表的是质量、压缩图片容量大小
  63. imagejpeg($image_wp, $imgdst, 50);
  64. imagedestroy($image_wp);
  65. imagedestroy($image);
  66. break;
  67. }
  68. }
  69. /**
  70. * 缩略图
  71. * @param $file
  72. * @param int $type
  73. * @return $this
  74. */
  75. public static function imageThumb($file, $type=1, $params=[]){
  76. $siteInfo = cmf_get_site_info();
  77. $thumbWidth = isset($siteInfo['thumb_width'])? intval($siteInfo['thumb_width']) : 0;
  78. $thumbHeight = isset($siteInfo['thumb_height'])? intval($siteInfo['thumb_height']) : 0;
  79. $width = isset($params['width'])? $params['width'] : 0;
  80. $height = isset($params['height'])? $params['height'] : 0;
  81. $thumbWidth = $thumbWidth>600? $thumbWidth : 600;
  82. $thumbHeight = $thumbHeight>600? $thumbHeight : 600;
  83. $thumbWidth = $width? $width : $thumbWidth;
  84. $thumbHeight = $height? $height : $thumbHeight;
  85. // 要加水印的图片
  86. $realFilename = 'upload/' . $file;
  87. if (!file_exists($realFilename)) {
  88. return false;
  89. }
  90. $image = \think\Image::open($realFilename);
  91. $filepath = dirname($realFilename);
  92. $paths = explode('_',basename($realFilename));
  93. $filename = end($paths);
  94. $thumbfile = $filepath . '/thumb_' . $thumbWidth . '_' . $thumbHeight . '_' . $type . '_' . $filename;
  95. $image->thumb($thumbWidth, $thumbHeight, $type)->save($thumbfile);
  96. return preg_replace("/^upload\//", '', $thumbfile);
  97. }
  98. //压缩图片2
  99. public static function imageThumb2($imgsrc, $imgdst, $type=3){
  100. list($width, $height, $type) = getimagesize($imgsrc);
  101. $new_width = $width;//压缩后的图片宽
  102. $new_height = $height;//压缩后的图片高
  103. // if($width >= 600){
  104. // $per = 600 / $width;//计算比例
  105. // $new_width = $width * $per;
  106. // $new_height = $height * $per;
  107. // }
  108. $image = \think\Image::open($imgsrc);
  109. // $filepath = dirname($imgsrc); //不需要获取根目录
  110. //$paths = explode('_',basename($realFilename));
  111. //$filename = end($paths);
  112. //
  113. $image->thumb($new_width, $new_height, $type)->save($imgdst);
  114. return preg_replace("/^upload\//", '', $imgdst);
  115. }
  116. }
  117. ?>