php中直接输出随机图片的API

180it 2020-10-20 PM 1728℃ 0条

最近应一个博友要求写了个随机图的api,可让php直接在浏览器中输出图片,支持本地文件也支持图片链接,主要功能就类似于那位博友说的网上的漫月api,下面言归正传直接贴上代码吧。

index.php(主体程序)

<?php
error_reporting(E_ERROR);
require_once '../include/common.php';
require_once 'lib/imgdata.php';
require_once 'lib/functions.php';

$karnc = new imgdata;

if ($_GET['a'] == 'local') {
    /* 列出指定目录下的图片 */
    $CONFIG = array(
        'imageManagerAllowFiles' => array(".png", ".jpg", ".jpeg", ".gif", ".bmp"),
        'imageManagerListPath' => "/background/upload/",
    );

    $host = 'http://' . $_SERVER['HTTP_HOST'];

    $allowFiles = $CONFIG['imageManagerAllowFiles'];
    $path = $CONFIG['imageManagerListPath'];

    $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);

    /* 获取文件列表 */
    $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path;
    $files = getfiles($path, $allowFiles);
    if (!count($path) || empty($files)) {
        $imgFile = 'http://api.karnc.com/background/404.jpg';
        $karnc->getdir($imgFile);
        $karnc->img2data();
        $karnc->data2img();
        die;
    }

    /* 获取指定范围的列表 */
    $len = count($files);
    for ($i = 0, $list = array(); $i < $len; $i++) {
        $list[] = $files[$i];
    }

    $rand = array_rand($list, 1);

    $img = $list[$rand];

    $imgUrl = $host . $img;

    $imgFile = $_SERVER['DOCUMENT_ROOT'] . (substr($list[$rand], 0, 1) == "/" ? "" : "/") . $img;
} else {
    //这里是从数据库中取相应图片(博主是使用树洞外链+又拍云做的)
    $img = $db_pdo->getOne("select `ming` from sd_file where quality = 1 ORDER BY RAND() limit 1");
    $host = 'https://static.karnc.com/';
    $imgFile = $imgUrl = $host . $img['ming'];
}

$refer = $_SERVER['HTTP_REFERER']; //前一URL
$karnc->getdir($imgFile);
$karnc->img2data();
$karnc->data2img();
die;

?>

imgdata.php(浏览器输出图片类)

<?php
class imgdata{
    public $imgsrc;
    public $imgdata;
    public $imgform;
    public function getdir($source){
        $this->imgsrc  = $source;
    }
    public function img2data(){
        $this->_imgfrom($this->imgsrc);
        return $this->imgdata=fopen($this->imgsrc,'rb');
    }
    public function data2img(){
        header("content-type:$this->imgform");
        return fpassthru($this->imgdata);
    }
    public function _imgfrom($imgsrc){
        $info=getimagesize($imgsrc);
        return $this->imgform = $info['mime'];
    }
}
?>

function.php(公共函数)

<?php

/**
 * 遍历获取目录下的指定类型的文件
 * @param $path
 * @param array $files
 * @return array
 */
function getfiles($path,$allowFiles,&$files = array())
{
    if (!is_dir($path)) return null;
    if(substr($path, strlen($path) - 1) != '/') $path .= '/';
    $handle = opendir($path);
    while (false !== ($file = readdir($handle))) {
        if ($file != '.' && $file != '..') {
            $path2 = $path . $file;
            if (is_dir($path2)) {
                getfiles($path2, $allowFiles, $files);
            } else {
                if (preg_match("/\.(".$allowFiles.")$/i", $file)) {
                    $files[] =substr($path2, strlen($_SERVER['DOCUMENT_ROOT']));
                }
            }
        }
    }
    return $files;
}

/**
 * 域名白名单校验
 * @param $domain_list(白名单)
 * @return true/false
 */
function checkReferer($domain_list=array('karnc.com','www.karnc.com')){
    $status=false;
    $refer=  $_SERVER['HTTP_REFERER']; //前一URL
    if($refer){
        $referhost=parse_url($refer);
        $host=strtolower($referhost['host']);//来源地址主域名
        if($host==$_SERVER['HTTP_HOST'] || in_array($host,$domain_list)){
            $status=true;
        }
    }
    return $status;
}

?>

这个随机图api的主要代码都在上面,在使用php直接在浏览器输出图片与个人带宽有关,图片太大还是会影响页面的加载速度,所以还是尽可能的压缩下图片再上传。图片外链我使用的是树洞外链+又拍云做的,本来打算使用又拍云的图片缩略处理功能的,这样可以让接口响应速度快一点,这个功能等有时间再研究下吧。

来源:https://karnc.com/random-images-api.html

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php中直接输出随机图片的API