From 541bbb38aa9a7a7fbfaa83a8e1936cb8074785a8 Mon Sep 17 00:00:00 2001 From: heshupeng <hsp@bronet.cn> Date: Thu, 3 Dec 2020 09:39:40 +0800 Subject: [PATCH] 高效判断远程图片是否存在 --- application/api/common.php | 28 ++++++++++++++++++++++++++++ application/api/controller/User.php | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/application/api/common.php b/application/api/common.php index 268d4d1..6aef27c 100644 --- a/application/api/common.php +++ b/application/api/common.php @@ -44,4 +44,32 @@ function createRoundImg($imgpath) { header("content-type:image/png"); imagepng($image,$imgpath); imagedestroy($image); +} + +/** + * 高效判断远程图片是否存在 + */ +function remoteImageExist($url) +{ + // $exif_imagetype = [ + // 1 => 'GIF', + // 2 => 'JPG', + // 3 => 'PNG', + // 4 => 'SWF', + // 5 => 'PSD', + // 6 => 'BMP', + // 7 => 'TIFF(intel byte order)', + // 8 => 'TIFF(motorola byte order)', + // 9 => 'JPC', + // 10 => 'JP2', + // 11 => 'JPX', + // 12 => 'JB2', + // 13 => 'SWC', + // 14 => 'IFF', + // 15 => 'WBMP', + // 16 => 'XBM' + // ]; + $isExsit = @exif_imagetype($url); + if ($isExsit === false) return false; + return in_array($isExsit, [1, 2, 3]) ? true : false; } \ No newline at end of file diff --git a/application/api/controller/User.php b/application/api/controller/User.php index b48c648..6535442 100644 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -681,7 +681,7 @@ class User extends Api { $user = \app\api\model\User::get($this->auth->id); empty($user['avatar']) && $this->error('请先上传头像'); - + !remoteImageExist($user['avatar']) && $this->error('头像失效,请更新头像'); // 本地路径 $dir = 'uploads/user'; if (!file_exists($dir)){ -- libgit2 0.24.0