[PHP] 纯文本查看 复制代码
**
* 获取标准二维码格式
*
* [url=home.php?mod=space&uid=952169]@Param[/url] unknown $url
* @param unknown $path
* @param unknown $ext
*/
function getQRcode($url, $path, $qrcode_name)
{
if (! is_dir($path)) {
$mode = intval('0777', 8);
mkdir($path, $mode, true);
chmod($path, $mode);
}
$path = $path . '/' . $qrcode_name . '.png';
if (file_exists($path)) {
unlink($path);
}
QRcode::png($url, $path, '', 4, 1);
return $path;
}
/**
* 制作推广二维码
*
* @param unknown $path
* 二维码地址
* @param unknown $thumb_qrcode中继二维码地址
* @param unknown $user_headimg
* 头像
* @param unknown $shop_logo
* 店铺logo
* @param unknown $user_name
* 用户名
* @param unknown $data
* 画布信息 数组
* @param unknown $create_path
* 图片创建地址 没有的话不创建图片
*/
function showUserQecode($upload_path, $path, $thumb_qrcode, $user_headimg, $shop_logo, $user_name, $data, $create_path)
{
// 暂无法生成
if (! strstr($path, "http://") && ! strstr($path, "https://")) {
if (! file_exists($path)) {
$path = "static/images/template_qrcode.png";
}
}
if (! file_exists($upload_path)) {
$mode = intval('0777', 8);
mkdir($upload_path, $mode, true);
}
// 定义中继二维码地址
$image = \think\Image::open($path);
// 生成一个固定大小为360*360的缩略图并保存为thumb_....jpg
$image->thumb(288, 288, \think\Image::THUMB_CENTER)->save($thumb_qrcode);
// 背景图片
$dst = $data["background"];
if (! strstr($dst, "http://") && ! strstr($dst, "https://")) {
if (! file_exists($dst)) {
$dst = "static/images/qrcode_bg/shop_qrcode_bg.png";
}
}
// dump($dst);die;
// $dst = "http://pic107.nipic.com/file/20160819/22733065_150621981000_2.jpg";
// 生成画布
list ($max_width, $max_height) = getimagesize($dst);
// $dests = imagecreatetruecolor($max_width, $max_height);
$dests = imagecreatetruecolor(640, 1134);
$dst_im = getImgCreateFrom($dst);
imagecopy($dests, $dst_im, 0, 0, 0, 0, $max_width, $max_height);
// ($dests, $dst_im, 0, 0, 0, 0, 640, 1134, $max_width, $max_height);
imagedestroy($dst_im);
// 并入二维码
// dump($thumb_qrcode);die;
// $src_im = imagecreatefrompng($thumb_qrcode);
$src_im = getImgCreateFrom($thumb_qrcode);
$src_info = getimagesize($thumb_qrcode);
// imagecopy($dests, $src_im, $data["code_left"] * 2, $data["code_top"] * 2, 0, 0, $src_info[0], $src_info[1]);
imagecopy($dests, $src_im, $data["code_left"] * 2, $data["code_top"] * 2, 0, 0, $src_info[0], $src_info[1]);
imagedestroy($src_im);
// 并入用户头像
if (! strstr($user_headimg, "http://") && ! strstr($user_headimg, "https://")) {
if (! file_exists($user_headimg)) {
$user_headimg = "uploads/qrcode/promoteuser/thumb_template/def_user_img.png";
}
}
$src_im_1 = getImgCreateFrom($user_headimg);
$src_info_1 = getimagesize($user_headimg);
// imagecopy($dests, $src_im_1, $data['header_left'] * 2, $data['header_top'] * 2, 0, 0, $src_info_1[0], $src_info_1[1]);
// imagecopy($dests, $src_im_1, $data['header_left'] * 2, $data['header_top'] * 2, 0, 0, $src_info_1[0], $src_info_1[1]);
imagecopyresampled($dests, $src_im_1, $data['header_left'] * 2, $data['header_top'] * 2, 0, 0, 80, 80, $src_info_1[0], $src_info_1[1]);
imagedestroy($src_im_1);
// 并入网站logo
if ($data['is_logo_show'] == '1') {
if (! strstr($shop_logo, "http://") && ! strstr($shop_logo, "https://")) {
if (! file_exists($shop_logo)) {
$shop_logo = "uploads/qrcode/promoteuser/thumb_template/def.png";
}
}
$src_im_2 = getImgCreateFrom($shop_logo);
$src_info_2 = getimagesize($shop_logo);
// imagecopy($dests, $src_im_2, $data['logo_left'] * 2, $data['logo_top'] * 2, 0, 0, $src_info_2[0], $src_info_2[1]);
imagecopyresampled($dests, $src_im_2, $data['logo_left'] * 2, $data['logo_top'] * 2, 0, 0, 200, 80, $src_info_2[0], $src_info_2[1]);
imagedestroy($src_im_2);
}
// 并入用户姓名
if ($user_name == "") {
$user_name = "";
}
$rgb = hColor2RGB($data['nick_font_color']);
$bg = imagecolorallocate($dests, $rgb['r'], $rgb['g'], $rgb['b']);
$name_top_size = $data['name_top'] * 2 + $data['nick_font_size'];
@imagefttext($dests, $data['nick_font_size'], 0, $data['name_left'] * 2, $name_top_size, $bg, "/static/font/Microsoft.ttf", $user_name);
header("Content-type: image/jpeg");
if ($create_path == "") {
imagejpeg($dests);
} else {
imagejpeg($dests, $create_path);
}
}
// 分类获取图片对象
function getImgCreateFrom($img_path)
{
$ename = getimagesize($img_path);
$ename = explode('/', $ename['mime']);
$ext = $ename[1];
switch ($ext) {
case "png":
$image = imagecreatefrompng($img_path);
break;
case "jpeg":
$image = imagecreatefromjpeg($img_path);
break;
case "jpg":
$image = imagecreatefromjpeg($img_path);
break;
case "gif":
$image = imagecreatefromgif($img_path);
break;
}
return $image;
}
/**
* 颜色十六进制转化为rgb
*/
function hColor2RGB($hexColor)
{
$color = str_replace('#', '', $hexColor);
if (strlen($color) > 3) {
$rgb = array(
'r' => hexdec(substr($color, 0, 2)),
'g' => hexdec(substr($color, 2, 2)),
'b' => hexdec(substr($color, 4, 2))
);
} else {
$color = str_replace('#', '', $hexColor);
$r = substr($color, 0, 1) . substr($color, 0, 1);
$g = substr($color, 1, 1) . substr($color, 1, 1);
$b = substr($color, 2, 1) . substr($color, 2, 1);
$rgb = array(
'r' => hexdec($r),
'g' => hexdec($g),
'b' => hexdec($b)
);
}
return $rgb;
}