Resource.php 4.6 KB

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