大部分東西來自這位大神 https://blog.csdn.net/mangrandi/article/details/80495136
- 框架是用的laravel框架
- 用的easywechat這個包 "overtrue/laravel-wechat": "~5.0",
一、傳入小程序路徑和文件名獲取小程序碼
/**
* 獲取小程序太陽碼
* $path 小程序路徑 pages/index/index
* $filename 圖片保存路徑
* 服務器圖片的地址
*/
public function getQrCode($path, $filename)
{
$response = $this->app->app_code->get($path);
$directory = 'wechatqr/';
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
// 微信小程序碼
$file = $response->saveAs($directory, $filename);
$file_put = $directory . $file;
$wechat_buffer = file_get_contents($file_put);
// 更換logo的圖片
$avatarUrl = 'https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83epny5Vr8fZzX0luLF6AtVUTiafGL013Mq4sojPiauUKw3SAOs4OqPTKicaVKCOWUMBsaoCvocQVh4fAQ/132';
$avatar = file_get_contents($avatarUrl);
// 更換logo的圖片變圓形 返回的是圖片數(shù)據(jù)流
$logo_buffer = $this->yuanImg($avatar);
// 二維碼與頭像結合
$share_pic = $this->qrcodeWithLogo($wechat_buffer, $logo_buffer);
file_put_contents($file_put, $share_pic);
return $file_put;
}
return false;
}
二、將需要需要替換的圖片切為圓形
/**
* 剪切圖片為圓形
* @param $picture 圖片數(shù)據(jù)流 比如file_get_contents(imageurl)返回的東東
* @return 圖片數(shù)據(jù)流
*/
public function yuanImg($picture)
{
$src_img = imagecreatefromstring($picture);
$w = imagesx($src_img);
$h = imagesy($src_img);
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
//這一句一定要有
imagesavealpha($img, true);
//拾取一個完全透明的顏色,最后一個參數(shù)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);
}
}
}
/**
* 如果想要直接輸出圖片,應該先設header。header("Content-Type: image/png; charset=utf-8");
* 并且去掉緩存區(qū)函數(shù)
*/
//獲取輸出緩存,否則imagepng會把圖片輸出到瀏覽器
ob_start();
imagepng($img);
imagedestroy($img);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
三、把新的圖片鑲嵌到小程序碼上
/**
* 在二維碼的中間區(qū)域鑲嵌圖片
* @param $QR 二維碼數(shù)據(jù)流。比如file_get_contents(imageurl)返回的東東,或者微信給返回的東東
* @param $logo 中間顯示圖片的數(shù)據(jù)流。比如file_get_contents(imageurl)返回的東東
* @return 返回圖片數(shù)據(jù)流
*/
public function qrcodeWithLogo($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左上角所在坐標點
/**
* 重新組合圖片并調(diào)整大小
* imagecopyresampled() 將一幅圖像(源圖象)中的一塊正方形區(qū)域拷貝到另一個圖像中
*/
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");
* 并且去掉緩存區(qū)函數(shù)
*/
//獲取輸出緩存,否則imagepng會把圖片輸出到瀏覽器
ob_start();
imagepng($QR);
imagedestroy($QR);
imagedestroy($logo);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}