开源透明,所有处理逻辑在此,你也可以自己部署
<?php
// ηSpace 老照片修复 - PHP 代理
// 将上传图片转发给 Node.js Sharp 处理引擎
header('Access-Control-Allow-Origin: *');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
die('Method not allowed');
}
// Check file uploaded
if (!isset($_FILES['image']) || $_FILES['image']['error'] !== UPLOAD_ERR_OK) {
http_response_code(400);
die('请选择一张图片');
}
$file = $_FILES['image'];
// Validate type
$allowed = ['image/jpeg', 'image/png', 'image/webp'];
if (!in_array($file['type'], $allowed)) {
http_response_code(400);
die('仅支持 JPG、PNG、WebP 格式');
}
// Limit size (20MB)
if ($file['size'] > 20 * 1024 * 1024) {
http_response_code(400);
die('图片太大,请选择小于 20MB 的图片');
}
$tmpPath = $file['tmp_name'];
// Check if curl is available
if (!function_exists('curl_init')) {
http_response_code(500);
die('服务器缺少 curl 扩展');
}
// Forward to Node.js server
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'http://127.0.0.1:3001/photo-restore',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => ['image' => new CURLFile($tmpPath, $file['type'], $file['name'])],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_CONNECTTIMEOUT => 5,
]);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$error = curl_error($ch);
curl_close($ch);
if ($result === false || $httpCode !== 200) {
http_response_code(500);
die('处理失败: ' . ($error ?: '服务内部错误'));
}
// Return the processed image
header('Content-Type: ' . ($contentType ?: 'image/jpeg'));
header('Content-Length: ' . strlen($result));
header('Cache-Control: no-cache');
echo $result;