依賴庫文件 phpqrcode.php
(下載地址://www.jb51.net/codes/189897.html ;或者在官網下載:http://phpqrcode.sourceforge.net )
本地演示文件代碼下載(裝環境即可)
百度網盤鏈接:https://pan.baidu.com/s/1L4dExJIJhbjrNa-hBrrdzg 密碼:dmvs
代碼邏輯:
1.生成一張url相關的 二維碼 QR
2.把log圖片跟QR合並成一個帶logo的二維碼 last
3.把帶logo的的二維碼跟 活動圖片合成為一張圖 保存到本地或直接輸出圖片
<?php
/**
* 生成管理員分銷二維碼
* ============================================================================
* ============================================================================
* $Author: 戈丫汝 QQ:534208139
* 2018-11-9 17:20:08Z
*/
require_once("./phpqrcode/phpqrcode.php");
//參數 活動模板圖片,二維碼url,模板內二維碼的位置
$mes = getActivityImg('./images/123.png','http://shop.izhiwo.com',258,273);
print_r($mes);die;
function getActivityImg($template,$url,$x,$y)
{
//二維碼中間添加logo
$logo = './images/logo.png';
$QR = "base.png";
$last = "last.png";
$errorCorrectionLevel = 'Q'; //防錯等級
$matrixPointSize = 3; //二維碼大小
//生成二維碼
//參數內容:二維碼儲存內容,生成存儲,防錯等級,二維碼大小,白邊大小
QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);
//合並logo跟二維碼
$QR = imagecreatefromstring(file_get_contents($QR));
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR); $logo_width = imagesx($logo);
$logo_height = imagesy($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);
imagepng($QR,$last); // 生成帶log的二維碼圖片 存儲到last
//合成帶logo的二維碼圖片跟 模板圖片
$path_1 = $template; //背景圖
$path_2 = $last; //帶logo的二維碼圖
$dst = imagecreatefromstring(file_get_contents($path_1));
$src = imagecreatefromstring(file_get_contents($path_2));
list($src_w, $src_h) = getimagesize($path_2);
imagecopymerge($dst, $src, $x, $y, 0, 0, $src_w, $src_h, 100);
list($dst_w, $dst_h, $dst_type) = getimagesize($path_1);
//第一種:圖片輸出到頁面
/*switch ($dst_type) {
case 1://GIF
header('Content-Type: image/gif');
imagegif($dst);
break;
case 2://JPG
header('Content-Type: image/jpeg');
imagejpeg($dst);
break;
case 3://PNG
header('Content-Type: image/png');
imagepng($dst);
break;
default:
break;
}
imagedestroy($dst);
imagedestroy($src);*/
//第二種:輸出到本地文件夾,返回生成圖片的路徑
$fileName= md5(rand(0,999));
$EchoPath='./images/'.$fileName.'.png';
imagepng($dst,$EchoPath);
imagedestroy($dst);
return $EchoPath;
}
時間:2019-03-31 新增代碼 (需求:生成后的圖片加文字)
以下代碼放到輸出圖片代碼前即可,其他不變,全部代碼文字位置二維碼位置都沒有調試,需要的可以根據自己需求調試. END~~~
//打上文字 $font = 'simsun.ttc'; //字體 $black = imagecolorallocate($dst, 0x00, 0x00, 0x00); //字體顏色 imagefttext($dst, 13, 0, 20, 20, $black, $font, " 快樂編程快樂編程快樂\n編程快樂編程快樂編程快樂編程快樂編程\n編程快樂編程快樂編程快樂編."); //輸出圖片 list($dst_w, $dst_h, $dst_type) = getimagesize($path_1);
