PHP批量生成带底部编号二维码(二维码生成+文字生成图片+图片拼接合并)

发布时间 2023-03-22 21:13:24作者: 垖垏尐

 phpqrcode 生成 二维码  -->  编号字符串生成图片 ---> 二维码与编号图片拼接---->压缩 下载

//生成二维码图片
function makeCodeImg($url, $product_sn = '2023**82024')
{
    $path = 'upload/product_qr_code';
    if (!is_dir($path)) {
        mkdir($path, 0777, true);
    }
    include_once app()->getRootPath()."/vendor/image-master/phpqrcode/phpqrcode.php";
    $value = $url;                  //二维码内容
    $errorCorrectionLevel = 'L';    //容错级别
    $matrixPointSize = 12;           //生成图片大小

    $filename = $path . '/' . $product_sn . '.jpg';
    QRcode::png($value, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
    $QR = $filename;                //已经生成的原始二维码图片文件
    $QR = imagecreatefromstring(file_get_contents($QR));
    imagejpeg($QR, $product_sn . 'jpg');
}

//文字生成图片
function makeImgWithStr($filename, $text, $font_size=20)
{
    $font = app()->getRootPath()."/vendor/image-master/font/Arial/Arial.ttf";
    //图片尺寸
    $im = imagecreatetruecolor(444, 70);
    //背景色
    $white = imagecolorallocate($im, 255, 255, 255);
    //字体颜色
    $black = imagecolorallocate($im, 0, 0, 0);

    imagefilledrectangle($im, 0, 0, 444, 300, $white);
    $txt_max_width = intval(0.8 * 444);
    $content = "";
    for ($i = 0; $i < mb_strlen($text); $i++) {
        $letter[] = mb_substr($text, $i, 1);
    }
    foreach ($letter as $l) {
        $test_str = $content . " " . $l;
        $test_box = imagettfbbox($font_size, 0, $font, $test_str);
        // 判断拼接后的字符串是否超过预设的宽度。超出宽度添加换行
        if (($test_box[2] > $txt_max_width) && ($content !== "")) {
            $content .= "\n";
        }
        $content .= $l;
    }

    $txt_width = $test_box[2] - $test_box[0];

    $y = 70 * 0.5; // 文字从何处的高度开始
    $x = (444 - $txt_width) / 2; //文字居中
    // echo $x;die;
    //文字写入
    imagettftext($im, $font_size, 0, $x, $y, $black, $font, $content); //写 TTF 文字到图中
    //图片保存
//    $filename = app()->getRootPath()."/public/".$filename;
    if (!is_dir('upload/sn_str_img')) {
        mkdir('upload/sn_str_img', 0777, true);
    }
    imagejpeg($im, $filename);
}

/**
 * 合并图片,拼接合并
 * @param array $image_path 需要合成的图片数组
 * @param $save_path 合成后图片保存路径
 * @param string $axis 合成方向
 * @param string $save_type 合成后图片保存类型
 * @return bool|array
 */
function CompositeImage(array $image_path, $save_path, $axis = 'y', $save_type = 'png')
{
    if (count($image_path) < 2) {
        return false;
    }
    //定义一个图片对象数组
    $image_obj = [];
    //获取图片信息
    $width = 0;
    $height = 0;
    foreach ($image_path as $k => $v) {
        $pic_info = getimagesize($v);
        list($mime, $type) = explode('/', $pic_info['mime']);
        //获取宽高度
        $width += $pic_info[0];
        $height += $pic_info[1];
        if ($type == 'jpeg') {
            $image_obj[] = imagecreatefromjpeg($v);
        } elseif ($type == 'png') {
            $image_obj[] = imagecreatefrompng($v);
        } else {
            $image_obj[] = imagecreatefromgif($v);
        }
    }
    //按轴生成画布方向
    if ($axis == 'x') {
        //TODO X轴无缝合成时请保证所有图片高度相同
        $img = imageCreatetruecolor($width, imagesy($image_obj[0]));
    } else {
        //TODO Y轴无缝合成时请保证所有图片宽度相同
        $img = imageCreatetruecolor(imagesx($image_obj[0]), $height);
    }
    //创建画布颜色
    $color = imagecolorallocate($img, 255, 255, 255);
    imagefill($image_obj[0], 0, 0, $color);
    //创建画布
    imageColorTransparent($img, $color);
    imagecopyresampled($img, $image_obj[0], 0, 0, 0, 0, imagesx($image_obj[0]), imagesy($image_obj[0]), imagesx($image_obj[0]), imagesy($image_obj[0]));
    $yx = imagesx($image_obj[0]);
    $x = 0;
    $yy = imagesy($image_obj[0]);
    $y = 0;
    //循环生成图片
    for ($i = 1; $i <= count($image_obj) - 1; $i++) {
        if ($axis == 'x') {
            $x = $x + $yx;
            imagecopymerge($img, $image_obj[$i], $x, 0, 0, 0, imagesx($image_obj[$i]), imagesy($image_obj[$i]), 100);
        } else {
            $y = $y + $yy;
            imagecopymerge($img, $image_obj[$i], 0, $y, 0, 0, imagesx($image_obj[$i]), imagesy($image_obj[$i]), 100);
        }
    }
    if (!is_dir('upload/pin_code')) {
        mkdir('upload/pin_code', 0777, true);
    }
    //设置合成后图片保存类型
    if ($save_type == 'png') {
        imagepng($img, $save_path);
    } elseif ($save_type == 'jpg' || $save_type == 'jpeg') {
        imagejpeg($img, $save_path);
    } else {
        imagegif($img, $save_path);
    }
    return true;
}

//图片等宽处理
function ImgCompress($src, $out_with = 150)
{
    // 获取图片基本信息
    list($width, $height, $type, $attr) = getimagesize($src);
    // 获取图片后缀名
    $pic_type = image_type_to_extension($type, false);
    // 拼接方法
    $imagecreatefrom = "imagecreatefrom" . $pic_type;
    // 打开传入的图片
    $in_pic = $imagecreatefrom($src);
    // 压缩后的图片长宽
    $new_width = $out_with;
    $new_height = $out_with / $width * $height;
    // 生成中间图片
    $temp = imagecreatetruecolor($new_width, $new_height);
    // 图片按比例合并在一起。
    imagecopyresampled($temp, $in_pic, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    // 销毁输入图片
    imagejpeg($temp, 'upload/merge' . time() . ".jpg");
    imagedestroy($in_pic);
    return array($temp, $new_width, $new_height);
}

//生成带编号说明的二维码 (生成二维码  文字生成图片 图片合并拼接)
function makeMergerImg($sn_product){
    makeCodeImg('https://www.baidu.com/',$sn_product);
    makeImgWithStr('upload/sn_str_img/'.$sn_product.'.jpg',$sn_product,30);
    CompositeImage(['upload/product_qr_code/'.$sn_product.'.jpg','upload/sn_str_img/'.$sn_product.'.jpg'],'upload/pin_code/'.$sn_product.'.png');
    unlink('upload/sn_str_img/'.$sn_product.'.jpg');
    unlink('upload/product_qr_code/'.$sn_product.'.jpg');
    unlink($sn_product.'jpg');
}

//生成压缩zip文件 $file_name 最终生成的文件名,包含路径   $file_list,用来生成file_name的文件数组
function makeZip($file_name, $file_list)
{
    if (file_exists($file_name)) {
        unlink($file_name);
    }
    //重新生成文件
    $zip = new ZipArchive();
    if ($zip->open($file_name, ZIPARCHIVE::CREATE) !== TRUE) {
        exit('无法打开文件,或者文件创建失败');
    }
    foreach ($file_list as $val) {
        if (file_exists($val)) {
            $new_filename = substr($val, strrpos($val, '/') + 1);
            $zip->addFile($val,$new_filename);
        }
    }
    $zip->close();//关闭
    if (!file_exists($file_name)) {
        exit('无法找到文件'); //即使创建,仍有可能失败
    }
}

//下载
function download($file){
    if ( file_exists ( $file )) {
        header ( 'Content-Description: File Transfer' );
        header ( 'Content-Type: application/octet-stream' );
        header ( 'Content-Disposition: attachment; filename=' . basename ( $file ));
        header ( 'Content-Transfer-Encoding: binary' );
        header ( 'Expires: 0' );
        header ( 'Cache-Control: must-revalidate' );
        header ( 'Pragma: public' );
        header ( 'Content-Length: ' . filesize ( $file ));
        ob_clean ();
        flush ();
        readfile ( $file );
        exit;
    }
}
//删除压缩包
        $product_str_start = 'cb05-00000155'; $product_str_end = 'cb05-00000160';
        $press = explode('-',$product_str_start)[0];
        $product_sn_start = explode('-',$product_str_start)[1];
        $product_sn_end = explode('-',$product_str_end)[1];
        $count = $product_sn_end - $product_sn_start;
        $arr = [];
        for ($i=0;$i<=$count;$i++){
            $product_sn = $press.'-'.substr($product_sn_start+$i+1000000,1,7);
            makeMergerImg($product_sn);
            $img_arr[$i] = 'upload/pin_code/'.$product_sn.'.png';
            $arr[] = $product_sn;
        }
        //生成压缩包
        makeZip('upload/'.$params['machine_id'].'.zip',$img_arr);
        $path = app()->getRootPath()."/public/upload/pin_code/";
        foreach ($arr as $v){
            unlink($path.$v.'.png');
        }
        //下载压缩包
        download('upload/'.$params['machine_id'].'.zip');
        die;