dyjh 6 years ago
parent
commit
eddfe7b22d
2 changed files with 54 additions and 2 deletions
  1. 52 1
      app/Helper/functions.inc.php
  2. 2 1
      app/Http/Controllers/Api/V1/AlbumController.php

+ 52 - 1
app/Helper/functions.inc.php

xqd xqd
@@ -32,12 +32,15 @@ if (! function_exists('getUrlImage')) {
     /**
      * Get the image in the url.
      *
-     * @param  string  $img
+     * @param  string  $url
      * @param  string  $filename
      * @return string|boolean
      */
     function getUrlImage($url, $filename)
     {
+        if (file_exists($filename)) {
+            unlink($filename);
+        }
         if (empty($url) || empty($filename)) {
             return false;
         }
@@ -53,6 +56,54 @@ if (! function_exists('getUrlImage')) {
     }
 }
 
+
+
+
+if (! function_exists('imageToCircle')) {
+    /**
+     * Change the image to the circle.
+     *
+     * @param  string  $url
+     * @return string
+     */
+    function imageToCircle($url)
+    {
+        $ext = pathinfo($url);
+        $src_img = null;
+        switch ($ext['extension']) {
+            case 'jpg':
+                $src_img = imagecreatefromjpeg($url);
+                break;
+            case 'png':
+                $src_img = imagecreatefrompng($url);
+                break;
+        }
+        $wh = getimagesize($url);
+        $w = $wh[0];
+        $h = $wh[1];
+        $w = min($w, $h);
+        $h = $w;
+        $img = imagecreatetruecolor($w, $h);
+        //这一句一定要有
+        imagesavealpha($img, true);
+        //拾取一个完全透明的颜色,最后一个参数127为全透明
+        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
+        imagefill($img, 0, 0, $bg);
+        $r = $w / 2; //圆半径
+        $y_x = $r; //圆心X坐标
+        $y_y = $r; //圆心Y坐标
+        for ($x = 0; $x < $w; $x++) {
+            for ($y = 0; $y < $h; $y++) {
+                $rgbColor = imagecolorat($src_img, $x, $y);
+                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
+                    imagesetpixel($img, $x, $y, $rgbColor);
+                }
+            }
+        }
+        return $img;
+    }
+}
+
 if( ! function_exists('array_to_sort')) {
     /**
      * 对二维数组排序

+ 2 - 1
app/Http/Controllers/Api/V1/AlbumController.php

xqd
@@ -239,7 +239,8 @@ class AlbumController extends Controller
             if (!file_exists(public_path() . '/base/poster/avatar/' . $datas['store_id'])) {
                 mkdir(public_path() . '/base/poster/avatar/' . $datas['store_id'], 0755, true);
             }
-            getUrlImage($userAuth->avatar, public_path() . '/base/poster/avatar/' . $datas['store_id'] . "/$userAuth->id.jpg");
+            $res_image = getUrlImage($userAuth->avatar, public_path() . '/base/poster/avatar/' . $datas['store_id'] . "/$userAuth->id.jpg");
+            imageToCircle($res_image);
             $token = $userAuth->createToken($userAuth->id . '-' . $userAuth->openid)->accessToken;
             $conf = AlbumManufacturerModel::where('store_id',$datas['store_id'])->first();
             $getPhone = $conf->getPhone;