public function test()
{
$template = ROOT_PATH . "public/uploads/back.png";
$x = "70";
$y = "30";
$code = ["尚勤楼","二年一班","112233"];
$qrcode = $this->create_qrcode($mac,$type);
$qrcode_url = ROOT_PATH . "public/uploads/".$v['mac']."_qrcode.png";
$curl = new Curl();
$curl->download($qrcode, $qrcode_url);
$filename = $addr."_".$name."_".$mac;
$pic = $this->getActivityImg($template, $qrcode_url, $x, $y, $code, time(), $filename);
$pic = "/public" .explode("public",$pic)[1];
return $pic;
}
/**
* api生成二维码(测试用)
* @param string $mac 设备MAC
* @param string $type 设备TYPE
*/
public function create_qrcode($mac,$type)
{
$ip = empty($ip) ? $_SERVER['HTTP_HOST'] : $ip;
$port = empty($port) ? $_SERVER['SERVER_PORT'] : $port;
$mac = empty($mac) ? "112233445566" : $mac;
$type = empty($type) ? "0A" : $type;
$data = ["ip"=>$ip,"port"=>$port,"mac"=>$mac,"type"=>$type];
$data = json_encode($data);
$http = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http';
//$url=urlencode("{$http}://{$ip}:{$port}/?m=Index&c=index&a=index_qu&mac={$mac}&type={$type}");
// $url=urlencode("{$http}://{$_SERVER['HTTP_HOST']}:{$_SERVER['SERVER_PORT']}/?ip={$ip}&port={$port}&mac={$mac}&type={$type}");
$url=urlencode("{$data}");
$qrcode = 'https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=' . $url;
return $qrcode;
}
/**
* 合并二维码生成图片
*
* @param [type] $template 背景图
* @param [type] $qr_url 二维码地址
* @param [type] $x x轴坐标
* @param [type] $y y轴坐标
* @param [type] $code 图片文字说明
* @param [type] $jointime 创建时间
* @param [type] $mac mac
* @return void
*/
private function getActivityImg($template, $qr_url, $x, $y, $code, $jointime, $filename) {
$time = date("Ym", $jointime);
//创建文件夹
// $dir = iconv("UTF-8", "GBK", ROOT_PATH."public/uploads/" . $time . "");
$dir = iconv("UTF-8", "GBK", ROOT_PATH."public/uploads/");
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
//合成带logo的二维码图片跟 模板图片--------------start
$path_1 = $template;
$path_2 = $qr_url;
$image_1 = imagecreatefrompng($path_1);
$image_2 = imagecreatefrompng($path_2);
// $image_2 = imagecreatefromstring(file_get_contents($path_2));
$image_3 = imageCreatetruecolor(imagesx($image_1), imagesy($image_1));
$color = imagecolorallocate($image_3, 0, 0, 0);
//在图片上加文字
$font_file = ROOT_PATH . "public/static/fonts/simkai.ttf"; //字体文件
$font_color_1 = ImageColorAllocate($image_1, 100, 100, 100);
imagettftext($image_1, 20, 0, 50, 300, $font_color_1, $font_file, "位置:" . $code[0]); //添加文字
imagettftext($image_1, 20, 0, 50, 350, $font_color_1, $font_file, "名称:" . $code[1]);
imagettftext($image_1, 20, 0, 50, 400, $font_color_1, $font_file, "MAC :" . $code[2]);
imageline($image_1, 100, 100, 100, 100, $font_color_1);
//在图片上加文字end
imagefill($image_3, 0, 0, $color);
imageColorTransparent($image_3, $color);
imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
imagecopymerge($image_3, $image_2, $x, $y, 0, 0, imagesx($image_2), imagesy($image_2), 100);
//合成带logo的二维码图片跟 模板图片--------------end
//输出到本地文件夹
$source = ROOT_PATH.'public/uploads/' . $filename . '.png'; //原图
$EchoPath = ROOT_PATH.'public/uploads/'. $filename .'.jpg'; //压缩后图片
imagepng($image_3, $source);
$this->handlePic($source);
imagedestroy($image_3);
unlink($source);
unlink($path_2);
//返回生成的路径
return $EchoPath;
}
/**
* 图片压缩
* @param $srcPathName
*/
public function handlePic($srcPathName)
{
$srcFile = $srcPathName;
$srcFileExt = strtolower(trim(substr(strrchr($srcFile, '.'), 1)));
if ($srcFileExt == 'png') {
$dstFile = str_replace('.png', '.jpg', $srcPathName);
$photoSize = GetImageSize($srcFile);
$pw = $photoSize[0];
$ph = $photoSize[1];
$dstImage = ImageCreateTrueColor($pw, $ph);
imagecolorallocate($dstImage, 255, 255, 255);
//读取图片
$srcImage = ImageCreateFromPNG($srcFile);
//合拼图片
imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $pw, $ph, $pw, $ph);
imagejpeg($dstImage, $dstFile, 70);
imagedestroy($srcImage);
}
}
php api获取二维码、组合生成图片
最新推荐文章于 2025-05-26 08:15:00 发布