diff --git a/img.php b/img.php new file mode 100644 index 0000000..bea60b4 --- /dev/null +++ b/img.php @@ -0,0 +1,69 @@ + 'image/gif', + 'jpeg' => 'image/jpeg', + 'jpg' => 'image/jpeg', + 'jpe' => 'image/jpeg', + 'png' => 'image/png', + 'webp' => 'image/webp', + ); + $type = $types[$ext] ? $types[$ext] : 'image/jpeg'; + + header("Content-type: ".$type); + + // 缓存图片 + $cacheDir = 'cache'; // 缓存文件夹 + + // 根据被缓存的网址创建文件夹 + $parsedUrl = parse_url($url); + $domain = $parsedUrl['host']; + $cacheSubDir = $cacheDir . '/' . $domain; + if (!file_exists($cacheSubDir)) { + mkdir($cacheSubDir, 0777, true); // 创建缓存子文件夹 + } + + // 获取原始图片名称 + $imageName = basename($url); + + $cachedImagePath = $cacheSubDir . '/' . $imageName; // 缓存图片路径 + + if (file_exists($cachedImagePath)) { + // 返回缓存图片 + readfile($cachedImagePath); + } else { + // 保存原始图片内容到缓存文件夹 + file_put_contents($cachedImagePath, $data); + + // 输出原始图片 + echo $data; + } +} +?> \ No newline at end of file