Skip to content

Commit

Permalink
Merge pull request #17 from DevTTL/invalid_text
Browse files Browse the repository at this point in the history
添加基于又拍云的内容识别功能 ,防止表情包的文字出现不符合社会主义价值观的文字
  • Loading branch information
PrintNow committed Jan 25, 2019
2 parents b2921a0 + 25d8bae commit a158c54
Show file tree
Hide file tree
Showing 47 changed files with 624 additions and 222 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
.idea
.git
*.zip
0.php
test
aliyun-oss
.gitignore
new
/vendor
Empty file modified Add_Template.md
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified Usage.md
100644 → 100755
Empty file.
183 changes: 108 additions & 75 deletions api.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,92 +1,125 @@
<?php
require_once "./config.php";

$OS_TYPE=DIRECTORY_SEPARATOR=='\\'?'windows':'linux';

$OS_TYPE = DIRECTORY_SEPARATOR == '\\' ? 'windows' : 'linux';

/**
* 解决 ffmpeg 对 windows 系统的绝对路径“不友好问题”。
* 好像解决的方法有点笨...
*/
if($OS_TYPE == 'windows') {
define('ROOT','.');
}else{
define('ROOT',__DIR__);
* 解决 ffmpeg 对 windows 系统的绝对路径“不友好问题”。
* 好像解决的方法有点笨...
*/
if ($OS_TYPE == 'windows') {
define('ROOT' ,'.');
} else {
define('ROOT' ,__DIR__);
}

$type = isset($_POST['type']) ? $_POST['type'] : false;
$data = isset($_POST['data']) ? $_POST['data'] : false;

/**
* 文字内容鉴别
* 必须在配置文件中开启
*/
if (INVALID_TEXT === true) {
// 配置必须不为空
if (UPYUN_KEY != '' && UPYUN_SECRET != '') {
// 导入guzzle
require_once ROOT . '/vendor/autoload.php';
// 导入Upyun
require_once ROOT . '/usr/lib/Upyun.php';

// 实例化
$upyun = new Upyun(UPYUN_KEY ,UPYUN_SECRET);

// 逐句校验
foreach ($data as $text) {
$validResult = $upyun->execute($text);
if ($validResult['code'] != 200) {
exit(json_encode($validResult));
}
}
} else {
exit(json_encode(sprintf('请补全UPYUN API参数 ,配置文件: %s' ,ROOT . '/config.php') ,JSON_UNESCAPED_UNICODE));
}
}

/**
* 鉴别结束
*/

$small = isset($_POST['small']) ? $_POST['small'] : false;
$request_time = time(true);

if($type && $data && $small){
$TEMP_ROOT = ROOT.'/templates/'.$type.'/';
$TEMP_ASS = $TEMP_ROOT.'template.ass';
$CACHE_ASS_PATH = ROOT.'/cache/'.$type.'_'.$request_time.'.ass';
if ($type && $data && $small) {
$TEMP_ROOT = ROOT . '/templates/' . $type . '/';
$TEMP_ASS = $TEMP_ROOT . 'template.ass';
$CACHE_ASS_PATH = ROOT . '/cache/' . $type . '_' . $request_time . '.ass';

if($small == 'true' || DEFAULT_CREATE_SMALL_GIF === true){
$TEMP_VIDEO = $TEMP_ROOT.'template-small.mp4';
if(!file_exists($TEMP_ROOT.'template-small.mp4')){
$TEMP_VIDEO = $TEMP_ROOT.'template.mp4';
}
}else{
$TEMP_VIDEO = $TEMP_ROOT.'template.mp4';
}
if ($small == 'true' || DEFAULT_CREATE_SMALL_GIF === true) {
$TEMP_VIDEO = $TEMP_ROOT . 'template-small.mp4';
if (! file_exists($TEMP_ROOT . 'template-small.mp4')) {
$TEMP_VIDEO = $TEMP_ROOT . 'template.mp4';
}
} else {
$TEMP_VIDEO = $TEMP_ROOT . 'template.mp4';
}

/**
* 判断根目录是否存在 cache 目录,不存在则创建
*/
if(!file_exists(ROOT.'/cache')){
if(mkdir(ROOT.'/cache',0777) === false){
$result['code'] = 500;
$result['msg'] = '服务端 `cache` 目录不存在,尝试创建 `cache` 目录,但创建失败,请网站管理员在网站根目录手动创建 `cache` 目录';
exit(json_encode($result));
}
}

if(file_exists($TEMP_ROOT)){
$ass_file = file_get_contents($TEMP_ASS);

for($i=0;$i<count($data);$i++){
$str_source[$i] = '<?=['.$i.']=?>';
}

$change_ass = str_replace($str_source,$data,$ass_file);
$create_temporary_ass = fopen($CACHE_ASS_PATH, "w") or die('{"code":501,"msg":"临时字幕文件创建失败,请网站管理员检查 `cache` 目录是否具有读写权限或用户组设否设置正确!"}');
fwrite($create_temporary_ass, $change_ass) or die('{"code":502,"msg":"临时字幕文件已创建,但写入失败,请网站管理员检查 `cache` 目录是否具有读写权限或用户组设否设置正确!"}');
fclose($create_temporary_ass);

$out_put_file=ROOT.'/cache/'.$request_time.'.gif';
$command = 'ffmpeg -y -i '.$TEMP_VIDEO.' -vf "ass='.$CACHE_ASS_PATH.'" '.$out_put_file;
system($command);
unlink($CACHE_ASS_PATH);//删除临时生成的字幕文件

$result['code'] = 200;
$result['type'] = $type;
$result['msg'] = '应该生成成功...';

if(UPLOAD_TO_SOGOU_IMG === true && filesize($out_put_file)<10000000){
require_once ROOT.'/usr/lib/functions.php';
$r = upload_to_sogou($out_put_file);
if($r === false) {
//如果上传失败,则返回本地路径
$result['path'] = './cache/'.$request_time.'.gif';
$result['upload_status'] = 'fail';
}else{
$result['path'] = 'https://'.$r['host'][rand(0,4)].$r['path'];
$result['upload_status'] = 'success';
}
}else{
$result['path'] = './cache/'.$request_time.'.gif';
}

}else{
$result['code'] = 404;
$result['type'] = $type;
$result['msg'] = '该模板文件不存在!';
}
}else{
$result['code'] = 400;
$result['msg'] = '缺少必要参数,请检查!';
/**
* 判断根目录是否存在 cache 目录,不存在则创建
*/
if (! file_exists(ROOT . '/cache')) {
if (mkdir(ROOT . '/cache' ,0777) === false) {
$result['code'] = 500;
$result['msg'] = '服务端 `cache` 目录不存在,尝试创建 `cache` 目录,但创建失败,请网站管理员在网站根目录手动创建 `cache` 目录';
exit(json_encode($result));
}
}

if (file_exists($TEMP_ROOT)) {
$ass_file = file_get_contents($TEMP_ASS);

for ($i = 0; $i < count($data); $i++) {
$str_source[$i] = '<?=[' . $i . ']=?>';
}

$change_ass = str_replace($str_source ,$data ,$ass_file);
$create_temporary_ass = fopen($CACHE_ASS_PATH ,"w") or die('{"code":501,"msg":"临时字幕文件创建失败,请网站管理员检查 `cache` 目录是否具有读写权限或用户组设否设置正确!"}');
fwrite($create_temporary_ass ,$change_ass) or die('{"code":502,"msg":"临时字幕文件已创建,但写入失败,请网站管理员检查 `cache` 目录是否具有读写权限或用户组设否设置正确!"}');
fclose($create_temporary_ass);

$out_put_file = ROOT . '/cache/' . $request_time . '.gif';
$command = 'ffmpeg -y -i ' . $TEMP_VIDEO . ' -vf "ass=' . $CACHE_ASS_PATH . '" ' . $out_put_file;
system($command);
unlink($CACHE_ASS_PATH);//删除临时生成的字幕文件

$result['code'] = 200;
$result['type'] = $type;
$result['msg'] = '应该生成成功...';

if (UPLOAD_TO_SOGOU_IMG === true && filesize($out_put_file) < 10000000) {
require_once ROOT . '/usr/lib/functions.php';
$r = upload_to_sogou($out_put_file);
if ($r === false) {
//如果上传失败,则返回本地路径
$result['path'] = './cache/' . $request_time . '.gif';
$result['upload_status'] = 'fail';
} else {
$result['path'] = 'https://' . $r['host'][rand(0 ,4)] . $r['path'];
$result['upload_status'] = 'success';
}
} else {
$result['path'] = './cache/' . $request_time . '.gif';
}

} else {
$result['code'] = 404;
$result['type'] = $type;
$result['msg'] = '该模板文件不存在!';
}
} else {
$result['code'] = 400;
$result['msg'] = '缺少必要参数,请检查!';
}

echo json_encode($result);
Empty file modified cache/README.md
100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"guzzlehttp/guzzle": "^6.3"
}
}
Loading

0 comments on commit a158c54

Please sign in to comment.