当前位置:网站首页>php api获取二维码、组合生成图片
php api获取二维码、组合生成图片
2022-06-30 08:10:00 【BlizzardWu】
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);
}
}
边栏推荐
- Deep learning - embedding matrix and learning word embedding andword2vec
- tp5设置直接下载文件
- Unity简单shader
- Deep learning -- sequence model and mathematical symbols
- 牛客小白月赛52
- Experiment 2 LED button PWM 2021/11/22
- 【NVMe2.0b 14-8】Set Features(下篇)
- Summary and common applications of direction and angle operators in Halcon
- [JUC series] overview of fork/join framework
- CRM&PM如何帮助企业创造最优销售绩效
猜你喜欢

【kotlin 协程】万字协程 一篇完成kotlin 协程进阶

【NVMe2.0b 14-7】Set Features(上篇)

Deep learning -- using word embedding and word embedding features

Final review -php learning notes 2-php language foundation

At the end of June, you can start to make preparations, otherwise you won't have a share in such a profitable industry

End-to-end 3D Point Cloud Instance Segmentation without Detection

Deep learning - networks in networks and 1x1 convolution

Recurrence relation (difference equation) -- Hanoi problem

Deep learning vocabulary representation

1162 Postfix Expression
随机推荐
Deep learning - residual networks resnets
[flower carving experience] 12 build the Arduino development environment of esp32c3
Want to ask, how to choose securities companies for stock speculation? Is it safe to open an account online?
Rendering engine development
Transformer architecture understanding
Recurrence relation (difference equation) -- Hanoi problem
Deep learning - bounding box prediction
Dlib database face
QT event cycle
Construction of module 5 of actual combat Battalion
增强for循环的增删操作 & 迭代器删除集合元素
跳槽字节跳动很难嘛?掌握这些技巧,你也能轻松通过
领域驱动下cloud项目中单个服务的示例
What are the Amazon evaluation terms?
Graffiti Wi Fi & ble SoC development slide strip
Redis设计与实现(七)| 发布 & 订阅
F12 packet capture is used for the whole process analysis of postman interface test
JS code case
电流探头的干扰源电流谱测试
Experiment 4 QT