米尔嘉
米尔嘉
发布于 2024-09-14 / 2 阅读
0
0

随机图片Api

随机图片Api源码一

将图片放在random.php同目录下的images文件夹里,访问random.php即可。

<?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="images/*.{gif,jpg,png}";

}else{

    $localurl="images/*.{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");

?>

随机图片Api源码二

将图片放在random.php同目录下的images文件夹里,访问random.php即可。

<?php

    // 图片文件夹路径

    $imageFolder = 'images/';

    

    // 获取图片文件列表

    $imageFiles = scandir($imageFolder);

    

    // 过滤掉"."和".."目录

    $imageFiles = array_diff($imageFiles, array('..', '.'));

    

    // 随机选择一张图片

    $randomImage = $imageFiles[array_rand($imageFiles)];

    

    // 图片显示

    // echo '<img src="' . $imageFolder . $randomImage . '" alt="随机图片">';

    // echo '<img src="' . $imageFolder . $randomImage . '" alt="随机图片"           style="width: 100%;height:100%;">';

    header("location:$imageFolder$randomImage");

?>

随机图片Api源码三(服务器本地调用图片)

1.新建一个文件夹,命名为:images(这个文件夹放你需要调用的图片)

2.新建一个index.php文件,写入以下代码 (这个文件就是API地址)

<?php

$allowed_extensions = ['webp', 'gif', 'jpg', 'png'];

$directory = "images/";

$img_array = [];

if (is_dir($directory)) {

    $files = scandir($directory);

    foreach ($files as $file) {

        $path_parts = pathinfo($file);

        if (in_array(strtolower($path_parts['extension']), $allowed_extensions)) {

            $img_array[] = $directory . $file;

        }

    }

    if (!empty($img_array)) {

        $random_img = $img_array[array_rand($img_array)];

        header("Location: " . $random_img);

        exit;

    } else {

        echo "No images found.";

    }

} else {

    echo "Directory does not exist.";

}

?>

随机图片Api源码四(服务器文件内图片链接进行调用)

1.创建一个img.txt文件 (文件内放置你的图片链接注意:一行一个链接)

2.新建一个index.php文件,写入以下代码 (这个文件就是api地址)

<?php

//存有链接的文件名,这里是存放图片链接的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"));

}

?>

随机图片Api源码五

1.创建一个images文件夹 (文件夹内放你的图片)

2.新建一个index.php文件,写入以下代码 (这个文件就是api地址)

<?php

$imageDirectory = '您的文件存储路径或网站';

// 检查目录是否存在

if (!is_dir($imageDirectory)) {

    echo "图片目录不存在";

    exit;

}

$imageFiles = scandir($imageDirectory);

$imageFiles = array_filter($imageFiles, function($file) {

    $ext = pathinfo($file, PATHINFO_EXTENSION);

    return in_array($ext, ['jpg', 'png', 'gif', 'webp']);

});

if (empty($imageFiles)) {

    echo "没有找到符合条件的图片";

    exit;

}

$randomImageFile = $imageFiles[array_rand($imageFiles)];

$imageName = pathinfo($randomImageFile, PATHINFO_FILENAME);

$imageExt = pathinfo($randomImageFile, PATHINFO_EXTENSION);

$imageUrl = "https://访问的域名/$imageName.$imageExt";

// 读取并输出图片

header('Content-Type: image/'. $imageExt);

$imageContent = file_get_contents($imageUrl);

echo $imageContent;

?>


评论