Resource.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\exception\UploadException;
  5. use app\common\library\Upload;
  6. use think\Exception;
  7. class Resource extends Api
  8. {
  9. protected $noNeedLogin = ["*"];
  10. private function compressedImage($imgsrc, $imgdst)
  11. {
  12. if (filesize($imgsrc) < 150000) {
  13. return;
  14. }
  15. list($width, $height, $type) = getimagesize($imgsrc);
  16. $new_width = $width > 800 ? 800 : $width;//图片宽度的限制
  17. $new_height = $height > 800 ? ceil($height * 800 / $width) : $height;//自适应匹配图片高度
  18. switch ($type) {
  19. case 1:
  20. $giftype = $this->check_gifcartoon($imgsrc);
  21. if ($giftype) {
  22. header('Content-Type:image/gif');
  23. $image_wp = imagecreatetruecolor($new_width, $new_height);
  24. $image = imagecreatefromgif($imgsrc);
  25. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  26. //90代表的是质量、压缩图片容量大小
  27. imagejpeg($image_wp, $imgdst, 90);
  28. imagedestroy($image_wp);
  29. imagedestroy($image);
  30. }
  31. break;
  32. case 2:
  33. header('Content-Type:image/jpeg');
  34. $image_wp = imagecreatetruecolor($new_width, $new_height);
  35. $image = imagecreatefromjpeg($imgsrc);
  36. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  37. //90代表的是质量、压缩图片容量大小
  38. imagejpeg($image_wp, $imgdst, 90);
  39. imagedestroy($image_wp);
  40. imagedestroy($image);
  41. break;
  42. case 3:
  43. header('Content-Type:image/png');
  44. $image_wp = imagecreatetruecolor($new_width, $new_height);
  45. $image = imagecreatefrompng($imgsrc);
  46. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  47. //90代表的是质量、压缩图片容量大小
  48. imagejpeg($image_wp, $imgdst, 90);
  49. imagedestroy($image_wp);
  50. imagedestroy($image);
  51. break;
  52. }
  53. }
  54. private function check_gifcartoon($image_file)
  55. {
  56. $fp = fopen($image_file, 'rb');
  57. $image_head = fread($fp, 1024);
  58. fclose($fp);
  59. return preg_match("/" . chr(0x21) . chr(0xff) . chr(0x0b) . 'NETSCAPE2.0' . "/", $image_head) ? false : true;
  60. }
  61. public function upload()
  62. {
  63. //默认普通上传文件
  64. $file = $this->request->file('file');
  65. try {
  66. $upload = new Upload($file);
  67. $attachment = $upload->upload();
  68. } catch (UploadException $e) {
  69. $this->error($e->getMessage());
  70. }
  71. $image_url = join("/", explode("\\", dirname(realpath(APP_PATH)))) . "/public" . $attachment->url;
  72. //$this->compressedImage($image_url, $image_url);
  73. $this->success([
  74. 'url' => $attachment->url,
  75. ]);
  76. }
  77. public function batch_compress($start_index = 0, $end_index = 10, $filenames = [])
  78. {
  79. $res = [
  80. "dir" => [],
  81. "images" => [],
  82. "errors" => [],
  83. ];
  84. $base = join("/", explode("\\", dirname(realpath(APP_PATH)))) . "/public/uploads";
  85. $handler = opendir($base);
  86. $files = $filenames;
  87. // while (($filename = readdir($handler)) !== false) {
  88. // if (strpos($filename, ".") === false) {
  89. // $files[] = $filename;
  90. // }
  91. // }
  92. // closedir($handler);
  93. $res["dir"] = $files;
  94. $images = [];
  95. for ($i = $start_index; $i <= $end_index; $i++) {
  96. $file_name = isset($files[$i]) ? $files[$i] : null;
  97. if (null === $file_name)
  98. continue;
  99. $handler = opendir($base . "/" . $file_name);
  100. while (($filename = readdir($handler)) !== false) {
  101. if (strlen($filename) > 3) {
  102. $images[] = $base . "/" . $file_name . "/" . $filename;
  103. }
  104. }
  105. closedir($handler);
  106. }
  107. $res["images"] = $images;
  108. foreach ($images as $item) {
  109. try {
  110. $this->compressedImage($item, $item);
  111. } catch (Exception $e) {
  112. $res["errors"][] = "$item -> {$e->getMessage()}";
  113. }
  114. }
  115. $this->success($res);
  116. }
  117. }