話不多說,直接上程式碼
$goods = GoodsModel::info(['id'=>$param['goods_id']]);
$appletsInfo = AppletsModel::findByUid($param['applets_id']);
$userInfo = UserModel::info(['id'=>$param['user_id']]);
$config = [
'app_id' => $appletsInfo['appid'],
'secret' => $appletsInfo['appsecret'],
'response_type' => 'array',
'log' => [
'level' => 'debug',
'file' => __DIR__.'/wechat.log',
],
];
$app = Factory::miniProgram($config);
$response = $app->app_code->getUnlimit(round(0000,9999));
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
if(!\file_exists('storage/qrcode')){
mkdir('storage/qrcode');
}
$filename = $response->save('storage/qrcode');
}
$img = imagecreatetruecolor(600,900);
$color =imagecolorallocate($img,255,255,255);
imagefill($img,0,0,$color);
imagepng($img,'haha.png');
$path_1 = BASE_PATH.'storage/qrcode//'.$filename;
$path_2 = BASE_PATH.'/'.'haha.png';
switch (\substr($path_1,-3)) {
case "png":
$dst = imagecreatefrompng($path_1);
break;
case "jpg":
$dst = imagecreatefromjpeg($path_1);
break;
case "jpeg":
$dst = imagecreatefromjpeg($path_1);
break;
case "gif":
$dst = imagecreatefromgif($path_1);
break;
}
switch (\substr($path_2,-3)) {
case "png":
$src = imagecreatefrompng($path_2);
break;
case "jpg":
$src = imagecreatefromjpeg($path_2);
break;
case "jpeg":
$src = imagecreatefromjpeg($path_2);
break;
case "gif":
$src = imagecreatefromgif($path_2);
break;
}
$thumb = imagecreatefromstring(file_get_contents($goods['thumb']));
switch ($thumb) {
case "png":
$thumb = imagesy(imagecreatefrompng($goods['thumb']));
break;
case "jpg":
$thumb = imagesy(imagecreatefromjpeg($goods['thumb']));
break;
case "jpeg":
$thumb = imagesy(imagecreatefromjpeg($goods['thumb']));
break;
case "gif":
$thumb = imagesy(imagecreatefromgif($goods['thumb']));
break;
}
imagecopyresampled($src, $dst, 400, 685, 0, 0, 160, 160, imagesx($dst), imagesy($dst));
imagecopyresampled($src, $thumb, 110, 240, 0, 0, 400, 400, imagesx($thumb), imagesy($thumb));
$width = imagesx($src);
$height = imagesy($src);
$colors =imagecolorallocate($img,0,0,0);
$font = BASE_PATH."simfang_ttf/simfang.ttf";
$font_size = BASE_PATH."simfang_ttf/simhei.ttf";
$fontBox = imagettfbbox(25, 0, $font, $appletsInfo['shop_name']);
$fontBox_title = imagettfbbox(25, 0, $font, $appletsInfo['shop_name']);
imagettftext($src, 25, 0, ceil(($width - $fontBox[2]) / 2), ceil(($height - $fontBox[1] - $fontBox[7]) / 7), $colors, $font_size, $appletsInfo['shop_name']);
imagettftext($src, 25, 0, 50, 750, $colors, $font, '¥'.$goods['selling_price']);
imagettftext($src, 15, 0, ceil(($width - $fontBox_title[2]) / 2), ceil(($height - $fontBox_title[1] - $fontBox_title[7]) / 5.5), $colors, $font, '來自:'.$userInfo['nickname'].'的推薦');
for ($i=0; $i < mb_strlen($goods['title']); $i++) {
$letter[] = mb_substr($goods['title'],$i,1);
}
$content = '';
foreach ($letter as $l) {
$test_str = $content." ".$l;
$fontBox = imagettfbbox(15, 0, $font, $test_str);
if (($fontBox[2] > 220) && ($content !== "")) {
$content .= "\n";
}
$content .= $l;
}
imagettftext($src, 20, 0, 50, 780, $colors, $font_size, $content);
imagepng($src,'test2.png');
效果如下:
本作品採用《CC 協議》,轉載必須註明作者和本文連結