ImgController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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);
  24. $new_width = $width;//压缩后的图片宽
  25. $new_height = $height;//压缩后的图片高
  26. if($width >= 600){
  27. $per = 600 / $width;//计算比例
  28. $new_width = $width * $per;
  29. $new_height = $height * $per;
  30. }
  31. // echo $type.':'.$width.":".$height;die();
  32. switch ($type) {
  33. case 1:
  34. $giftype = check_gifcartoon($imgsrc);
  35. if ($giftype) {
  36. header('Content-Type:image/gif');
  37. $image_wp = imagecreatetruecolor($new_width, $new_height);
  38. $image = imagecreatefromgif($imgsrc);
  39. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  40. //90代表的是质量、压缩图片容量大小
  41. imagejpeg($image_wp, $imgdst, 100);
  42. imagedestroy($image_wp);
  43. imagedestroy($image);
  44. }
  45. break;
  46. case 2:
  47. header('Content-Type:image/jpeg');
  48. $image_wp = imagecreatetruecolor($new_width, $new_height);
  49. $image = imagecreatefromjpeg($imgsrc);
  50. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  51. //90代表的是质量、压缩图片容量大小
  52. imagejpeg($image_wp, $imgdst, 100);
  53. imagedestroy($image_wp);
  54. imagedestroy($image);
  55. break;
  56. case 3:
  57. header('Content-Type:image/png');
  58. $image_wp = imagecreatetruecolor($new_width, $new_height);
  59. $image = imagecreatefrompng($imgsrc);
  60. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  61. //90代表的是质量、压缩图片容量大小
  62. imagejpeg($image_wp, $imgdst, 100);
  63. imagedestroy($image_wp);
  64. imagedestroy($image);
  65. break;
  66. }
  67. }
  68. }
  69. ?>