最新公告
  • 资源宝-专注于优秀资源,本站秉承服务宗旨 履行“站长”责任, 服务永无止境!
  • PHP实现随机图片API

    已弃用QQ,邮箱:[email protected] 【哔哩哔哩留言】上线看见信息第一时间回复,本站大部分源码亲测

    分享两种PHP随机图片源码,第一种访问本地图片,第二种访问图片链接,代码如下:

    <!-- 资源宝分享:www.httple.net -->
    <?php
    header('Cache-Control:no-cache,must-revalidate');
    header('Pragma:no-cache');
    header("Expires:0");
    header("Access-Control-Allow-Origin:*");
    //处理请求输出数据
    //这将得到一个文件夹中的所有gif,jpg和png图片的数组
    $rand=rand(0,1);
    if($rand){
        $localurl="img/*.{gif,jpg,png}"; //图片所在文件夹
    }else{
        $localurl="img/*.{gif,jpg,png}";
    }
    $img_array=glob($localurl,GLOB_BRACE);
    //从数组中选择一个随机图片 
    $img=array_rand($img_array);
    $imgurl=$img_array[$img];
    $https=isset($_GET["https"])?$_GET["https"]:1;
    if($https == "true"){
        $imgurl='https://'.$_SERVER['SERVER_NAME'].'/'.$imgurl;
    }else{
        $imgurl='http://'.$_SERVER['SERVER_NAME'].'/'.$imgurl;
    }
    if(isset($_GET["type"])?$_GET["type"]:1=="json"){
        $rTotal='0';
        $gTotal='0';
        $bTotal='0';
        $total='0';
        $imageInfo = getimagesize($img_array[$img]);
        //图片类型
        $imgType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
        //对应函数
        $imageFun = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType);
        $i = $imageFun($img_array[$img]);
        //测试图片,自己定义一个,注意路径
        for($x=0;
        $x<imagesx($i);
        $x++){
            for($y=0;
            $y<imagesy($i);
            $y++){
                $rgb=imagecolorat($i,$x,$y);
                $r=($rgb>>16)&0xFF;
                $g=($rgb>>8)&0xFF;
                $b=$rgb&0xFF;
                $rTotal+=$r;
                $gTotal+=$g;
                $bTotal+=$b;
                $total++;
            }
        }
        $rAverage=round($rTotal/$total);
        $gAverage=round($gTotal/$total);
        $bAverage=round($bTotal/$total);
        $arr=array('ImgUrl'=>$imgurl,'Color'=>"$rAverage,$gAverage,$bAverage");
        echo json_encode($arr);
        exit();
    }
    //在页面显示图片地址
    //echo $imgurl;
    header("location:$imgurl");
    
    
    <?php
    //资源宝分享:www.httple.net
    //存有美图链接的文件名img.txt
    $filename = "img.txt";
    if(!file_exists($filename)){
        die('文件不存在');
    }
     
    //从文本获取链接
    $pics = [];
    $fs = fopen($filename, "r");
    while(!feof($fs)){
        $line=trim(fgets($fs));
        if($line!=''){
            array_push($pics, $line);
        }
    }
     
    //从数组随机获取链接
    $pic = $pics[array_rand($pics)];
     
    //返回指定格式
    $type=$_GET['type'];
    switch($type){
     
    //JSON返回
    case 'json':
        header('Content-type:text/json');
        die(json_encode(['pic'=>$pic]));
     
    default:
        die(header("Location: $pic"));
    }
     
    ?>
    
    

     

    资源宝-—专注于优秀资源搜集共享与发布
    资源宝 » PHP实现随机图片API
    本站所有资源来源于网络,仅限用于学习研究;无任何技术支持!不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除内容。如果您喜欢,请支持正版。如有侵权请邮件与我们联系处理。

     本站部分源码亲测,单个源码赞助前请咨询 

    • 99999访问总数
    • 206会员总数(位)
    • 9539资源总数(个)
    • 900 资源大小(GB)
    • 2376稳定运行(天)

    提供最优质的资源集合

    网站源码 APP源码