PHP生成邀請海報

/**
 * @param $imageDefault
 * @param $textDefault
 * @param $background
 * @param string $filename
 * @param array $config
 */
function getbgqrcode($user_id,$wx_headimg,$nickname,$filename){
    if(empty($filename))
        header("content-type: image/png");

    $imageDefault = array(
        'left'=>260,
        'top'=>580,
        'right'=>0,
        'bottom'=>0,
        'width'=>245,
        'height'=>245,
        'opacity'=>100
    );
    $textDefault = array(
        'text'=>htmlspecialchars_decode($nickname,ENT_QUOTES),
        'fontSize'=>16,       //字號
        'fontColor'=>'0,0,0', //字體顏色
        'angle'=>0,
        'fontPath'=> SOURCE_PATH . '/Public/You/font/msyh.ttc',
    );
    $background = SOURCE_PATH . '/Public/You/img/yaoqing.jpg';//海報最底層得背景
    //生成二維碼//
    $value = C('YOU_OFFICIAL_WEBSITE').'You/Public/register.html?invite_code='.($user_id+C('INVITE_CODE_BASE_NUM'));
    $errorCorrectionLevel = 'H';//容錯級別
    $matrixPointSize = 10;//生成圖片大小
    ob_clean();
    vendor('Qrcode.phpqrcode');
    $QR = \QRcode::returnImage($value, $errorCorrectionLevel, $matrixPointSize, 2);
    if(0 != $QR['errNo']){
        exit();
    } else {
        $QR = $QR['result'];
    }
    $logo = VENDOR_PATH .'Qrcode/ewm_tx.png';//準備好的logo圖片
    if ($logo !== FALSE) {
        $logo = imagecreatefromstring(file_get_contents($logo));
        $QR_width = imagesx($QR);//二維碼圖片寬度
        $QR_height = imagesy($QR);//二維碼圖片高度
        $logo_width = imagesx($logo);//logo圖片寬度
        $logo_height = imagesy($logo);//logo圖片高度
        $logo_qr_width = $QR_width / 5;
        $scale = $logo_width/$logo_qr_width;
        $logo_qr_height = $logo_height/$scale;
        $from_width = ($QR_width - $logo_qr_width) / 2;
        //從新組合圖片並調整大小
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
    }

    //背景方法
    $backgroundInfo = getimagesize($background);
    $ext = image_type_to_extension($backgroundInfo[2], false);
    $backgroundFun = 'imagecreatefrom'.$ext;
    $background = $backgroundFun($background);
    $backgroundWidth = imagesx($background);  //背景寬度
    $backgroundHeight = imagesy($background);  //背景高度
    $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
    $color = imagecolorallocate($imageRes, 0, 0, 0);
    imagefill($imageRes, 0, 0, $color);
    imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));

    //創建畫板 ,縮放圖片至指定尺寸
    $canvas = imagecreatetruecolor($imageDefault['width'], $imageDefault['height']);
    imagefill($canvas, 0, 0, $color);
    //關鍵函數,參數(目標資源,源,目標資源的開始座標x,y, 源資源的開始座標x,y,目標資源的寬高w,h,源資源的寬高w,h)
    imagecopyresampled($canvas, $QR, 0, 0, 0, 0, $imageDefault['width'], $imageDefault['height'],$QR_width,$QR_height);
    $imageDefault['left'] = $imageDefault['left']<0?$backgroundWidth- abs($imageDefault['left']) - $imageDefault['width']:$imageDefault['left'];
    $imageDefault['top'] = $imageDefault['top']<0?$backgroundHeight- abs($imageDefault['top']) - $imageDefault['height']:$imageDefault['top'];
    //放置圖像
    imagecopymerge($imageRes,$canvas, $imageDefault['left'],$imageDefault['top'],$imageDefault['right'],$imageDefault['bottom'],$imageDefault['width'],$imageDefault['height'],$imageDefault['opacity']);//左,上,右,下,寬度,高度,透明度

    if(!empty($wx_headimg) && file_exists(SOURCE_PATH.$wx_headimg)){
        //處理頭像
        $headimg = imagecreatefromstring(file_get_contents(SOURCE_PATH.$wx_headimg));
        $width = imagesx($headimg);
        $height = imagesy($headimg);
        $canvas = imagecreatetruecolor($width,$height);
        imagefill($canvas, 0, 0, $color);
        imagecopyresampled($canvas, $headimg, 0, 0, 0, 0, $width, $height,$width,$height);
        imagecopymerge($imageRes,$canvas, 550,890,0,0,$width,$height,100);//左,上,右,下,寬度,高度,透明度

        //處理暱稱
        list($R,$G,$B) = explode(',', $textDefault['fontColor']);
        $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
        if(mb_strlen($textDefault['text']) > 10){
            $tmp_text = mb_substr($textDefault['text'], 0, 10, 'utf-8').'...';
        } else {
            $tmp_text = $textDefault['text'];
        }
        $box = imagettfbbox($textDefault['fontSize'], 0, $textDefault['fontPath'], $tmp_text);
        $text_height = abs($box[1] - $box[7]);
        $text_width = abs($box[0] - $box[2]);
        imagettftext($imageRes,$textDefault['fontSize'],$textDefault['angle'],550+($width/2)-($text_width/2),$height+890+$text_height+5,$fontColor,$textDefault['fontPath'],$tmp_text);
    }

    //生成圖片
    if(!empty($filename)){
        $res = imagejpeg ($imageRes,$filename,90);
        //保存到本地
        imagedestroy($imageRes);
    }else{
        imagejpeg ($imageRes);
        //在瀏覽器上顯示
        imagedestroy($imageRes);
    }
}
相關文章
相關標籤/搜索