Skip to content

Instantly share code, notes, and snippets.

@leeirvinekezzico
Created November 29, 2025 21:38
Show Gist options
  • Select an option

  • Save leeirvinekezzico/891562365344b273e3508a7c626dfe83 to your computer and use it in GitHub Desktop.

Select an option

Save leeirvinekezzico/891562365344b273e3508a7c626dfe83 to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__ . '/_db.php';
// Get puzzle_id from URL parameter
if (!isset($_GET['id'])) {
http_response_code(400);
exit('Missing puzzle ID');
}
$puzzle_id = $_GET['id'];
// Fetch the puzzle from database
try {
$stmt = $pdo->prepare("SELECT answer FROM puzzles WHERE puzzle_id = :puzzle_id AND solved_at IS NULL");
$stmt->bindParam(':puzzle_id', $puzzle_id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) {
http_response_code(404);
exit('Puzzle not found or expired');
}
$answer = (int)$row['answer'];
} catch (PDOException $e) {
http_response_code(500);
exit('Database error');
}
// Generate the math problem from the answer
// We need to reverse-engineer the numbers from the stored answer
// Since we stored just the answer, we'll create a random combination that equals it
$num1 = rand(1, min(15, $answer - 1));
$num2 = $answer - $num1;
// Create SVG image instead of using GD (which requires extension)
$width = 200;
$height = 60;
// Add some visual complexity to make OCR harder
$noise_elements = '';
for ($i = 0; $i < 5; $i++) {
$x1 = rand(0, $width);
$y1 = rand(0, $height);
$x2 = rand(0, $width);
$y2 = rand(0, $height);
$noise_elements .= "<line x1='$x1' y1='$y1' x2='$x2' y2='$y2' stroke='#ddd' stroke-width='1' opacity='0.3'/>";
}
// Text to display
$text = "What is {$num1} + {$num2}?";
// Create SVG
$svg = <<<SVG
<svg width="$width" height="$height" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="#f0f0f0"/>
$noise_elements
<text x="100" y="30" dominant-baseline="middle" text-anchor="middle"
font-family="Arial, sans-serif" font-size="18" fill="#333"
transform="rotate(-1 100 30)">$text</text>
<text x="101" y="31" dominant-baseline="middle" text-anchor="middle"
font-family="Arial, sans-serif" font-size="18" fill="#ccc"
transform="rotate(-1 101 31)">$text</text>
</svg>
SVG;
// Output SVG
header('Content-Type: image/svg+xml');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
echo $svg;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment