天天看點

微信小程式碼生成與更換LOGO

在我們進行微信小程式開發時,

有時需要生成微信小程式碼進行分享;

但是這個微信小程式碼中的LOGO并不是使用者頭像;

這時就需要我們使用 

GD

 庫對二維碼進行處理;

将使用者頭像裁剪成圓形,

然後合并到生成的二維碼上。

例子:

<?php
/**
 * @Author: [FENG] <[email protected]>
 * @Date:   2019-06-28T17:40:14+08:00
 * @Last Modified by:   [FENG] <[email protected]>
 * @Last Modified time: 2019-07-01T21:58:29+08:00
 */

$wx_appid = 'wx...........';
$wx_secret = 'wx...........';
$path = $_GET['path']; // 傳遞的 路徑

$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $wx_appid . '&secret=' . $wx_secret;
$data = httpRequest($url, 'GET');
if (!$data)
    die('access_token擷取失敗');

$access_token = json_decode($data,TRUE)['access_token']; // 小程式端擷取的 access_token
$url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token; // 請求位址,擷取小程式碼
$data = array(
    'path'          => $path, // 掃碼進入的小程式頁面路徑
    'width'         => 430, // 二維碼的寬度,機關 px。
    'auto_color'    => false, // 自動配置線條顔色
    'is_hyaline'    => true // 是否需要透明底色
);

$qrcode = httpRequest($url,"POST",json_encode($data)); // 擷取到二進制小程式碼,可直接寫入檔案生成圖檔
$avatarUrl = $card['avatar']; // 使用者頭像位址
$avatarUrl = 'https://fengkui.net/images/home/logo.png'; // 頭像位址
// $qrcode = file_get_contents('https://fengkui.net/images/home/qrcode.jpg'); // 二維碼位址 位址
$date_avatar = file_get_contents($avatarUrl);
$logo = yuan_img($date_avatar); // 頭像裁剪成圓形
$sharePic = qrcode_with_logo($qrcode, $logo); // 頭像與二維碼合并
file_put_contents('./qrcode.png',$sharePic); // 生成檔案

/**
 * [yuan_img 剪切圖檔為圓形]
 * @param  [type] $picture [圖檔資料流 比如file_get_contents(imageurl)傳回的資料]
 * @return [type]          [圖檔資料流]
 */
function yuan_img($picture)
{
    $src_img = imagecreatefromstring($picture);
    $w = imagesx($src_img);
    $h = imagesy($src_img);
    $w = min($w, $h);
    $h = $w;
    $img = imagecreatetruecolor($w, $h);
    imagealphablending($img, false); // 設定圖像的混色模式
    imagesavealpha($img, true); // 這一句一定要有(設定标記以在儲存 PNG 圖像時儲存完整的 alpha 通道資訊)
    $bg = imagecolorallocatealpha($img, 255, 255, 255, 127); // 拾取一個完全透明的顔色,最後一個參數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);
            }
        }
    }
    /**
     * 如果想要直接輸出圖檔,應該先設header。header("Content-Type: image/png; charset=utf-8");
     * 并且去掉緩存區函數
     */
    //擷取輸出緩存,否則imagepng會把圖檔輸出到浏覽器
    ob_start();
    imagepng($img);
    imagedestroy($img);
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
}

/**
 * [qrcode_with_logo 在二維碼的中間區域鑲嵌圖檔]
 * @param  [type] $QR   [二維碼資料流。比如file_get_contents(imageurl)傳回的資料,或者微信給傳回的資料]
 * @param  [type] $logo [中間顯示圖檔的資料流。比如file_get_contents(imageurl)傳回的東東]
 * @return [type]       [傳回圖檔資料流]
 */
function qrcode_with_logo($QR, $logo)
{
    $QR = imagecreatefromstring($QR);
    $logo = imagecreatefromstring($logo);
    $QR_width = imagesx($QR); // 二維碼圖檔寬度
    $QR_height = imagesy($QR); // 二維碼圖檔高度
    $logo_width = imagesx($logo); // logo圖檔寬度
    $logo_height = imagesy($logo); // logo圖檔高度
    $logo_qr_width = $QR_width / 2.2; // 組合之後logo的寬度(占二維碼的1/2.2)
    $scale = $logo_width / $logo_qr_width; // logo的寬度縮放比(本身寬度/組合後的寬度)
    $logo_qr_height = $logo_height / $scale; // 組合之後logo的高度
    $from_width = ($QR_width - $logo_qr_width) / 2; // 組合之後logo左上角所在坐标點
    /**
     * 重新組合圖檔并調整大小
     * imagecopyresampled() 将一幅圖像(源圖象)中的一塊正方形區域拷貝到另一個圖像中
     */
    imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
    /**
     * 如果想要直接輸出圖檔,應該先設header。header("Content-Type: image/png; charset=utf-8");
     * 并且去掉緩存區函數
     */
    //擷取輸出緩存,否則imagepng會把圖檔輸出到浏覽器
    ob_start();
    imagepng($QR);
    imagedestroy($QR);
    imagedestroy($logo);
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
}