<?php
/**
 * 二维码生成 - 使用第三方API
 */
$text = isset($_GET['text']) ? trim($_GET['text']) : '';

if (empty($text)) {
    header('HTTP/1.1 400 Bad Request');
    echo 'Missing text parameter';
    exit;
}

$size = isset($_GET['size']) ? intval($_GET['size']) : 200;

// 使用goqr.me API生成二维码
$apiUrl = 'https://api.qrserver.com/v1/create-qr-code/?size=' . $size . 'x' . $size . '&data=' . urlencode($text);

// 获取并输出图片
$context = stream_context_create([
    'http' => [
        'timeout' => 10,
        'user_agent' => 'Mozilla/5.0'
    ]
]);

$imageData = @file_get_contents($apiUrl, false, $context);

if ($imageData !== false && !empty($imageData)) {
    header('Content-Type: image/png');
    echo $imageData;
} else {
    // 备用：使用HTTP QR code API
    $backupUrl = 'http://qr.liantu.com/api.php?text=' . urlencode($text) . '&w=' . $size;
    $backupData = @file_get_contents($backupUrl, false, $context);
    if ($backupData !== false) {
        header('Content-Type: image/png');
        echo $backupData;
    } else {
        header('HTTP/1.1 500 Server Error');
        echo 'Failed to generate QR code';
    }
}
?>