php api获取二维码、组合生成图片

这篇博客详细介绍了如何在PHP中生成二维码并与背景图片合并,同时添加文字说明,最后进行图片压缩的过程。主要涉及的方法包括创建二维码、合并图片、在图片上添加文字以及图片压缩等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值