public function downQrCode($productId)
{
header("Content-type:image/png");
$label = "扫描二维码领取礼品";
$qrPath = runtime_path('qrcode');
// 存在清空文件不存在创建目录
if (is_dir($qrPath)){
array_map('unlink', glob($qrPath .'*'));
} else {
mkdir($qrPath,0777,true);
}
require "vendor/dh2y/think-qrcode/src/phpqrcode/phpqrcode.php";
$qrCode = new \QRcode();
$row = Voucher::find($productId);
$res = $row['details'];
$result = [];
foreach ($res as $code) {
$domain = $_SERVER['REQUEST_SCHEME'] . '://' .$_SERVER['SERVER_NAME'];
$data = $domain.'/daijinquan?param=mini&code='.$code."_".$productId;
$level = 'L';
$size = 7;
$QR = $qrPath . time() . ".png";
$qrCode->png($data, $QR, $level, $size, 2);
$result[] = $this->fugai($QR,$code);
}
//生成zip文件下载
$path = app()->getRootPath() . 'upfile/zip/';
if (!is_dir($path))mkdir($path);
array_map('unlink', glob($path .'*'));
$downName = $row['name'] . date("YmdHis") . '.zip';
$zipName = $path . 'qrcode'.time().'.zip';
$zip = new \ZipArchive();
if ($zip->open($zipName, \ZIPARCHIVE::OVERWRITE | \ZIPARCHIVE::CREATE)!==TRUE) {
exit('无法打开文件,或者文件创建失败');
}
foreach($result as $val){
//解压zip直接是文件
$zip->addFile($val, basename($val));
}
$zip->close();//关闭
if(!file_exists($zipName)){
exit("无法找到文件"); //即使创建,仍有可能失败
}
//如果不要下载,下面这段删掉即可,如需返回压缩包下载链接,只需 return $downName;
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($downName)); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($zipName)); //告诉浏览器,文件大小
@readfile($zipName);
return $downName;
}
public function fugai($QR,$code){
$code_path = runtime_path('qrcode').$code.time().'.png';
$dst_path = root_path('public')."share.png";//背景图片路径
$src_path = $QR;//覆盖图
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
//获取覆盖图图片的宽高
list($src_w, $src_h) = getimagesize($src_path);
//将覆盖图复制到目标图片上,最后个参数100是设置透明度(100是不透明),这里实现不透明效果
imagecopymerge($dst, $src, 230, 640, 0, 0, $src_w, $src_h, 100); //位置可以自己调试
imagepng($dst,$code_path);//根据需要生成相应的图片
imagedestroy($dst);
imagedestroy($src);
$image = imagecreatefrompng($code_path);
$font = root_path('public').'MSYH.TTF'; // 字体文件
$color = imagecolorallocate($image,0,0,0); // 文字颜色
imagettftext($image, 40, 0, 250, 600, $color, $font, $code); // 创建文字
// imagettftext($image, 10, 0, 250, 600, $color, $font, $code); // 创建文字
imagepng($image,$code_path);
return $code_path;
}