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 =&gt; 'GIF',
+    //  2 =&gt; 'JPG',
+    //  3 =&gt; 'PNG',
+    //  4 =&gt; 'SWF',
+    //  5 =&gt; 'PSD',
+    //  6 =&gt; 'BMP',
+    //  7 =&gt; 'TIFF(intel byte order)',
+    //  8 =&gt; 'TIFF(motorola byte order)',
+    //  9 =&gt; 'JPC',
+    //  10 =&gt; 'JP2',
+    //  11 =&gt; 'JPX',
+    //  12 =&gt; 'JB2',
+    //  13 =&gt; 'SWC',
+    //  14 =&gt; 'IFF',
+    //  15 =&gt; 'WBMP',
+    //  16 =&gt; '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