PHP 实现随机图片 api 接口以及9000多个头像打包下载


共计 685 个字符,预计需要花费 2 分钟才能阅读完成。

由于博客头像经常图裂,清朝云考虑做个随机头像 api 接口,资源文件在后面,代码如下:

<?php
$type = isset($_GET['type']) ? $_GET['type'] : '';
$dirPath = './' . $type . '/';
if ($type != '' && is_dir($dirPath)) {
  $files = glob($dirPath . '*.{jpg,png,webp}', GLOB_BRACE);
  if (count($files) > 0) {
    $imagePath = $files[array_rand($files)]; // 随机选择一张图片
    header('Content-Type: image/jpeg');
    readfile($imagePath);
  } else {
    echo $files.'No image found in this directory';
  }
} else {
  echo 'Invalid request';
}
?>

该 php 文件可放在根目录下的 randomimg 文件夹下,命名为 index.php,然后将头像文件夹命名为 tx,放在 randimg 文件夹下,访问 /randomimg/index.php?type=tx 即可访问随机头像。

如果你有其他的类型的图片,比如随机缩略图,那么命名为 thumbnail 文件夹,同样放在 randomimg 文件夹下,访问 /randomimg/index.php?type=thumbnail 即可访问随机缩略图。

相关截图:

PHP 实现随机图片 api 接口以及9000多个头像打包下载

/>

下载地址

9000多个头像:https://pan.baidu.com/s/1xtwYOs7PllY_LCcbP5O94A?pwd=akak

提醒:本文发布于543天前,文中所关联的信息可能已发生改变,请知悉!

Tips:清朝云网络工作室

阅读剩余
THE END