| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\exception\UploadException;
- use app\common\library\Upload;
- use app\common\model\Attachment;
- use think\Exception;
- class Resource extends Api
- {
- protected $noNeedLogin = ["*"];
- private function compressedImage($imgsrc, $imgdst)
- {
- if (filesize($imgsrc) < 150000) {
- return;
- }
- list($width, $height, $type) = getimagesize($imgsrc);
- $new_width = $width > 800 ? 800 : $width;//图片宽度的限制
- $new_height = $height > 800 ? ceil($height * 800 / $width) : $height;//自适应匹配图片高度
- switch ($type) {
- case 1:
- $giftype = $this->check_gifcartoon($imgsrc);
- if ($giftype) {
- header('Content-Type:image/gif');
- $image_wp = imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefromgif($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- //90代表的是质量、压缩图片容量大小
- imagejpeg($image_wp, $imgdst, 90);
- imagedestroy($image_wp);
- imagedestroy($image);
- }
- break;
- case 2:
- header('Content-Type:image/jpeg');
- $image_wp = imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefromjpeg($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- //90代表的是质量、压缩图片容量大小
- imagejpeg($image_wp, $imgdst, 90);
- imagedestroy($image_wp);
- imagedestroy($image);
- break;
- case 3:
- header('Content-Type:image/png');
- $image_wp = imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefrompng($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- //90代表的是质量、压缩图片容量大小
- imagejpeg($image_wp, $imgdst, 90);
- imagedestroy($image_wp);
- imagedestroy($image);
- break;
- }
- }
- private function check_gifcartoon($image_file)
- {
- $fp = fopen($image_file, 'rb');
- $image_head = fread($fp, 1024);
- fclose($fp);
- return preg_match("/" . chr(0x21) . chr(0xff) . chr(0x0b) . 'NETSCAPE2.0' . "/", $image_head) ? false : true;
- }
- public function upload()
- {
- //默认普通上传文件
- $file = $this->request->file('file');
- try {
- $upload = new Upload($file);
- $attachment = $upload->upload();
- } catch (UploadException $e) {
- $this->error($e->getMessage());
- }
- $image_url = join("/", explode("\\", dirname(realpath(APP_PATH)))) . "/public" . $attachment->url;
- //$this->compressedImage($image_url, $image_url);
- $this->success([
- 'url' => $attachment->url,
- ]);
- }
- public function batch_compress($start_index = 0, $end_index = 10, $filenames = [])
- {
- $res = [
- "dir" => [],
- "images" => [],
- "errors" => [],
- ];
- $base = join("/", explode("\\", dirname(realpath(APP_PATH)))) . "/public/uploads";
- $handler = opendir($base);
- $files = $filenames;
- // while (($filename = readdir($handler)) !== false) {
- // if (strpos($filename, ".") === false) {
- // $files[] = $filename;
- // }
- // }
- // closedir($handler);
- $res["dir"] = $files;
- $images = [];
- for ($i = $start_index; $i <= $end_index; $i++) {
- $file_name = isset($files[$i]) ? $files[$i] : null;
- if (null === $file_name)
- continue;
- $handler = opendir($base . "/" . $file_name);
- while (($filename = readdir($handler)) !== false) {
- if (strlen($filename) > 3) {
- $images[] = $base . "/" . $file_name . "/" . $filename;
- }
- }
- closedir($handler);
- }
- $res["images"] = $images;
- foreach ($images as $item) {
- try {
- $this->compressedImage($item, $item);
- } catch (Exception $e) {
- $res["errors"][] = "$item -> {$e->getMessage()}";
- }
- }
- $this->success($res);
- }
- }
|