首页 > 其他 > 详细

在画布中添加二维码加文字 和 压缩多图片到一个压缩包中

时间:2020-08-19 00:37:48      阅读:93      评论:0      收藏:0      [点我收藏+]

一、在画布中添加二维码加文字

<?php 

class Image{

 public function erweima($device){
require_once ‘mobile/example/phpqrcode/phpqrcode.php‘;
$device_sn = $device;
$urls =$_SERVER[‘REQUEST_SCHEME‘] . ‘://‘ . $_SERVER[‘SERVER_NAME $url = $urls."/Mobile/index.html?device_sn=".$device_sn;
    $url = urldecode($url);
$qr_code_path = ‘uploads/QRcodeDevice/‘.date("Y-m-d").‘/‘;
if (!file_exists($qr_code_path)) {
mkdir($qr_code_path);
}
/* 生成二维码 */
$qr_code_file = $qr_code_path.$device_sn.‘.png‘;
\QRcode::png($url, $qr_code_file, ‘H‘, 6,6);//最后一个6是控制白边大小
$background = "mobile/font/back.png";
$logo = "mobile/font/logo.png";
$this->mark_photo($qr_code_file,$device_sn,$logo,$qr_code_file);
$where = array(‘device_sn‘=>$device);

$arr[‘QRcode‘] = $urls."/".$qr_code_file;
return $arr;
}

//拼接图片,logo,文字
private function mark_photo($background,$text,$logo,$filename){
  $info = getimagesize($background); // 获取图片信息
  $type = image_type_to_extension($info[2],false); // 获取图片扩展名
  $fun = "imagecreatefrom{$type}"; // 构建处理图片方法名-关键是这里
  $back= $fun($background); // 调用方法处理
    $color=imagecolorallocate($back,0,0,0);
   $logo_info = getimagesize($logo);
  $logo_type = image_type_to_extension($info[2],false); // 获取图片扩展名
  $logo_fun  = "imagecreatefrom{$logo_type}"; // 构建处理图片方法名-关键是这里
    $logo_w=$info[0];
$logo_h=$info[1];
$font="mobile/font/STYH.otf"; // 字体文件
//imagettftext只认utf8字体,所以用iconv转换
imagettftext($back, 21, 0, 40, 337, $color, $font, $text);//调二维码中字体位置
//执行合成调整位置
imagecopyresampled($back, $logo, 139,140, 0, 0, 65, 65, $logo_w, $logo_h);//调中间logo位置
  $image_fun = ‘image‘.$type;
$image_fun($back,$filename);//保存
imagedestroy($back);
imagedestroy($logo);
  return json_encode(‘code‘=>0,‘msg‘=>‘成功‘,array(‘filename‘=>$filename));
}
/**
 *压缩文件
* @return mixed
*/
public function getZipper()
{
$zip = new \ZipArchive();
// 图片路径
$img_path = public_path(‘/uploads/qr‘);


$img_files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($img_path));


$path = public_path(‘/uploads/download‘);

if(!file_exists($path)){
mkdir($path,0777,true);
}
$zip_file = $path.‘/download.zip‘;

$zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);

$this->forZip($zip, $img_files, $img_path, ‘‘);

$zip->close();
return response()->download($zip_file);
}
//循环文件 添加压缩包中
private function forZip($zip, $files, $file_path, $new_path)
{
foreach ($files as $name => $file) {
// 我们要跳过所有子目录
if ( ! $file->isDir()) {
$filePath = $file->getRealPath();
// 用 substr/strlen 获取文件名
$relativePath = $new_path . substr($filePath, strlen($file_path));
$zip->addFile($filePath, $relativePath);
}
}
}

}

在画布中添加二维码加文字 和 压缩多图片到一个压缩包中

原文:https://www.cnblogs.com/hua-nuo/p/13526897.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!