diff --git a/README.md b/README.md index b22da26..b823e8b 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,10 @@ https://minimalistic-wallpaper.demolab.com/?random=3 This API can be used for setting daily wallpapers on a mobile device by combining it with an app such as IFTTT. +> **Note** +> By default, the API will fetch the image from GitHub and return it as content if it is smaller than 4.5MB. +> To force the API to redirect to the image on GitHub instead, add `&redirect=1` to the end of the URL. + ## Contributing All images appearing in the images directory contain the author (sometimes this is the original artist, for others, this is a Reddit user who shared the image or a website the image was found on in cases where the original artist is unknown) and the title or description of the wallpaper. diff --git a/api/index.php b/api/index.php index 034832a..6f19d72 100644 --- a/api/index.php +++ b/api/index.php @@ -1,5 +1,12 @@ 4500000) { + header("Location: $url"); + exit; + } + + // set content type + if (preg_match("/\.(jpg|jpeg)$/", $url)) { + header('Content-Type: image/jpeg'); + } elseif (preg_match("/\.(png)$/", $url)) { + header('Content-Type: image/png'); + } elseif (preg_match("/\.(gif)$/", $url)) { + header('Content-Type: image/gif'); + } + // set default filename + header('Content-Disposition: inline; filename="' . basename($url) . '"'); + // output the image + exit($contents); +} + $REPO = "DenverCoder1/minimalistic-wallpaper-collection"; $BRANCH_NAME = "main"; @@ -30,19 +70,23 @@ $IMGPROXY_PREFIX = "https://dc1imgproxy.fly.dev/x/rs:auto:332:200:1/plain/" . ur // API url to get a listing of images in the directory on GitHub $GITHUB_API_URL = "https://api.github.com/repos/$REPO/contents/$IMAGES_DIRECTORY/"; +// whether to force a redirect to the image instead of displaying it directly +$redirect = isset($_GET['redirect']) ? $_GET['redirect'] === "1" : false; + +// if the current URL is in the form "/images/...", show the image +if (preg_match("/\/images\/(.*)$/", $_SERVER['REQUEST_URI'], $matches)) { + $image_path = $BASE_URL . $matches[1]; + displayImage($image_path, $REPO, $redirect); +} + +// fetch the list of images from GitHub $images = json_decode(curlGetContents($GITHUB_API_URL, $REPO)); // if the random query string parameter is set, pick a random image if (isset($_GET['random'])) { // get the image url $random_image_path = $images[array_rand($images)]->download_url; - header("Location: $random_image_path"); -} - -// if the current URL is in the form "/images/...", show the image -if (preg_match("/\/images\/(.*)$/", $_SERVER['REQUEST_URI'], $matches)) { - $image_path = $BASE_URL . $matches[1]; - header("Location: $image_path"); + displayImage($random_image_path, $REPO, $redirect); } ?>