mirror of https://github.com/helloxz/imgurl
update
parent
d2bca34d8d
commit
5fbe28d19d
|
@ -10,8 +10,9 @@ ImgURL是一个简单、纯粹的图床程序,使用PHP脚本开发,不需
|
|||
- [x] 一键生成链接
|
||||
- [x] 浏览与删除图片
|
||||
- [x] 图片压缩
|
||||
- [x] 图片鉴黄
|
||||
- [ ] 图片水印
|
||||
- [ ] 多图上传
|
||||
- [ ] 图片鉴黄
|
||||
- [ ] API上传
|
||||
|
||||
### 安装
|
||||
|
@ -24,5 +25,5 @@ Demo:[http://imgurl.org/](http://imgurl.org/)
|
|||

|
||||
|
||||
### 联系我
|
||||
* [https://www.xiaoz.me/](https://www.xiaoz.me/)
|
||||
* Blog:[https://www.xiaoz.me/](https://www.xiaoz.me/)
|
||||
* QQ:337003006
|
||||
|
|
|
@ -7,6 +7,14 @@
|
|||
<h1>ImgURL</h1>
|
||||
<p>ImgURL是一个简洁、纯粹的图床程序,使用PHP开发,不需要数据库支持,也没有复杂的配置。</p>
|
||||
<p>没有永久免费的午餐,也没用永久免费的图床,ImgURL无法保障永久存储您的图片,但有了ImgURL让图床多了一个选择,我坚信只有图片掌握在自己手中才更安全。</p>
|
||||
<h3>功能与特色</h3>
|
||||
<ul>
|
||||
<li>拽拖上传图片、实时预览</li>
|
||||
<li>一键生成链接,一键复制</li>
|
||||
<li>基本图片管理</li>
|
||||
<li>TinyPNG图片压缩</li>
|
||||
<li>图片智能鉴黄</li>
|
||||
</ul>
|
||||
<h3>安装</h3>
|
||||
<p>访问:<a href="https://github.com/helloxz/imgurl/archive/master.zip" target = "_blank" rel = "nofollow">master.zip</a>下载最新版ImgURL程序,放到您的站点根目录并解压。修改一下<code>config.php</code>设置你自己的域名和密码,访问<code>http(s)://domain.com/</code>即可,就是这么简单。</p>
|
||||
<h3>说明</h3>
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
/*
|
||||
@name:万象优图API处理接口
|
||||
@author:xiaoz.me
|
||||
*/
|
||||
error_reporting(E_ALL^E_NOTICE^E_WARNING^E_DEPRECATED);
|
||||
//载入配置
|
||||
include_once('../config.php');
|
||||
|
||||
//载入万象优图SDK
|
||||
require_once '../sdk/wxyt/index.php';
|
||||
use QcloudImage\CIClient;
|
||||
$client = new CIClient($identify['APP_ID'], $identify['SECRET_ID'], $identify['SECRET_KEY'], $identify['BUCKET']);
|
||||
$client->setTimeout(60);
|
||||
|
||||
//获取图片地址
|
||||
$url = $_GET['url'];
|
||||
//获取上级目录地址
|
||||
|
||||
//对URL进行替换
|
||||
$url = str_replace($config['domain'],'',$url);
|
||||
$imgdir = explode('/',$url);//对目录进行分割
|
||||
|
||||
//如果链接是管理员目录则不鉴黄
|
||||
if($imgdir[0] == $config['admindir']) {
|
||||
$re_data = array(
|
||||
"code" => 0,
|
||||
"result" => 0,
|
||||
"confidence"=> 0
|
||||
);
|
||||
|
||||
echo $re_data = json_encode($re_data);
|
||||
exit;
|
||||
}
|
||||
//如果不是游客目录
|
||||
if($config['userdir'] != $imgdir[0]) {
|
||||
//echo $imgdir[0];
|
||||
echo '非法请求';
|
||||
exit;
|
||||
}
|
||||
//重组完整图片
|
||||
$imgurl = $config['domain'].$url;
|
||||
$imginfo = ($client->pornDetect(array('urls'=>array($imgurl))));
|
||||
|
||||
$imginfo = json_decode($imginfo);
|
||||
|
||||
//获取状态码,0为成功
|
||||
//$code = $imginfo->http_code;
|
||||
//转换为数组
|
||||
$imginfo = object2array($imginfo);
|
||||
//状态码,0为成功
|
||||
$code = $imginfo['result_list']['0']->code;
|
||||
$imginfo = object2array($imginfo['result_list']['0']->data);
|
||||
//识别结果,0 正常,1 黄图,2 疑似图片
|
||||
$result = $imginfo['result'];
|
||||
//识别评分,分数越高,越可能是黄图
|
||||
$confidence = $imginfo['confidence'];
|
||||
|
||||
//重新返回json数据
|
||||
$re_data = array(
|
||||
"code" => $code,
|
||||
"result" => $result,
|
||||
"confidence"=> $confidence
|
||||
);
|
||||
|
||||
//严格模式,如果是色情图片或疑似色情图片均放到回收站
|
||||
if(($re_data['result'] == 1) || ($re_data['result'] == 2)) {
|
||||
//获取图片地址
|
||||
$url = dirname(dirname(__FILE__)).'/'.$url;
|
||||
//回收站地址
|
||||
$recycle = dirname(dirname(__FILE__))."/recycle/".end($imgdir);
|
||||
//移动到回收站
|
||||
if(copy($url,$recycle)){
|
||||
unlink($url); //删除图片
|
||||
}
|
||||
}
|
||||
|
||||
echo $re_data = json_encode($re_data);
|
||||
exit;
|
||||
?>
|
||||
|
||||
<?php
|
||||
//对象转数组
|
||||
function object2array($object) {
|
||||
if (is_object($object)) {
|
||||
foreach ($object as $key => $value) {
|
||||
$array[$key] = $value;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$array = $object;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
?>
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
/*
|
||||
@name:TinyPNG图片压缩接口
|
||||
@author:xiaoz.me
|
||||
*/
|
||||
|
||||
//载入SDK
|
||||
require_once("../sdk/tinypng/Tinify/Exception.php");
|
||||
require_once("../sdk/tinypng/Tinify/ResultMeta.php");
|
||||
require_once("../sdk/tinypng/Tinify/Result.php");
|
||||
require_once("../sdk/tinypng/Tinify/Source.php");
|
||||
require_once("../sdk/tinypng/Tinify/Client.php");
|
||||
require_once("../sdk/tinypng/Tinify.php");
|
||||
//载入配置文件
|
||||
require_once("../config.php");
|
||||
|
||||
//获取图片URL地址
|
||||
$imgurl = $_GET['url'];
|
||||
//获取URI
|
||||
$imguri = str_replace($config['domain'],"",$imgurl);
|
||||
//echo $imguri;
|
||||
//仅获取域名
|
||||
$domain = str_replace("http://","",$config['domain']);
|
||||
$domain = str_replace("https://","",$domain);
|
||||
$domain = explode("/",$domain); //最终的目的是取出域名
|
||||
|
||||
|
||||
//搜索域名是否匹配
|
||||
$sdomain = strpos($imgurl,$domain[0]);
|
||||
|
||||
if($sdomain == false) {
|
||||
echo '地址不合法';
|
||||
exit;
|
||||
}
|
||||
//合法的域名,那我们继续咯
|
||||
//在判断下目录是否合法
|
||||
$imgdir = explode("/",$imguri);
|
||||
//如果目录不是访客目录也不是管理员目录,那就不处理咯
|
||||
if(($imgdir[0] != $config['userdir']) && ($imgdir[0] != $config['admindir'])) {
|
||||
echo '地址不合法';
|
||||
exit;
|
||||
}
|
||||
//ok,上面都过了,判断下图片类型
|
||||
//使用exif_imagetype函数来判断文件类型
|
||||
$imguri = '../'.$imguri;
|
||||
$file_type = exif_imagetype($imguri);
|
||||
switch ( $file_type )
|
||||
{
|
||||
case IMAGETYPE_JPEG:
|
||||
tinypng($config['tinypng'],$config['domain'],$imguri,$imgurl);
|
||||
$redata = array("code" => 1,"type" => $file_type);
|
||||
echo json_encode($redata);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
tinypng($config['tinypng'],$config['domain'],$imguri,$imgurl);
|
||||
$redata = array("code" => 1,"type" => $file_type);
|
||||
echo json_encode($redata);
|
||||
break;
|
||||
default:
|
||||
$redata = array("code" => 0,"type" => $file_type);
|
||||
echo json_encode($redata);
|
||||
break;
|
||||
}
|
||||
?>
|
||||
|
||||
<?php
|
||||
//压缩图片
|
||||
function tinypng($api,$host,$imgfile,$imgurl){
|
||||
if($api == '') {
|
||||
echo '未开启TinyPNG';
|
||||
exit;
|
||||
}
|
||||
else{
|
||||
Tinify\setKey($api);
|
||||
Tinify\fromFile($imgfile)->toFile($imgfile);
|
||||
//获取主机名
|
||||
$host = $host."/api/";
|
||||
//对压缩后的图片鉴黄
|
||||
$ch = curl_init($host."identify.php?url=".$imgurl) ;
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证>证书和hosts
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
|
||||
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
|
||||
$output = curl_exec($ch) ;
|
||||
curl_close($ch);
|
||||
|
||||
return $imgfile;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -8,4 +8,12 @@
|
|||
'password' => 'xiaoz.me', //管理员密码
|
||||
'tinypng' => '' //使用TinyPNG压缩图片,填写TinyPNG KEY,为空则不启用压缩
|
||||
);
|
||||
//是否启用腾讯万象优图鉴黄识别
|
||||
$identify = array(
|
||||
'eroticism' => false, //如果此项为true则下面必须填写,请参考帮助文档。
|
||||
'APP_ID' => '',
|
||||
'SECRET_ID' => '',
|
||||
'SECRET_KEY' => '',
|
||||
'BUCKET' => ''
|
||||
)
|
||||
?>
|
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
|
@ -59,18 +59,36 @@
|
|||
function delete($imgname,$userdir,$admindir) {
|
||||
//字符串分割
|
||||
$imgdir = explode("/",$imgname);
|
||||
//只允许删除用户目录和管理员目录
|
||||
if(($imgdir[0] == $userdir) || ($imgdir[0] == $admindir)){
|
||||
if(unlink($imgname)) {
|
||||
echo 'ok'; //删除图片成功
|
||||
}
|
||||
else {
|
||||
echo '删除失败,可能是图片不存在。';
|
||||
}
|
||||
}
|
||||
else {
|
||||
echo '非法操作';
|
||||
exit;
|
||||
//只允许删除用户目录/管理员目录/回收站目录
|
||||
switch ( $imgdir[0] )
|
||||
{
|
||||
case $userdir:
|
||||
if(unlink($imgname)) {
|
||||
echo 'ok'; //删除图片成功
|
||||
}
|
||||
else {
|
||||
echo '删除失败,可能是图片不存在。';
|
||||
}
|
||||
break;
|
||||
case $admindir:
|
||||
if(unlink($imgname)) {
|
||||
echo 'ok'; //删除图片成功
|
||||
}
|
||||
else {
|
||||
echo '删除失败,可能是图片不存在。';
|
||||
}
|
||||
break;
|
||||
case 'recycle':
|
||||
if(unlink($imgname)) {
|
||||
echo 'ok'; //删除图片成功
|
||||
}
|
||||
else {
|
||||
echo '删除失败,可能是图片不存在。';
|
||||
}
|
||||
break;
|
||||
default:
|
||||
echo '非法操作';
|
||||
break;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -8,6 +8,8 @@
|
|||
<meta name="keywords" content="ImgURL,免费图床,图床程序,小z图床,XZ Pic" />
|
||||
<meta name="description" content="ImgURL是一个简单、纯粹的图床程序,让个人图床多一个选择。" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
|
||||
<link rel="Bookmark" href="favicon.ico" />
|
||||
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="./static/uploadfile.css" rel="stylesheet">
|
||||
<link href="./static/style.css" rel="stylesheet">
|
||||
|
@ -51,8 +53,8 @@
|
|||
if(isset($_COOKIE['uid'])) {
|
||||
include_once('./config.php');
|
||||
$mydir = $config['admindir'];
|
||||
echo "<li><a href='./recycle.php'>回收站</a></li>";
|
||||
echo "<li><a href='./explore.php?dir=$mydir'>管理员</a></li>";
|
||||
echo " | ";
|
||||
echo "<li><a href='./functions.php?type=logout'>退出</a></li>";
|
||||
}
|
||||
else {
|
||||
|
|
28
index.php
28
index.php
|
@ -1,7 +1,35 @@
|
|||
<?php
|
||||
//载入header
|
||||
include_once('./header.php');
|
||||
include_once('./config.php');
|
||||
?>
|
||||
<!--是否启用鉴黄-->
|
||||
<?php
|
||||
//如果启用鉴黄
|
||||
if($identify['eroticism'] == true) {
|
||||
$eroticism = 1;
|
||||
}
|
||||
else{
|
||||
$eroticism = 0;
|
||||
}
|
||||
//判断启用图片压缩
|
||||
switch ( $config['tinypng'] )
|
||||
{
|
||||
//为空,没启用
|
||||
case '':
|
||||
$tinypng = 0;
|
||||
break;
|
||||
default:
|
||||
$tinypng = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
?>
|
||||
<div id="eroticism" style = "display: none;"><?php echo $eroticism; ?></div>
|
||||
<!--是否启用鉴黄END-->
|
||||
<!--是否启用图片压缩-->
|
||||
<div id="tinypng" style = "display: none;"><?php echo $tinypng; ?></div>
|
||||
<!--是否启用图片压缩END-->
|
||||
<div style = "clear:both;"></div>
|
||||
<div class="container" style = "margin-bottom:40px;">
|
||||
<div class="row">
|
||||
|
|
2
pro.php
2
pro.php
|
@ -6,7 +6,7 @@
|
|||
<div class="col-lg-10 col-md-offset-1">
|
||||
<h1>捐赠版</h1>
|
||||
<p>ImgURL普通版和捐赠版功能上没有任何区别,不过您可以请作者喝一杯咖啡或吃一顿午餐即可获得捐赠版。可扫描下方二维码获取,留言请填写自己的网址。</p>
|
||||
<p><img src="https://cdn.xiaoz.me/wp-content/uploads/2013/12/juanzeng260.png" alt="" width="260" height="309" class="alignnone size-full wp-image-9144" /></p>
|
||||
<p><img src="https://imgurl.org/upload/1712/cb349aa4a1b95997.png" alt="" width="260" height="309" class="alignnone size-full wp-image-9144" /></p>
|
||||
<h3>技术支持</h3>
|
||||
<p>获得捐赠版的童鞋,可以去除底部版权,可提供首次安装及调试。(但不提供代码再次开发)</p>
|
||||
<ul>
|
||||
|
|
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
error_reporting(E_ALL^E_NOTICE^E_WARNING^E_DEPRECATED);
|
||||
//载入配置
|
||||
include_once('./config.php');
|
||||
//载入header
|
||||
include_once('./header.php');
|
||||
//权限判断
|
||||
$id = md5($config['username'].$config['password']);
|
||||
$uid = $_COOKIE['uid'];
|
||||
if($id != $uid) {
|
||||
echo "<h3 class = 'text-center'>权限不足</h3>";
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<div class="container" style = "margin-top:40px;">
|
||||
<div class="row">
|
||||
<div class="col-lg-10 col-md-offset-1">
|
||||
<!--图片预览-->
|
||||
<div class="col-lg-6">
|
||||
<img id = "viewid" src="./static/view.jpg" class="img-thumbnail img-responsive">
|
||||
</div>
|
||||
<!--图片预览END-->
|
||||
<div class="col-lg-6">
|
||||
<table class="table table-striped">
|
||||
<tbody>
|
||||
<?php
|
||||
function get_files($dir) {
|
||||
$files = array();
|
||||
|
||||
for (; $dir->valid(); $dir->next()) {
|
||||
if ($dir->isDir() && !$dir->isDot()) {
|
||||
if ($dir->haschildren()) {
|
||||
$files = array_merge($files, get_files($dir->getChildren()));
|
||||
};
|
||||
}else if($dir->isFile()){
|
||||
$files[] = $dir->getPathName();
|
||||
}
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
$path = 'recycle';
|
||||
$dir = new RecursiveDirectoryIterator($path);
|
||||
$fname = get_files($dir);
|
||||
$num = count($fname) - 1;
|
||||
|
||||
for($i = 0;$i <= $num;$i++) {
|
||||
$fname[$i] = str_replace("\\","/",$fname[$i]);
|
||||
//如果文件是空的,则终止循环
|
||||
?>
|
||||
<tr id = "row<?php echo $i; ?>">
|
||||
<td onmouseover = "return view('<?php echo $config['domain'].$fname[$i] ?>');">
|
||||
<?php
|
||||
echo "<a href = "."'".$config['domain'].$fname[$i]."' target = '_blank'>"."$fname[$i]</a>";
|
||||
?>
|
||||
</td>
|
||||
<td>
|
||||
<?php
|
||||
if(isset($_COOKIE['uid'])) {
|
||||
echo "<a href = \"javascript:;\" onclick = \"del('$fname[$i]',$i);\">删除</a>";
|
||||
}
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
function view(imgurl) {
|
||||
$("#viewid").src;
|
||||
$("#viewid").attr('src',imgurl);
|
||||
}
|
||||
//删除图片
|
||||
function del(filedir,rowid) {
|
||||
//行id
|
||||
var rowid = 'row' + rowid;
|
||||
//确认删除?
|
||||
var msg = "确认删除?";
|
||||
if (confirm(msg)==true){
|
||||
$.get("./functions.php?type=delete&dir="+filedir,function(data,status){
|
||||
//删除成功
|
||||
if(data == 'ok') {
|
||||
$("#"+rowid).remove();
|
||||
}
|
||||
else{
|
||||
alert(data); //删除失败,弹出报错
|
||||
}
|
||||
});
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<?php
|
||||
//载入页脚
|
||||
include_once('./footer.php');
|
||||
?>
|
|
@ -0,0 +1,5 @@
|
|||
<IfModule mod_rewrite.c>
|
||||
RewriteEngine On
|
||||
order allow,deny
|
||||
deny from all
|
||||
</IfModule>
|
|
@ -0,0 +1,7 @@
|
|||
User-Agent: *
|
||||
Disallow: /recycle/
|
||||
Disallow: /sdk/
|
||||
Disallow: /api/
|
||||
Disallow: /temp/
|
||||
Disallow: /upload/
|
||||
Disallow: /static/
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
const VERSION = "1.5.2";
|
||||
|
||||
class Tinify {
|
||||
private static $key = NULL;
|
||||
private static $appIdentifier = NULL;
|
||||
private static $proxy = NULL;
|
||||
|
||||
private static $compressionCount = NULL;
|
||||
private static $client = NULL;
|
||||
|
||||
public static function setKey($key) {
|
||||
self::$key = $key;
|
||||
self::$client = NULL;
|
||||
}
|
||||
|
||||
public static function setAppIdentifier($appIdentifier) {
|
||||
self::$appIdentifier = $appIdentifier;
|
||||
self::$client = NULL;
|
||||
}
|
||||
|
||||
public static function setProxy($proxy) {
|
||||
self::$proxy = $proxy;
|
||||
self::$client = NULL;
|
||||
}
|
||||
|
||||
public static function getCompressionCount() {
|
||||
return self::$compressionCount;
|
||||
}
|
||||
|
||||
public static function setCompressionCount($compressionCount) {
|
||||
self::$compressionCount = $compressionCount;
|
||||
}
|
||||
|
||||
public static function getClient() {
|
||||
if (!self::$key) {
|
||||
throw new AccountException("Provide an API key with Tinify\setKey(...)");
|
||||
}
|
||||
|
||||
if (!self::$client) {
|
||||
self::$client = new Client(self::$key, self::$appIdentifier, self::$proxy);
|
||||
}
|
||||
|
||||
return self::$client;
|
||||
}
|
||||
|
||||
public static function setClient($client) {
|
||||
self::$client = $client;
|
||||
}
|
||||
}
|
||||
|
||||
function setKey($key) {
|
||||
return Tinify::setKey($key);
|
||||
}
|
||||
|
||||
function setAppIdentifier($appIdentifier) {
|
||||
return Tinify::setAppIdentifier($appIdentifier);
|
||||
}
|
||||
|
||||
function setProxy($proxy) {
|
||||
return Tinify::setProxy($proxy);
|
||||
}
|
||||
|
||||
function getCompressionCount() {
|
||||
return Tinify::getCompressionCount();
|
||||
}
|
||||
|
||||
function compressionCount() {
|
||||
return Tinify::getCompressionCount();
|
||||
}
|
||||
|
||||
function fromFile($path) {
|
||||
return Source::fromFile($path);
|
||||
}
|
||||
|
||||
function fromBuffer($string) {
|
||||
return Source::fromBuffer($string);
|
||||
}
|
||||
|
||||
function fromUrl($string) {
|
||||
return Source::fromUrl($string);
|
||||
}
|
||||
|
||||
function validate() {
|
||||
try {
|
||||
Tinify::getClient()->request("post", "/shrink");
|
||||
} catch (AccountException $err) {
|
||||
if ($err->status == 429) return true;
|
||||
throw $err;
|
||||
} catch (ClientException $err) {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,160 @@
|
|||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
class Client {
|
||||
const API_ENDPOINT = "https://api.tinify.com";
|
||||
|
||||
const RETRY_COUNT = 1;
|
||||
const RETRY_DELAY = 500;
|
||||
|
||||
private $options;
|
||||
|
||||
public static function userAgent() {
|
||||
$curl = curl_version();
|
||||
return "Tinify/" . VERSION . " PHP/" . PHP_VERSION . " curl/" . $curl["version"];
|
||||
}
|
||||
|
||||
private static function caBundle() {
|
||||
return __DIR__ . "/../data/cacert.pem";
|
||||
}
|
||||
|
||||
function __construct($key, $app_identifier = NULL, $proxy = NULL) {
|
||||
$curl = curl_version();
|
||||
|
||||
if (!($curl["features"] & CURL_VERSION_SSL)) {
|
||||
throw new ClientException("Your curl version does not support secure connections");
|
||||
}
|
||||
|
||||
if ($curl["version_number"] < 0x071201) {
|
||||
$version = $curl["version"];
|
||||
throw new ClientException("Your curl version ${version} is outdated; please upgrade to 7.18.1 or higher");
|
||||
}
|
||||
|
||||
$this->options = array(
|
||||
CURLOPT_BINARYTRANSFER => true,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HEADER => true,
|
||||
CURLOPT_USERPWD => "api:" . $key,
|
||||
CURLOPT_CAINFO => self::caBundle(),
|
||||
CURLOPT_SSL_VERIFYPEER => true,
|
||||
CURLOPT_USERAGENT => join(" ", array_filter(array(self::userAgent(), $app_identifier))),
|
||||
);
|
||||
|
||||
if ($proxy) {
|
||||
$parts = parse_url($proxy);
|
||||
if (isset($parts["host"])) {
|
||||
$this->options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
|
||||
$this->options[CURLOPT_PROXY] = $parts["host"];
|
||||
} else {
|
||||
throw new ConnectionException("Invalid proxy");
|
||||
}
|
||||
|
||||
if (isset($parts["port"])) {
|
||||
$this->options[CURLOPT_PROXYPORT] = $parts["port"];
|
||||
}
|
||||
|
||||
$creds = "";
|
||||
if (isset($parts["user"])) $creds .= $parts["user"];
|
||||
if (isset($parts["pass"])) $creds .= ":" . $parts["pass"];
|
||||
|
||||
if ($creds) {
|
||||
$this->options[CURLOPT_PROXYAUTH] = CURLAUTH_ANY;
|
||||
$this->options[CURLOPT_PROXYUSERPWD] = $creds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function request($method, $url, $body = NULL) {
|
||||
$header = array();
|
||||
if (is_array($body)) {
|
||||
if (!empty($body)) {
|
||||
$body = json_encode($body);
|
||||
array_push($header, "Content-Type: application/json");
|
||||
} else {
|
||||
$body = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
for ($retries = self::RETRY_COUNT; $retries >= 0; $retries--) {
|
||||
if ($retries < self::RETRY_COUNT) {
|
||||
usleep(self::RETRY_DELAY * 1000);
|
||||
}
|
||||
|
||||
$request = curl_init();
|
||||
if ($request === false || $request === null) {
|
||||
throw new ConnectionException(
|
||||
"Error while connecting: curl extension is not functional or disabled."
|
||||
);
|
||||
}
|
||||
|
||||
curl_setopt_array($request, $this->options);
|
||||
|
||||
$url = strtolower(substr($url, 0, 6)) == "https:" ? $url : self::API_ENDPOINT . $url;
|
||||
curl_setopt($request, CURLOPT_URL, $url);
|
||||
curl_setopt($request, CURLOPT_CUSTOMREQUEST, strtoupper($method));
|
||||
|
||||
if (count($header) > 0) {
|
||||
curl_setopt($request, CURLOPT_HTTPHEADER, $header);
|
||||
}
|
||||
|
||||
if ($body) {
|
||||
curl_setopt($request, CURLOPT_POSTFIELDS, $body);
|
||||
}
|
||||
|
||||
$response = curl_exec($request);
|
||||
|
||||
if (is_string($response)) {
|
||||
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
|
||||
$headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE);
|
||||
curl_close($request);
|
||||
|
||||
$headers = self::parseHeaders(substr($response, 0, $headerSize));
|
||||
$body = substr($response, $headerSize);
|
||||
|
||||
if (isset($headers["compression-count"])) {
|
||||
Tinify::setCompressionCount(intval($headers["compression-count"]));
|
||||
}
|
||||
|
||||
if ($status >= 200 && $status <= 299) {
|
||||
return (object) array("body" => $body, "headers" => $headers);
|
||||
}
|
||||
|
||||
$details = json_decode($body);
|
||||
if (!$details) {
|
||||
$message = sprintf("Error while parsing response: %s (#%d)",
|
||||
PHP_VERSION_ID >= 50500 ? json_last_error_msg() : "Error",
|
||||
json_last_error());
|
||||
$details = (object) array(
|
||||
"message" => $message,
|
||||
"error" => "ParseError"
|
||||
);
|
||||
}
|
||||
|
||||
if ($retries > 0 && $status >= 500) continue;
|
||||
throw Exception::create($details->message, $details->error, $status);
|
||||
} else {
|
||||
$message = sprintf("%s (#%d)", curl_error($request), curl_errno($request));
|
||||
curl_close($request);
|
||||
if ($retries > 0) continue;
|
||||
throw new ConnectionException("Error while connecting: " . $message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static function parseHeaders($headers) {
|
||||
if (!is_array($headers)) {
|
||||
$headers = explode("\r\n", $headers);
|
||||
}
|
||||
|
||||
$res = array();
|
||||
foreach ($headers as $header) {
|
||||
if (empty($header)) continue;
|
||||
$split = explode(":", $header, 2);
|
||||
if (count($split) === 2) {
|
||||
$res[strtolower($split[0])] = trim($split[1]);
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
class Exception extends \Exception {
|
||||
public $status;
|
||||
|
||||
public static function create($message, $type, $status) {
|
||||
if ($status == 401 || $status == 429) {
|
||||
$klass = "Tinify\AccountException";
|
||||
} else if($status >= 400 && $status <= 499) {
|
||||
$klass = "Tinify\ClientException";
|
||||
} else if($status >= 500 && $status <= 599) {
|
||||
$klass = "Tinify\ServerException";
|
||||
} else {
|
||||
$klass = "Tinify\Exception";
|
||||
}
|
||||
|
||||
if (empty($message)) $message = "No message was provided";
|
||||
return new $klass($message, $type, $status);
|
||||
}
|
||||
|
||||
function __construct($message, $type = NULL, $status = NULL) {
|
||||
$this->status = $status;
|
||||
if ($status) {
|
||||
parent::__construct($message . " (HTTP " . $status . "/" . $type . ")");
|
||||
} else {
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class AccountException extends Exception {}
|
||||
class ClientException extends Exception {}
|
||||
class ServerException extends Exception {}
|
||||
class ConnectionException extends Exception {}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
class Result extends ResultMeta {
|
||||
protected $data;
|
||||
|
||||
public function __construct($meta, $data) {
|
||||
$this->meta = $meta;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function data() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function toBuffer() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function toFile($path) {
|
||||
return file_put_contents($path, $this->toBuffer());
|
||||
}
|
||||
|
||||
public function size() {
|
||||
return intval($this->meta["content-length"]);
|
||||
}
|
||||
|
||||
public function mediaType() {
|
||||
return $this->meta["content-type"];
|
||||
}
|
||||
|
||||
public function contentType() {
|
||||
return $this->mediaType();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
class ResultMeta {
|
||||
protected $meta;
|
||||
|
||||
public function __construct($meta) {
|
||||
$this->meta = $meta;
|
||||
}
|
||||
|
||||
public function width() {
|
||||
return intval($this->meta["image-width"]);
|
||||
}
|
||||
|
||||
public function height() {
|
||||
return intval($this->meta["image-height"]);
|
||||
}
|
||||
|
||||
public function location() {
|
||||
return isset($this->meta["location"]) ? $this->meta["location"] : null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
class Source {
|
||||
private $url, $commands;
|
||||
|
||||
public static function fromFile($path) {
|
||||
return self::fromBuffer(file_get_contents($path));
|
||||
}
|
||||
|
||||
public static function fromBuffer($string) {
|
||||
$response = Tinify::getClient()->request("post", "/shrink", $string);
|
||||
return new self($response->headers["location"]);
|
||||
}
|
||||
|
||||
public static function fromUrl($url) {
|
||||
$body = array("source" => array("url" => $url));
|
||||
$response = Tinify::getClient()->request("post", "/shrink", $body);
|
||||
return new self($response->headers["location"]);
|
||||
}
|
||||
|
||||
public function __construct($url, $commands = array()) {
|
||||
$this->url = $url;
|
||||
$this->commands = $commands;
|
||||
}
|
||||
|
||||
public function preserve() {
|
||||
$options = $this->flatten(func_get_args());
|
||||
$commands = array_merge($this->commands, array("preserve" => $options));
|
||||
return new self($this->url, $commands);
|
||||
}
|
||||
|
||||
public function resize($options) {
|
||||
$commands = array_merge($this->commands, array("resize" => $options));
|
||||
return new self($this->url, $commands);
|
||||
}
|
||||
|
||||
public function store($options) {
|
||||
$response = Tinify::getClient()->request("post", $this->url,
|
||||
array_merge($this->commands, array("store" => $options)));
|
||||
return new Result($response->headers, $response->body);
|
||||
}
|
||||
|
||||
public function result() {
|
||||
$response = Tinify::getClient()->request("get", $this->url, $this->commands);
|
||||
return new Result($response->headers, $response->body);
|
||||
}
|
||||
|
||||
public function toFile($path) {
|
||||
return $this->result()->toFile($path);
|
||||
}
|
||||
|
||||
public function toBuffer() {
|
||||
return $this->result()->toBuffer();
|
||||
}
|
||||
|
||||
private static function flatten($options) {
|
||||
$flattened = array();
|
||||
foreach ($options as $option) {
|
||||
if (is_array($option)) {
|
||||
$flattened = array_merge($flattened, $option);
|
||||
} else {
|
||||
array_push($flattened, $option);
|
||||
}
|
||||
}
|
||||
return $flattened;
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
/**
|
||||
* Signature create related functions.
|
||||
*/
|
||||
namespace QcloudImage;
|
||||
|
||||
/**
|
||||
* Auth class for creating reusable signature.
|
||||
*/
|
||||
class Auth {
|
||||
|
||||
public function __construct($appId, $secretId, $secretKey) {
|
||||
$this->appId = $appId;
|
||||
$this->secretId = $secretId;
|
||||
$this->secretKey = $secretKey;
|
||||
}
|
||||
/**
|
||||
* Return the appId
|
||||
*/
|
||||
public function getAppId() {
|
||||
return $this->appId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create reusable signature.
|
||||
* This signature will expire at time()+$howlong timestamp.
|
||||
* Return the signature on success.
|
||||
* Return false on fail.
|
||||
*/
|
||||
public function getSign($bucket, $howlong = 30) {
|
||||
if ($howlong <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$now = time();
|
||||
$expiration = $now + $howlong;
|
||||
$random = rand();
|
||||
|
||||
$plainText = "a=".$this->appId."&b=$bucket&k=".$this->secretId."&e=$expiration&t=$now&r=$random&f=";
|
||||
$bin = hash_hmac('SHA1', $plainText, $this->secretKey, true);
|
||||
|
||||
return base64_encode($bin.$plainText);
|
||||
}
|
||||
|
||||
private $appId = "";
|
||||
private $secretId = "";
|
||||
private $secretKey = "";
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
/**
|
||||
* Some settings for SDK.
|
||||
*/
|
||||
namespace QcloudImage;
|
||||
|
||||
/**
|
||||
* Conf class.
|
||||
*/
|
||||
class Conf {
|
||||
private static $VERSION = '1.0.0';
|
||||
private static $SERVER_ADDR = 'service.image.myqcloud.com';
|
||||
private static $HEADER_HOST = 'service.image.myqcloud.com';
|
||||
private $REQ_TIMEOUT = 60;
|
||||
private $SCHEME = 'http';
|
||||
|
||||
public function useHttp() {
|
||||
$this->SCHEME = 'http';
|
||||
}
|
||||
public function useHttps() {
|
||||
$this->SCHEME = 'https';
|
||||
}
|
||||
public function setTimeout($timeout) {
|
||||
if ($timeout > 0) {
|
||||
$this->REQ_TIMEOUT = $timeout;
|
||||
}
|
||||
}
|
||||
public function timeout() {
|
||||
return $this->REQ_TIMEOUT;
|
||||
}
|
||||
public function host() {
|
||||
return self::$HEADER_HOST;
|
||||
}
|
||||
|
||||
public function buildUrl($uri) {
|
||||
return $this->SCHEME.'://'.self::$SERVER_ADDR.'/'.ltrim($uri, "/");
|
||||
}
|
||||
|
||||
public static function getUa($appid = null) {
|
||||
$ua = 'CIPhpSDK/'.self::$VERSION.' ('.php_uname().')';
|
||||
if ($appid) {
|
||||
$ua .= " User($appid)";
|
||||
}
|
||||
return $ua;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
/**
|
||||
* Error defination.
|
||||
*/
|
||||
namespace QcloudImage;
|
||||
|
||||
class Error {
|
||||
|
||||
/**
|
||||
* Create reusable signature.
|
||||
* This signature will expire at time()+$howlong timestamp.
|
||||
* Return the signature on success.
|
||||
* Return false on fail.
|
||||
*/
|
||||
public static function json($code, $message, $httpcode = 0) {
|
||||
return json_encode(array(
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
'httpcode' => $httpcode,
|
||||
'data' => json_decode('{}',true)
|
||||
));
|
||||
}
|
||||
|
||||
public static $Param = -1;
|
||||
public static $Network = -2;
|
||||
public static $FilePath = -3;
|
||||
public static $Unknown = -4;
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
/**
|
||||
* Http Client use curl.
|
||||
*/
|
||||
namespace QcloudImage;
|
||||
|
||||
function my_curl_reset($handler) {
|
||||
curl_setopt($handler, CURLOPT_URL, '');
|
||||
curl_setopt($handler, CURLOPT_HTTPHEADER, array());
|
||||
curl_setopt($handler, CURLOPT_POSTFIELDS, array());
|
||||
curl_setopt($handler, CURLOPT_TIMEOUT, 0);
|
||||
curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
}
|
||||
|
||||
class HttpClient {
|
||||
|
||||
public function __destory() {
|
||||
if ($this->curlHandler) {
|
||||
curl_close($this->curlHandler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* send http request
|
||||
* @param array $request http请求信息
|
||||
* url : 请求的url地址
|
||||
* method : 请求方法,'get', 'post', 'put', 'delete', 'head'
|
||||
* data : 请求数据,如有设置,则method为post
|
||||
* header : 需要设置的http头部
|
||||
* host : 请求头部host
|
||||
* timeout : 请求超时时间
|
||||
* cert : ca文件路径
|
||||
* ssl_version: SSL版本号
|
||||
* @return string http请求响应
|
||||
*/
|
||||
public function sendRequest($request) {
|
||||
if (!is_array($request) || !isset($request["url"])) {
|
||||
return false;
|
||||
}
|
||||
if ($this->curlHandler) {
|
||||
if (function_exists('curl_reset')) {
|
||||
curl_reset($this->curlHandler);
|
||||
} else {
|
||||
my_curl_reset($this->curlHandler);
|
||||
}
|
||||
} else {
|
||||
$this->curlHandler = curl_init();
|
||||
}
|
||||
|
||||
curl_setopt($this->curlHandler, CURLOPT_URL, $request['url']);
|
||||
|
||||
$method = 'GET';
|
||||
if (isset($request['method']) &&
|
||||
in_array(strtolower($request['method']), array('get', 'post', 'put', 'delete', 'head'))) {
|
||||
$method = strtoupper($request['method']);
|
||||
} else if (isset($request['data'])) {
|
||||
$method = 'POST';
|
||||
}
|
||||
|
||||
$header = isset($request['header']) ? $request['header'] : array();
|
||||
$header[] = 'Method:'.$method;
|
||||
$header[] = 'Connection: keep-alive';
|
||||
if ('POST' == $method) {
|
||||
$header[] = 'Expect: ';
|
||||
}
|
||||
|
||||
isset($request['host']) && $header[] = 'Host:' . $request['host'];
|
||||
curl_setopt($this->curlHandler, CURLOPT_HTTPHEADER, $header);
|
||||
curl_setopt($this->curlHandler, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($this->curlHandler, CURLOPT_CUSTOMREQUEST, $method);
|
||||
isset($request['timeout']) && curl_setopt($this->curlHandler, CURLOPT_TIMEOUT, $request['timeout']);
|
||||
isset($request['data']) && in_array($method, array('POST', 'PUT')) &&
|
||||
curl_setopt($this->curlHandler, CURLOPT_POSTFIELDS, $request['data']);
|
||||
$ssl = substr($request['url'], 0, 8) == "https://" ? true : false;
|
||||
if (isset($request['cert'])) {
|
||||
curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYPEER,true);
|
||||
curl_setopt($this->curlHandler, CURLOPT_CAINFO, $request['cert']);
|
||||
curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYHOST,2);
|
||||
if (isset($request['ssl_version'])) {
|
||||
curl_setopt($this->curlHandler, CURLOPT_SSLVERSION, $request['ssl_version']);
|
||||
} else {
|
||||
curl_setopt($this->curlHandler, CURLOPT_SSLVERSION, 4);
|
||||
}
|
||||
} else if ($ssl) {
|
||||
curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYPEER,false); //true any ca
|
||||
curl_setopt($this->curlHandler, CURLOPT_SSL_VERIFYHOST,1); //check only host
|
||||
if (isset($request['ssl_version'])) {
|
||||
curl_setopt($this->curlHandler, CURLOPT_SSLVERSION, $request['ssl_version']);
|
||||
} else {
|
||||
curl_setopt($this->curlHandler, CURLOPT_SSLVERSION, 4);
|
||||
}
|
||||
}
|
||||
$ret = curl_exec($this->curlHandler);
|
||||
$this->httpInfo = curl_getinfo($this->curlHandler);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function statusCode() {
|
||||
if ($this->httpInfo) {
|
||||
return $this->httpInfo['http_code'];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private $httpInfo;
|
||||
private $curlHandler;
|
||||
}
|
|
@ -0,0 +1,224 @@
|
|||
# tencentyun/image-php-sdk-v2.0
|
||||
腾讯云 [万象优图(Cloud Image)](https://www.qcloud.com/product/ci) SDK for PHP
|
||||
|
||||
## 安装(直接下载源码集成)
|
||||
### 直接下载源码集成
|
||||
从github下载源码,并加载image-php-sdk-v2.0/index.php就可以了。
|
||||
调用请参考sample.php
|
||||
|
||||
### 1. 在腾讯云申请业务的授权
|
||||
授权包括:
|
||||
|
||||
APP_ID
|
||||
SECRET_ID
|
||||
SECRET_KEY
|
||||
BUCKET
|
||||
|
||||
### 2. 创建对应操作类的对象
|
||||
如果要使用图片,需要创建图片操作类对象
|
||||
|
||||
require_once __DIR__ . '/index.php';
|
||||
use QcloudImage\CIClient;
|
||||
|
||||
$client = new CIClient('APP_ID', 'SECRET_ID', 'SECRET_KEY', 'BUCKET');
|
||||
$client->setTimeout(30);
|
||||
|
||||
### 3. 调用对应的方法
|
||||
在创建完对象后,根据实际需求,调用对应的操作方法就可以了。sdk提供的方法包括:图片识别、人脸识别及人脸核身等。
|
||||
|
||||
#### 3.1 图片识别
|
||||
图片识别包括:图片鉴黄、图片标签、OCR-身份证识别及OCR-名片识别。
|
||||
|
||||
##### 图片鉴黄
|
||||
|
||||
```php
|
||||
//单个或多个图片Url
|
||||
var_dump ($client->pornDetect(array('urls'=>array('YOUR URL A',
|
||||
'YOUR URL B'))));
|
||||
//单个或多个图片File
|
||||
var_dump ($client->pornDetect(array('files'=>array('F:\pic\你好.jpg','G:\pic\test2.jpg'))));
|
||||
```
|
||||
|
||||
##### 图片标签
|
||||
|
||||
```php
|
||||
//单个图片url
|
||||
var_dump ($client->tagDetect(array('url'=>'YOUR URL')));
|
||||
//单个图片file
|
||||
var_dump ($client->tagDetect(array('file'=>'G:\pic\hot1.jpg')));
|
||||
//单个图片内容
|
||||
var_dump ($client->tagDetect(array('buffer'=>file_get_contents('G:\pic\hot1.jpg'))));
|
||||
```
|
||||
|
||||
##### OCR-身份证识别
|
||||
|
||||
```php
|
||||
//单个或多个图片Url,识别身份证正面
|
||||
var_dump ($client->idcardDetect(array('urls'=>array('YOUR URL A',
|
||||
'YOUR URL B')), 0));
|
||||
//单个或多个图片file,识别身份证正面
|
||||
var_dump ($client->idcardDetect(array('files'=>array('F:\pic\id6_zheng.jpg', 'F:\pic\id2_zheng.jpg')), 0));
|
||||
//单个或多个图片内容,识别身份证正面
|
||||
var_dump ($client->idcardDetect(array('buffers'=>array(file_get_contents('F:\pic\id6_zheng.jpg'),
|
||||
file_get_contents('F:\pic\id2_zheng.jpg'))), 0));
|
||||
|
||||
//单个或多个图片Url,识别身份证反面
|
||||
var_dump ($client->idcardDetect(array('urls'=>array('YOUR URL C',
|
||||
'YOUR URL D')), 1));
|
||||
//单个或多个图片file,识别身份证反面
|
||||
var_dump ($client->idcardDetect(array('files'=>array('F:\pic\id5_fan.jpg', 'F:\pic\id7_fan.png')), 1));
|
||||
//单个或多个图片内容,识别身份证反面
|
||||
var_dump ($client->idcardDetect(array('buffers'=>array(file_get_contents('F:\pic\id5_fan.jpg'),
|
||||
file_get_contents('F:\pic\id7_fan.jpg'))), 1));
|
||||
```
|
||||
|
||||
##### OCR-名片识别
|
||||
```php
|
||||
//单个或多个图片Url
|
||||
var_dump ($client->namecardDetect(array('urls'=>array('YOUR URL A',
|
||||
'YOUR URL B')), 0));
|
||||
//单个或多个图片file,
|
||||
var_dump ($client->namecardDetect(array('files'=>array('F:\pic\r.jpg', 'F:\pic\name2.jpg')), 1));
|
||||
//单个或多个图片内容
|
||||
var_dump ($client->namecardDetect(array('buffers'=>array(file_get_contents('F:\pic\name1.jpg'),
|
||||
file_get_contents('F:\pic\name2.jpg'))), 0));
|
||||
```
|
||||
|
||||
#### 3.2 人脸识别
|
||||
人脸识别包括:人脸检测、五官定位、个体信息管理、人脸验证、人脸对比及人脸检索。
|
||||
|
||||
#### 人脸检测
|
||||
|
||||
```php
|
||||
//单个图片Url, mode:1为检测最大的人脸 , 0为检测所有人脸
|
||||
var_dump ($client->faceDetect(array('url'=>'YOUR URL'), 1));
|
||||
//单个图片file,mode:1为检测最大的人脸 , 0为检测所有人脸
|
||||
var_dump ($client->faceDetect(array('file'=>'F:\pic\face1.jpg'),0));
|
||||
//单个图片内容,mode:1为检测最大的人脸 , 0为检测所有人脸
|
||||
var_dump ($client->faceDetect(array('buffer'=>file_get_contents('F:\pic\face1.jpg')), 1));
|
||||
```
|
||||
|
||||
##### 五官定位
|
||||
|
||||
```php
|
||||
//单个图片Url,mode:1为检测最大的人脸 , 0为检测所有人脸
|
||||
var_dump ($client->faceShape(array('url'=>'YOUR URL'),1));
|
||||
//单个图片file,mode:1为检测最大的人脸 , 0为检测所有人脸
|
||||
var_dump ($client->faceShape(array('file'=>'F:\pic\face1.jpg'),0));
|
||||
//单个图片内容,mode:1为检测最大的人脸 , 0为检测所有人脸
|
||||
var_dump ($client->faceShape(array('buffer'=>file_get_contents('F:\pic\face1.jpg')), 1));
|
||||
```
|
||||
|
||||
##### 个体信息管理
|
||||
```php
|
||||
//个体创建,创建一个Person,并将Person放置到group_ids指定的组当中,不存在的group_id会自动创建。
|
||||
//创建一个Person, 使用图片url
|
||||
var_dump ($client->faceNewPerson('person1111', array('group11',), array('url'=>'YOUR URL'), 'xiaoxin'));
|
||||
//创建一个Person, 使用图片file
|
||||
var_dump ($client->faceNewPerson('person2111', array('group11',), array('file'=>'F:\pic\hot1.jpg')));
|
||||
//创建一个Person, 使用图片内容
|
||||
var_dump ($client->faceNewPerson('person3111', array('group11',), array('buffer'=>file_get_contents('F:\pic\zhao1.jpg'))));
|
||||
|
||||
//增加人脸,将一组Face加入到一个Person中。
|
||||
//将单个或者多个Face的url加入到一个Person中
|
||||
var_dump ($client->faceAddFace('person1111', array('urls'=>array('YOUR URL A',
|
||||
'YOUR URL B'))));
|
||||
//将单个或者多个Face的file加入到一个Person中
|
||||
var_dump ($client->faceAddFace('person2111', array('files'=>array('F:\pic\yang.jpg','F:\pic\yang2.jpg'))));
|
||||
//将单个或者多个Face的文件内容加入到一个Person中
|
||||
var_dump ($client->faceAddFace('person3111', array('buffers'=>array(file_get_contents('F:\pic\yang.jpg'),file_get_contents('F:\pic\yang2.jpg')))));
|
||||
|
||||
// 删除人脸,删除一个person下的face
|
||||
var_dump ($client->faceDelFace('person1', array('12346',)));
|
||||
|
||||
//设置信息
|
||||
var_dump ($client->faceSetInfo('person1', 'fanbing'));
|
||||
|
||||
//获取信息
|
||||
var_dump ($client->faceGetInfo('person1'));
|
||||
|
||||
//获取组列表
|
||||
var_dump ($client->faceGetGroupIds());
|
||||
|
||||
//获取人列表
|
||||
var_dump ($client->faceGetPersonIds('group1'));
|
||||
|
||||
//获取人脸列表
|
||||
var_dump ($client->faceGetFaceIds('person1'));
|
||||
|
||||
//获取人脸信息
|
||||
var_dump ($client->faceGetFaceInfo('1704147773393235686'));
|
||||
|
||||
//删除个人
|
||||
var_dump ($client->faceDelPerson('person11'));
|
||||
```
|
||||
|
||||
##### 人脸验证
|
||||
给定一个Face和一个Person,返回是否是同一个人的判断以及置信度
|
||||
|
||||
```php
|
||||
//单个图片Url
|
||||
var_dump ($client->faceVerify('person1', array('url'=>'YOUR URL')));
|
||||
//单个图片file
|
||||
var_dump ($client->faceVerify('person3111', array('file'=>'F:\pic\yang3.jpg')));
|
||||
//单个图片内容
|
||||
var_dump ($client->faceVerify('person3111', array('buffer'=>file_get_contents('F:\pic\yang3.jpg'))));
|
||||
```
|
||||
|
||||
##### 人脸检索
|
||||
对于一个待识别的人脸图片,在一个Group中识别出最相似的Top5 Person作为其身份返回,返回的Top5中按照相似度从大到小排列。
|
||||
|
||||
```php
|
||||
//单个文件url
|
||||
var_dump ($client->faceIdentify('group1', array('url'=>'YOUR URL')));
|
||||
//单个文件file
|
||||
var_dump ($client->faceIdentify('group11', array('file'=>'F:\pic\yang3.jpg')));
|
||||
//单个文件内容
|
||||
var_dump ($client->faceIdentify('group11', array('buffer'=>file_get_contents('F:\pic\yang3.jpg'))));
|
||||
```
|
||||
|
||||
##### 人脸对比
|
||||
|
||||
```php
|
||||
//两个对比图片的文件url
|
||||
var_dump ($client->faceCompare(array('url'=>"YOUR URL A"),
|
||||
array('url'=>'YOUR URL B')));
|
||||
//两个对比图片的文件file
|
||||
var_dump ($client->faceCompare(array('file'=>'F:\pic\yang.jpg'), array('file'=>'F:\pic\yang2.jpg')));
|
||||
//两个对比图片的文件内容
|
||||
var_dump ($client->faceCompare(array('file'=>'F:\pic\yang.jpg'), array('file'=>'F:\pic\yang2.jpg')));
|
||||
```
|
||||
|
||||
|
||||
#### 3.3 人脸核身
|
||||
|
||||
##### 身份证识别对比
|
||||
|
||||
```php
|
||||
//身份证url
|
||||
var_dump ($client->faceIdCardCompare('xxxxxxxxxxx', 'xxxxxxxxxxx', array('url'=>'YOUR URL')));
|
||||
//身份证文件file
|
||||
var_dump ($client->faceIdCardCompare('xxxxxxxxxxx', 'xxxxxxxxxxx', array('file'=>'F:\pic\idcard.jpg')));
|
||||
//身份证文件内容
|
||||
var_dump ($client->faceIdCardCompare('xxxxxxxxxxx', 'xxxxxxxxxxx', array('buffer'=>file_get_contents('F:\pic\idcard.jpg'))));
|
||||
```
|
||||
|
||||
##### 活体检测—获取唇语验证码
|
||||
|
||||
```php
|
||||
$obj = $client->faceLiveGetFour();
|
||||
var_dump ($obj);
|
||||
$validate_data = $obj['data']['validate_data'];
|
||||
```
|
||||
|
||||
##### 活体检测-视频与用户照片的比对
|
||||
|
||||
```php
|
||||
var_dump ($client->faceLiveDetectFour($validate_data, array('file'=>'F:\pic\ZOE_0171.mp4'), False, array('F:\pic\idcard.jpg')));
|
||||
```
|
||||
|
||||
##### 活体检测-视频与身份证高清照片的比对
|
||||
|
||||
```php
|
||||
var_dump ($client->faceIdCardLiveDetectFour($validate_data, array('file'=>'F:\pic\ZOE_0171.mp4'), 'xxxxxxxxxxx', 'xxxxxxxxxxx'));
|
||||
```
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
function classLoader($class)
|
||||
{
|
||||
$path = str_replace('\\', DIRECTORY_SEPARATOR, $class);
|
||||
$file = __DIR__ . DIRECTORY_SEPARATOR . $path . '.php';
|
||||
if (file_exists($file)) {
|
||||
require_once $file;
|
||||
}
|
||||
}
|
||||
spl_autoload_register('classLoader');
|
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
|
||||
require_once __DIR__ . '/autoload.php';
|
|
@ -0,0 +1,150 @@
|
|||
<?php
|
||||
|
||||
require_once __DIR__ . '/index.php';
|
||||
use QcloudImage\CIClient;
|
||||
|
||||
$appid = 'YOUR_APPID';
|
||||
$secretId = 'YOUR_SECRETID';
|
||||
$secretKey = 'YOUR_SECRETKEY';
|
||||
$bucket = 'YOUR_BUCKET';
|
||||
|
||||
$client = new CIClient($appid, $secretId, $secretKey, $bucket);
|
||||
$client->setTimeout(30);
|
||||
|
||||
//图片鉴黄
|
||||
//单个或多个图片Url
|
||||
var_dump ($client->pornDetect(array('urls'=>array('YOUR URL A','YOUR URL B'))));
|
||||
//单个或多个图片File
|
||||
var_dump ($client->pornDetect(array('files'=>array('F:\pic\你好.jpg','G:\pic\test2.jpg'))));
|
||||
|
||||
//图片标签
|
||||
//单个图片url
|
||||
var_dump ($client->tagDetect(array('url'=>'YOUR URL')));
|
||||
//单个图片file
|
||||
var_dump ($client->tagDetect(array('file'=>'G:\pic\hot1.jpg')));
|
||||
//单个图片内容
|
||||
var_dump ($client->tagDetect(array('buffer'=>file_get_contents('G:\pic\hot1.jpg'))));
|
||||
|
||||
//身份证识别
|
||||
//单个或多个图片Url,识别身份证正面
|
||||
var_dump ($client->idcardDetect(array('urls'=>array('YOUR URL A', 'YOUR URL B')), 0));
|
||||
//单个或多个图片file,识别身份证正面
|
||||
var_dump ($client->idcardDetect(array('files'=>array('F:\pic\id6_zheng.jpg', 'F:\pic\id2_zheng.jpg')), 0));
|
||||
//单个或多个图片内容,识别身份证正面
|
||||
var_dump ($client->idcardDetect(array('buffers'=>array(file_get_contents('F:\pic\id6_zheng.jpg'), file_get_contents('F:\pic\id2_zheng.jpg'))), 0));
|
||||
|
||||
//单个或多个图片Url,识别身份证反面
|
||||
var_dump ($client->idcardDetect(array('urls'=>array('YOUR URL A', 'YOUR URL B')), 1));
|
||||
//单个或多个图片file,识别身份证反面
|
||||
var_dump ($client->idcardDetect(array('files'=>array('F:\pic\id5_fan.jpg', 'F:\pic\id7_fan.png')), 1));
|
||||
//单个或多个图片内容,识别身份证反面
|
||||
var_dump ($client->idcardDetect(array('buffers'=>array(file_get_contents('F:\pic\id5_fan.jpg'), file_get_contents('F:\pic\id7_fan.png'))), 1));
|
||||
|
||||
//名片识别
|
||||
//单个或多个图片Url
|
||||
var_dump ($client->namecardDetect(array('urls'=>array('YOUR URL A', 'YOUR URL B')), 0));
|
||||
//单个或多个图片file,
|
||||
var_dump ($client->namecardDetect(array('files'=>array('F:\pic\r.jpg', 'F:\pic\name2.jpg')), 1));
|
||||
//单个或多个图片内容
|
||||
var_dump ($client->namecardDetect(array('buffers'=>array(file_get_contents('F:\pic\name1.jpg'), file_get_contents('F:\pic\name2.jpg'))), 0));
|
||||
|
||||
//人脸检测
|
||||
//单个图片Url, mode:1为检测最大的人脸 , 0为检测所有人脸
|
||||
var_dump ($client->faceDetect(array('url'=>'YOUR URL'), 1));
|
||||
//单个图片file,mode:1为检测最大的人脸 , 0为检测所有人脸
|
||||
var_dump ($client->faceDetect(array('file'=>'F:\pic\face1.jpg'),0));
|
||||
//单个图片内容,mode:1为检测最大的人脸 , 0为检测所有人脸
|
||||
var_dump ($client->faceDetect(array('buffer'=>file_get_contents('F:\pic\face1.jpg')), 1));
|
||||
|
||||
//五官定位
|
||||
//单个图片Url,检测最大的人脸
|
||||
var_dump ($client->faceShape(array('url'=>'YOUR URL'),1));
|
||||
//单个图片Url,检测所有人脸
|
||||
var_dump ($client->faceShape(array('file'=>'F:\pic\face1.jpg'),0));
|
||||
//单个图片Url,检测所有人脸
|
||||
var_dump ($client->faceShape(array('buffer'=>file_get_contents('F:\pic\face1.jpg')), 1));
|
||||
|
||||
|
||||
//创建一个Person,并将Person放置到group_ids指定的组当中, 使用图片url
|
||||
var_dump ($client->faceNewPerson('person1111', array('group11',), array('url'=>'YOUR URL'), 'xiaoxin'));
|
||||
//创建一个Person,并将Person放置到group_ids指定的组当中, 使用图片file
|
||||
var_dump ($client->faceNewPerson('person2111', array('group11',), array('file'=>'F:\pic\hot1.jpg')));
|
||||
//创建一个Person,并将Person放置到group_ids指定的组当中, 使用图片内容
|
||||
var_dump ($client->faceNewPerson('person3111', array('group11',), array('buffer'=>file_get_contents('F:\pic\zhao1.jpg'))));
|
||||
|
||||
|
||||
//增加人脸,将单个或者多个Face的url加入到一个Person中.注意,一个Face只能被加入到一个Person中。 一个Person最多允许包含20个Face
|
||||
var_dump ($client->faceAddFace('person_one', array('urls'=>array('YOUR URL A','YOUR URL B'))));
|
||||
//增加人脸,将单个或者多个Face的file加入到一个Person中.注意,一个Face只能被加入到一个Person中。 一个Person最多允许包含20个Face
|
||||
var_dump ($client->faceAddFace('person_two', array('files'=>array('F:\pic\yang.jpg','F:\pic\yang2.jpg'))));
|
||||
//增加人脸,将单个或者多个Face的文件内容加入到一个Person中.注意,一个Face只能被加入到一个Person中。 一个Person最多允许包含20个Face
|
||||
var_dump ($client->faceAddFace('person_three', array('buffers'=>array(file_get_contents('F:\pic\yang.jpg'),file_get_contents('F:\pic\yang2.jpg')))));
|
||||
|
||||
//删除人脸
|
||||
var_dump ($client->faceDelFace('person_one', array('one',)));
|
||||
//设置信息
|
||||
var_dump ($client->faceSetInfo('person_one', 'fanbing'));
|
||||
//获取信息
|
||||
var_dump ($client->faceGetInfo('person_one'));
|
||||
//获取组列表
|
||||
var_dump ($client->faceGetGroupIds());
|
||||
//获取人列表
|
||||
var_dump ($client->faceGetPersonIds('group1'));
|
||||
//获取人脸列表
|
||||
var_dump ($client->faceGetFaceIds('person_one'));
|
||||
//获取人脸信息
|
||||
var_dump ($client->faceGetFaceInfo('1704147773393235686'));
|
||||
//删除个人
|
||||
var_dump ($client->faceDelPerson('person_one'));
|
||||
|
||||
//人脸验证
|
||||
//单个图片Url
|
||||
var_dump ($client->faceVerify('person1', array('url'=>'YOUR URL')));
|
||||
//单个图片file
|
||||
var_dump ($client->faceVerify('person3111', array('file'=>'F:\pic\yang3.jpg')));
|
||||
//单个图片内容
|
||||
var_dump ($client->faceVerify('person3111', array('buffer'=>file_get_contents('F:\pic\yang3.jpg'))));
|
||||
|
||||
//人脸检索
|
||||
//单个文件url
|
||||
var_dump ($client->faceIdentify('group1', array('url'=>'YOUR URL')));
|
||||
//单个文件file
|
||||
var_dump ($client->faceIdentify('group11', array('file'=>'F:\pic\yang3.jpg')));
|
||||
//单个文件内容
|
||||
var_dump ($client->faceIdentify('group11', array('buffer'=>file_get_contents('F:\pic\yang3.jpg'))));
|
||||
|
||||
//人脸对比
|
||||
//两个对比图片的文件url
|
||||
var_dump ($client->faceCompare(array('url'=>"YOUR URL A"), array('url'=>'YOUR URL B')));
|
||||
//两个对比图片的文件file
|
||||
var_dump ($client->faceCompare(array('file'=>'F:\pic\yang.jpg'), array('file'=>'F:\pic\yang2.jpg')));
|
||||
//两个对比图片的文件内容
|
||||
var_dump ($client->faceCompare( array('buffer'=>file_get_contents('F:\pic\yang.jpg')), array('buffer'=>file_get_contents('F:\pic\yang3.jpg'))));
|
||||
|
||||
|
||||
//身份证识别对比
|
||||
//身份证url
|
||||
var_dump ($client->faceIdCardCompare('ID CARD NUM', 'NAME', array('url'=>'YOUR URL')));
|
||||
//身份证文件file
|
||||
var_dump ($client->faceIdCardCompare('ID CARD NUM', 'NAME', array('file'=>'F:\pic\idcard.jpg')));
|
||||
//身份证文件内容
|
||||
var_dump ($client->faceIdCardCompare('ID CARD NUM', 'NAME', array('buffer'=>file_get_contents('F:\pic\idcard.jpg'))));
|
||||
|
||||
|
||||
//人脸核身
|
||||
//活体检测第一步:获取唇语(验证码)
|
||||
$obj = $client->faceLiveGetFour();
|
||||
var_dump ($obj);
|
||||
$faceObj = json_decode($obj, true);
|
||||
var_dump ($faceObj);
|
||||
$validate_data = '';
|
||||
if ($faceObj && isset($faceObj['data']['validate_data'])) {
|
||||
$validate_data = $faceObj['data']['validate_data'];
|
||||
}
|
||||
var_dump ($validate_data);
|
||||
|
||||
//活体检测第二步:检测
|
||||
var_dump ($client->faceLiveDetectFour($validate_data, array('file'=>'F:\pic\ZOE_0171.mp4'), False, array('F:\pic\idcard.jpg')));
|
||||
//活体检测第二步:检测--对比指定身份信息
|
||||
var_dump ($client->faceIdCardLiveDetectFour($validate_data, array('file'=>'F:\pic\ZOE_0171.mp4'), '330782198802084329', '季锦锦'));
|
||||
|
|
@ -17,6 +17,8 @@ $(document).ready(function()
|
|||
},
|
||||
onSuccess:function(files,data,xhr,pd)
|
||||
{
|
||||
var eroticism = $("#eroticism").html(); //鉴黄状态
|
||||
var tinypng = $("#tinypng").html(); //压缩状态
|
||||
$("#loading").hide();
|
||||
$("#relink").show();
|
||||
var imginfo = new Function("return" + data)();
|
||||
|
@ -27,13 +29,20 @@ $(document).ready(function()
|
|||
$("#show_img").attr('src',imginfo.linkurl);
|
||||
$("#img-url").attr('href',imginfo.linkurl);
|
||||
$("#img-box").show();
|
||||
//如果启用了鉴黄
|
||||
if(eroticism == 1) {
|
||||
identify(imginfo.linkurl);
|
||||
}
|
||||
//如果启用了图片压缩
|
||||
if(tinypng == 1) {
|
||||
compression(imginfo.linkurl);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//复制按钮
|
||||
function copy(url) {
|
||||
|
||||
new clipBoard($("#url"),{
|
||||
copy: function() {
|
||||
return $("#" + url).val();
|
||||
|
@ -44,4 +53,29 @@ function copy(url) {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
//鉴黄
|
||||
function identify(imgurl) {
|
||||
$.get("./api/identify.php?url="+imgurl,function(data,status){
|
||||
var reinfo = new Function("return" + data)();
|
||||
if((reinfo.code == 0) && (reinfo.result >= 1)) {
|
||||
alert('请勿上传违规图片!');
|
||||
return false;
|
||||
}
|
||||
//请求失败,重复执行
|
||||
while(reinfo.code == null) {
|
||||
$.get("./api/identify.php?url="+imgurl,function(data,status){
|
||||
var reinfo = new Function("return" + data)();
|
||||
if((reinfo.code == 0) && (reinfo.result >= 1)) {
|
||||
alert('请勿上传违规图片!');
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
//图片压缩
|
||||
function compression(imgurl) {
|
||||
$.get("./api/tinypng.php?url="+imgurl,function(data,status){
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
require_once 'wxyt/index.php';
|
||||
use QcloudImage\CIClient;
|
||||
$client = new CIClient('', '', '', '');
|
||||
$client->setTimeout(30);
|
||||
$imginfo = ($client->pornDetect(array('urls'=>array('https://imgurl.org/upload/1712/caace16a4a5b0646.png'))));
|
||||
|
||||
$imginfo = json_decode($imginfo);
|
||||
|
||||
//获取状态码,0为成功
|
||||
//$code = $imginfo->http_code;
|
||||
//转换为数组
|
||||
$imginfo = object2array($imginfo);
|
||||
//状态码,0为成功
|
||||
$code = $imginfo['result_list']['0']->code;
|
||||
$imginfo = object2array($imginfo['result_list']['0']->data);
|
||||
//识别结果,0 正常,1 黄图,2 疑似图片
|
||||
$result = $imginfo['result'];
|
||||
//识别评分,分数越高,越可能是黄图
|
||||
$confidence = $imginfo['confidence'];
|
||||
|
||||
//重新返回json数据
|
||||
$re_data = array(
|
||||
"code" => $code,
|
||||
"result" => $result,
|
||||
"confidence"=> $confidence
|
||||
);
|
||||
echo $re_data = json_encode($re_data);
|
||||
?>
|
||||
|
||||
<?php
|
||||
//对象转数组
|
||||
function object2array($object) {
|
||||
if (is_object($object)) {
|
||||
foreach ($object as $key => $value) {
|
||||
$array[$key] = $value;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$array = $object;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
?>
|
25
upload.php
25
upload.php
|
@ -2,17 +2,6 @@
|
|||
error_reporting(E_ALL^E_NOTICE^E_WARNING^E_DEPRECATED);
|
||||
include_once('./config.php');
|
||||
|
||||
//检测是否使用Tinypng压缩图片
|
||||
if($config['tinypng'] != '') {
|
||||
//载入SDK
|
||||
require_once("lib/Tinify/Exception.php");
|
||||
require_once("lib/Tinify/ResultMeta.php");
|
||||
require_once("lib/Tinify/Result.php");
|
||||
require_once("lib/Tinify/Source.php");
|
||||
require_once("lib/Tinify/Client.php");
|
||||
require_once("lib/Tinify.php");
|
||||
}
|
||||
|
||||
//验证用户,并设置上传目录
|
||||
$dir = check($_COOKIE['uid'],$config['username'],$config['password'],$config['userdir'],$config['admindir']);
|
||||
|
||||
|
@ -92,7 +81,7 @@
|
|||
//如果上传成功
|
||||
if(move_uploaded_file($img_tmp,$dir_name)){
|
||||
//压缩图片
|
||||
tinypng($config['tinypng'],$dir_name);
|
||||
//tinypng($config['tinypng'],$dir_name);
|
||||
$img_url = $config['domain'].$dir_name; //自定义图片路径
|
||||
$img_info = getimagesize($dir_name);
|
||||
$img_width = $img_info['0']; //图片宽度
|
||||
|
@ -100,6 +89,7 @@
|
|||
$re_data = array("linkurl" => $img_url,width => $img_width,"height" => $img_height,"status" => 'ok');
|
||||
//返回json格式
|
||||
echo json_encode($re_data);
|
||||
exit;
|
||||
}
|
||||
//没有上传成功
|
||||
else{
|
||||
|
@ -125,15 +115,4 @@
|
|||
return $udir;
|
||||
}
|
||||
}
|
||||
//压缩图片
|
||||
function tinypng($api,$imgfile){
|
||||
if($api == '') {
|
||||
return $imgfile;
|
||||
}
|
||||
else{
|
||||
Tinify\setKey($api);
|
||||
Tinify\fromFile($imgfile)->toFile($imgfile);
|
||||
return $imgfile;
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in New Issue