天云图床搜狗图床版-开源API

搜狗图床API

天云图床搜狗图床版

最近新浪图床已经失效,无法再上传,研究了一下搜狗图片的上传识图接口,写了一个PHP版的上传接口

0x00 API源码:

<?php

/**
 * 搜狗图床上传
 * author:DYBOY
 */

header("Access-Control-Allow-Origin:*");
header('Content-type: application/json');
error_reporting(0);

// $type 上传方式:multipart, remote, bs64
$type = isset($_REQUEST['type']) ? addslashes(trim($_REQUEST['type'])) : 'multipart';    //上传方式
$num = isset($_GET['num']) ? addslashes(trim($_GET['num'])) : '';
$userIp = empty($_SERVER['REMOTE_ADDR']) ? '0.0.0.0' : $_SERVER['REMOTE_ADDR'];


if ($num == 'multiple') {
    if (count($_FILES['files']['tmp_name']) > 15) {
        echo json_encode([
            'code' => '444',
            'data' => '上传图片数量不能超过15张,请重新选择图片!'
        ]);
        exit();
    }
    $result = [];
    $count = 0;
    foreach ($_FILES['files']['tmp_name'] as $key => $value) {
        // 缓存图片目录
        $tempImgPath = dirname(__FILE__) . '/tempimg/';
        if(!is_dir($tempImgPath)){ mkdir('tempimg', 0777, true); }
        // 新文件名
        $newFileName = $tempImgPath . md5(rand(10000,9999)) . '.jpg';
        // 放入缓存目录
        if(!move_uploaded_file($value, $newFileName)){
            unlink($newFileName);
            // 上传失败
        }
        $file = $newFileName;
        $imgarr = upload($file);
        $imgarr['name'] = addslashes($_FILES["files"]["name"][$count]);
        $sql = 'INSERT INTO imgs(imgurl,imgname,uptime,user_ip) VALUES("' . $imgarr['url2'] . '", "' . $imgarr['name'] . '", now(),"' . $userIp . '")';
        mysqli_query($conn, $sql);
        $count += 1;
        array_push($result, $imgarr);
    }
    echo json_encode($result);
    exit();
}


// 上传文件
function upload($file, $type = 'multipart')
{
    $apiUrl = 'http://pic.sogou.com/ris_upload?r=' . rand(10000, 99999);
    if ($type == 'multipart') {
        if (class_exists('CURLFile')) {     // php 5.5
            $post['pic_path'] = new \CURLFile(realpath($file));
        } else {
            $post['pic_path'] = '@' . realpath($file);
        }
    }

    // Curl提交
    $ch = curl_init($apiUrl);
    curl_setopt_array($ch, array(
        CURLOPT_POST => true,
        CURLOPT_VERBOSE => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $post,
    ));
    $output = curl_exec($ch);
    curl_close($ch);

    $data = urldecode($output);
    preg_match('/query\=(.*?)\&oname/i',$data, $ImageUrl);

    if(isset($ImageUrl[1])){
        // 获取图片信息
        $ImageUrl[1] = 'https' . substr($ImageUrl[1], 4);
        $localImgInfo = getimagesize($file);
        $res = [
            'code' => 200,
            'width' => $localImgInfo[0],
            'height' => $localImgInfo[1],
            'url' => $ImageUrl[1],
            'url2' => $ImageUrl[1]
        ];
    }
    else {
        // 上传失败返回默认图片
        $res = [
            'code' => 500,
            'width' => 246,
            'height' => 205,
            'name' => 'default.jpg',
            'url' => 'https://api.top15.cn/static/img/errorImg2.png',
            'url2' => 'https://api.top15.cn/static/img/errorImg.png'
        ];

    }
    unlink($file);
    return $res;
}

0x01 前端源码:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="https://blog.dyboy.cn/content/templates/dy_monkey/img/favicon.ico">
    <title>天云图床 - 免费图片上传, 专业图片外链, 免费公共图床床, 极简图床</title>
    <meta name="keywords" content="免费图床,新浪图床,极简图床,路过图床,永久免费图床,稳定图床,图片外链,免费外链,网站外链,天云图床,图床,api.top15.cn" />
    <meta name="description" content="天云图床是一个致力于打造免费高速的图床平台,供广大站长快速存取图片资源获取外链,提升网站速度!图床基于全网稳定外链服务高效极速!存图片,就上天云图床" />
    <meta name="generator" content="天云图床 1.1" /><meta author="DYBOY,dyboy2017@qq.com">
    <link href="https://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">

</head>
<body>
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Menu</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="http://img.top15.cn/">天云图床(img.top15.cn)</a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="http://cos.top15.cn">看美女</a></li>
            <li><a href="https://pay.top15.cn/" target="_blank">天云收款</a></li>
            <li><a href="http://taobao.dyboy.cn" target="_blank" style="color:red;font-weight:bold;">淘宝优惠</a></li>
            <li><a href="https://blog.dyboy.cn/" target="_blank">DYBOY博客</a></li>
          </ul>
        </div>
      </div>
  </nav>
    <div class="container">
        <div class="panel panel-default">
          <div class="panel-heading">多图上传 - 天云图床</div>
          <div class="panel-body" align="center">
              <div class="alert alert-success" role="alert" style="text-align: left;margin-bottom: 15px;">
                <h4>使用说明:</h4>
                  <p>- 请勿上传色情、违法图片,否则永久封禁IP</p>
                <p>- 电脑上鼠标框选,或按住<code>Ctrl+鼠标右键单击</code></p>
                <p>- 每次最多上传 <code>15</code> 张图片,服务器配置升级!!!</p>
                  <p>- 使用中某些浏览器存在卡顿现象,请耐心等待,推荐使用<code>火狐浏览器</code>!</p>
                <p>- 每张图片不能大于<code>2M</code>,否则无法正确获取数据,上传大图时建议单张上传,否则可能导致数据丢失</p>
                <p>- 开源下载:<a href="http://img.top15.cn/tianyun_picbed.zip" target="_blank">天云图床 V1.1.zip</a></p>
                  <p>- 新浪开启了图片防盗链机制,全网基于新浪图床全部挂掉了,天云图床更换源成功,感谢大家支持!!! 2019-4-27 19:25:01</p>
                <p>- 换源搜狗图床,支持SSL,2019-10-7 13:56:53</p>
            </div>
            <button class="btn btn-primary"> 选择图片 </button>
            <button class="btn btn-success" id="upload_btn"> 立即上传 </button>
            <form id="form" enctype="multipart/form-data">
                <input type="file" name="files[]" multiple="multiple" id="files" accept="image/png,image/gif,image/jpg,image/jpeg" style="display: none;">
            </form>

          </div>
        </div>

        <!-- URL超链接版 -->
        <div class="panel panel-default">
            <div class="panel-heading" style="width: 100%;">上传结果(URL版)</div>
            <div class="panel-body" style="margin: 5px">
                <div class="imgs_src" style="background-color: #444a56;padding: 10px;width: 100%; color: #fff;word-wrap:break-word">
                    亲,您还没有上传图片哦!
                </div>
              </div>
        </div>

        <!-- 图片代码版 -->
        <div class="panel panel-default">
            <div class="panel-heading" style="width: 100%;">上传结果(图片版)
                <button class="btn btn-danger" id="copyImgHtml" onclick="copyCodeAreaCt();">复制</button>
            </div>
            <div class="panel-body imgs_src_html" style="margin: 5px">
                <textarea class="textarea" id="textarea" style="background-color: #444a56;padding: 10px;width: 100%; color: #fff;height:200px;">亲,您还没有上传图片哦!</textarea>
              </div>
        </div>

    </div>



    <!-- DYBOY/小东 感谢各位老板保留版权 -->
    <footer style="text-align: center;">
        Copyright © 2018-2019 <a href="https://blog.dyboy.cn/" target="_blank" title="点击访问我的博客" style="color:black;">DYBOY</a>, All Rights Reserved
    </footer>
    <script src="https://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="https://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function(){

            //选择图片
            $('.btn-primary').click(function(){
                $('#files').trigger('click');
            })

            $('#files').change(function(){
                var fileNum = $("#files")[0].files.length;
                $('.btn-primary').text('上传文件('+fileNum+')');
            })

            $('#upload_btn').click(function(){
                  $("#upload_btn").text("上传中...");
                $('#upload_btn').attr('disabled', true);
                var fileNum = $("#files")[0].files.length;
                if(fileNum && fileNum <= 15){
                    $.ajax({
                        url : "https://api.top15.cn/picbed/sgApi.php?type=multipart&num=multiple",
                        type : 'POST',
                        cache: false,
                        data : new FormData($('#form')[0]),
                        processData : false,                 
                        contentType : false,
                        async : false,
                        success : function(data) {
                            if(data){
                                var str = '';
                                var str2 = '';
                                $.each(data, function(index, obj){
                                    str += '<a href="'+ obj.url2+'" target="_blank" rel="noreferrer">'+obj.url2+'</a><br>';
                                    str2 += '<p><a href="'+ obj.url2+'" target="_blank" title="'+obj.name +'"><img src="'+obj.url+'" alt="' + obj.name + '" width="'+obj.width+'" height="'+obj.height+'"></a></p>';
                                })
                                $(".imgs_src").html(str);
                                $(".textarea").text(str2);
                            }
                        }
                    });
                    $("#upload_btn").attr('disabled', false);
                    $('#files').val('');
                    $('.btn-primary').text('上传文件(0)');

                }
                else{
                    alert("请选择上传文件,且上传文件数不超过15!");
                    $('#files').val('');
                    $('.btn-primary').text('上传文件(0)');
                    $("#upload_btn").attr('disabled', false);
                }
              $("#upload_btn").text("一键上传");
            })
        })

        function copyCodeAreaCt() {
            $(".textarea").select();
            document.execCommand("Copy"); // 执行浏览器复制命令
            $("#copyImgHtml").text('已复制');
            setTimeout(function() {
                $("#copyImgHtml").text('复制');
            },2000);
        }

    </script>
    <script>var _hmt = _hmt || [];(function() {var hm = document.createElement("script");hm.src = "https://hm.baidu.com/hm.js?a8569fd6981018f096d774868306a054";var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s);})();</script>
</body>
</html>

0x02 源码下载:

上传服务器或者空间后,访问 http://域名/picbed/ 即可正常使用

下载地址:天云图床-搜狗图床版

发表评论 / Comment

用心评论~