Files
tougao/application/api/controller/Img.php
wangjinlei 3ae9be1e60 20201112
2021-07-26 14:27:01 +08:00

281 lines
9.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\api\controller;
use think\Controller;
use think\Queue;
/**
* Description of Auto
*
* @author jgll2
*/
class Img extends Controller {
public function __construct(\think\Request $request = null) {
parent::__construct($request);
}
public function test() {
echo 'okokoook';
}
public function reviewerImport() {
//接收信息
$data = $this->request->post();
//读取excel
$path = ROOT_PATH . 'public' . DS . $data['url'];
$arr = self::readExcel($path);
foreach ($arr as $v) {
if ($v['username'] == '') {
continue;
}
$datae['name'] = $v['username'];
$datae['course'] = $data['course'];
Queue::push('app\api\job\img@fire', $datae, "img");
}
}
/**
* 接收导入文件
* @return type
*/
public function up_import() {
$file = request()->file('importExcel');
if ($file) {
$info = $file->move(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'import');
if ($info) {
return json(['code' => 0, 'upurl' => str_replace("\\", "/", $info->getSaveName())]);
} else {
return json(['code' => 1, 'msg' => $file->getError()]);
}
}
}
/**
* 获取excel数据
*/
public function getExcelData() {
//接收数据
$data = $this->request->post();
$has = $this->getImgs($data['course']);
$frag = [];
if ($data['url'] != '') {
$path = ROOT_PATH . 'public' . DS . $data['url'];
$frag = self::readExcel($path);
}
$re['list'] = array_merge($frag, $has);
$re['count'] = count($frag);
$re['has'] = count($has);
return json($re);
}
/**
* 获取目前科目下的文件总数
*/
public function getAllNum(){
$data = $this->request->post();
$path = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img/'.$data['course'].DS;
$res = scandir($path);
$dirs = [];
foreach ($res as $v) {
if ($v == '.' || $v == '..') {
continue;
}
$dirs[] = ['dirname' => $v];
}
return json(count($dirs));
}
/**
* 获取课程列表
*/
public function getCourse() {
$base = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img/';
$res = scandir($base);
$dirs = [];
foreach ($res as $v) {
if ($v == '.' || $v == '..') {
continue;
}
$dirs[] = ['dirname' => $v];
}
return json($dirs);
}
/**
* 获取文件下的证书文件
*/
private function getImgs($course) {
$dir = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img/' . $course . DS;
$res = scandir($dir);
$imgs = [];
foreach ($res as $v) {
if ($v == '.' || $v == '..') {
continue;
}
$imgs[] = ['username' => substr($v, 0, strrpos($v, '.')), 'has' => '已出证'];
}
return $imgs;
}
public function ttt() {
$v = '12_王金磊.png';
echo substr($v, 0, strrpos($v, '_'));
}
/**
* 处理图片主方法
*/
private function imgCL($arr, $course) {
$cous = $this->getImgs($course);
foreach ($arr as $k => $v) {
if ($v['username'] == '') {
continue;
}
$lim = [];
if (preg_match("/^[\x7f-\xff]+$/", $v['username']) && strlen($v['username']) == 9) {
$lim = [640, 890];
} else if (preg_match("/^[\x7f-\xff]+$/", $v['username']) && strlen($v['username']) == 6) {
$lim = [670, 890];
} else {
$left = (730 - ((35 * strlen($v['username'])) / 2));
$lim = [$left, 890];
}
$template = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'template.png';
$ziti = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'siyuan.ttf';
$image = \think\Image::open($template);
$image->text($v['username'], $ziti, 50, '#000000', $lim)
->text('NO.ZYMR' . (20210619001 + $k + count($cous)), $ziti, 25, '#000000', [573, 650])
->save(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $course . DS . (1 + $k + count($cous)) . '_' . $v['username'] . '.png');
}
}
/**
*
* 清空
*/
public function clear() {
$data = $this->request->post();
$path = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img/' . $data['course'] . DS;
$dh = opendir($path);
while ($file = readdir($dh)) {
if ($file != "." && $file != "..") {
$fullpath = $path . "/" . $file;
// if (!is_dir($fullpath)) {
unlink($fullpath);
// } else {
// deldir($fullpath);
// }
}
}
closedir($dh);
return jsonSuccess(['code'=>0]);
}
//压缩文件
public function getZip() {
$data = $this->request->post();
$zipName = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'zip' . DS . $data['course'] . '.zip';
$zip = new \ZipArchive; //使用本类linux需开启zlibwindows需取消php_zip.dll前的注释
if (is_file($zipName)) {
$zip->open($zipName, \ZIPARCHIVE::OVERWRITE);
} else {
$zip->open($zipName, \ZIPARCHIVE::CREATE);
}
$dir = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $data['course'] . '/';
$files = scandir($dir);
foreach ($files as $val) {
if ($val == '.' || $val == '..') {
continue;
}
if (file_exists(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $data['course'] . '/' . $val)) {
//addFile函数首个参数如果带有路径则压缩的文件里包含的是带有路径的文件压缩
//若不希望带有路径,则需要该函数的第二个参数
$zip->addFile(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $data['course'] . '/' . $val, basename(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $data['course'] . '/' . $val)); //第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下
}
}
$zip->close(); //关闭
if (!file_exists($zipName)) {
exit("无法找到文件"); //即使创建,仍有可能失败
}
$re['url'] = 'public/zhengshu' . DS . 'zip' . DS . $data['course'] . '.zip';
return jsonSuccess($re);
}
public function testaaa(){
$this->imgZL('王金磊', 'ZLLJ');
}
/**
* 获取当前证书流水号最大值
*/
private function getmax($course) {
$dir = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img/' . $course . DS;
$res = scandir($dir);
$big = 0;
foreach ($res as $v) {
if ($v == '.' || $v == '..') {
continue;
}
$cac = intval(substr($v, 0, stripos($v, '_')));
if ($cac > $big) {
$big = $cac;
}
}
return $big;
}
/**
* 处理图片主方法
*/
public function imgZL($name, $course) {
$max = $this->getmax($course);
$lim = [];
if (preg_match("/^[\x7f-\xff]+$/", $name) && strlen($name) == 9) {
$lim = [640, 890];
} else if (preg_match("/^[\x7f-\xff]+$/", $name) && strlen($name) == 6) {
$lim = [670, 890];
} else {
$left = (730 - ((35 * strlen($name)) / 2));
$lim = [$left, 890];
}
$template = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'template1.png';
$ziti = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'siyuan.ttf';
$image = \think\Image::open($template);
$image->text($name, $ziti, 50, '#00000000', $lim)
->text('NO.ZLLJ' . (20210807001 + $max), $ziti, 30, '#00000000', [573, 660])
->save(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $course . DS . (1 + $max) . '_' . $name . '.png');
}
/**
* 读取excel数据
*/
private function readExcel($path) {
$extension = substr($path, strrpos($path, '.') + 1);
vendor("PHPExcel.PHPExcel");
if ($extension == 'xlsx') {
$objReader = new \PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load($path);
} else if ($extension == 'xls') {
$objReader = new \PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load($path);
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$frag = [];
for ($i = 1; $i <= $highestRow; $i++) {
$aa['username'] = $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
$aa['has'] = '未出证';
if($aa['username']==''){
continue;
}
$frag[] = $aa;
}
return $frag;
}
}