二维码缓存图片写上文字并打包下载

public function download($ids = '')  //下载图片
    {
        $pk = $this->model->getPk();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        $list = $this->model->where($pk, 'in', $ids)->select();
        $imgDir = './uploads/code'.date('mdHis');   //必须是项目所在的绝对路径
        if(!file_exists($imgDir)){
            //检查是否有该文件夹,如果没有就创建,并给予最高权限
            mkdir($imgDir, 0700);
        }
        foreach ($list as $k => $v) {
            $file = _curl($_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . '/api/index/code_graph?code=' . $v->id . '&user_id=' . $v->user_id);
            $filename = $v->id . ".png"; //新图片名称
            $newFilePath = $imgDir.'/'.$filename;
            file_put_contents($newFilePath, $file);
            $user_name = isset($this->username()[$v->user_id])?$this->username()[$v->user_id]:$v->user_id;
            $this->addFontToPic($newFilePath,'公司名称:'.$user_name.' - 位置:'.$v->position.'-'.$v->specific_position,'设备编号:'.date('ym',$v->createtime).$v->id.' - 设备名称:'.$v->fname,$newFilePath);
        }
        $zip=new \ZipArchive; //打开PHP自带的php_zip
        if($zip->open($imgDir.'.zip',\ZIPARCHIVE::CREATE)===TRUE){
            //$zip->addFile($n_file);//假设加入的文件名是image.txt,在当前路径下
            addFileToZip($imgDir, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
            $zip->close(); //关闭处理的zip文件
        }
        rmdirs($imgDir, $withself = true); //遍历删除文件
        $filename = $imgDir.'.zip';
        header('Content-disposition:attachment;filename='. basename($filename));
        header('Content-length:'. filesize($filename));
        readfile($filename);
        unlink($filename);
    }

protected function addFontToPic($pic,$font1,$font2,$filename)
    {
        $image = imagecreatefrompng($pic);//注意图片格式
        $image_det = imagecreatefrompng('./background.png');
        imagecopymerge($image_det,$image,0,0,0,0,imagesx($image),imagesy($image),80);
        $text_color = imagecolorallocate($image_det, 233, 14, 91);
        imagettftext($image_det,8,0,5,326,$text_color,'./msyh.ttc',$font1);//第一行文字
        imagettftext($image_det,8,0,5,338,$text_color,'./msyh.ttc',$font2);//第二行文字
        header('Content-Type: image/png');
        imagepng($image_det, $filename);
        imagedestroy($image);
        imagedestroy($image_det);
    }
    
  protected function createimg()//创建一个背景图
    {
        $im = @imagecreate(320, 340) or die("Cannot Initialize new GD image stream");
        imagecolorallocate($im, 255, 255, 255);//背景是白的
        imagepng($im,'background.png');
        imagedestroy($im);

    }  






function addFileToZip($path, $zip)//遍历添加文件压缩
    {
        $handler = opendir($path); //打开当前文件夹由$path指定。
        while (($filename = readdir($handler)) !== false) {
            if ($filename != "." && $filename != "..") {//文件夹文件名字为'.'和‘..',不要对他们进行操作
                if (is_dir($path . "/" . $filename)) {// 如果读取的某个对象是文件夹,则递归
                    addFileToZip($path . "/" . $filename, $zip);
                } else { //将文件加入zip对象
                    $zip->addFile($path . "/" . $filename);
                }
            }
        }
        @closedir($path);
    }
 
function rmdirs($dirname, $withself = true)//遍历删除
    {
        if (!is_dir($dirname)) {
            return false;
        }
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($files as $fileinfo) {
            $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
            $todo($fileinfo->getRealPath());
        }
        if ($withself) {
            @rmdir($dirname);
        }
        return true;
    }

 
 function _curl($url, $data = '')//比file_put_contents()资源占用少
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

炒鸡时光机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值