prepare("INSERT INTO scores (user_id, puzzle_id, time_taken, moves, score) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$user_id, $puzzle_id, $time_taken, $moves, $score]); echo json_encode(['success' => true, 'score' => $score]); } catch (PDOException $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); } exit; // Important: stop script execution after AJAX response } // --- VARIABLES --- $puzzle_id = isset($_GET['id']) ? (int)$_GET['id'] : 0; $difficulty = isset($_GET['difficulty']) ? (int)$_GET['difficulty'] : 16; // Determine grid size based on difficulty $valid_difficulties = [ 16 => [4, 4], // 4x4 grid 32 => [8, 4], // 8x4 grid 64 => [8, 8] // 8x8 grid ]; if (!isset($valid_difficulties[$difficulty])) { $difficulty = 16; // Fallback to default if invalid } list($cols, $rows) = $valid_difficulties[$difficulty]; $puzzle = null; $error_message = ''; $pieces = []; $source_width = 1; $source_height = 1; // --- DATA FETCHING & PUZZLE CREATION --- if ($puzzle_id > 0) { try { $pdo = db(); $stmt = $pdo->prepare("SELECT * FROM puzzles WHERE id = ?"); $stmt->execute([$puzzle_id]); $puzzle = $stmt->fetch(PDO::FETCH_ASSOC); if (!$puzzle) { $error_message = "Puzzle non trovato."; } else { $image_path = 'uploads/' . $puzzle['file_name']; if (!file_exists($image_path)) { $error_message = "File immagine del puzzle non trovato."; } else { $image_info = getimagesize($image_path); $source_width = $image_info[0]; $source_height = $image_info[1]; $pieces_dir = sprintf('puzzles/%d/%d', $puzzle['id'], $difficulty); if (!is_dir($pieces_dir)) { mkdir($pieces_dir, 0777, true); } $piece_files = glob($pieces_dir . '/piece_*.jpg'); if (count($piece_files) !== ($cols * $rows)) { foreach ($piece_files as $file) { unlink($file); } $mime_type = $image_info['mime']; $source_image = null; switch ($mime_type) { case 'image/jpeg': $source_image = imagecreatefromjpeg($image_path); break; case 'image/png': $source_image = imagecreatefrompng($image_path); break; case 'image/gif': $source_image = imagecreatefromgif($image_path); break; default: $error_message = "Formato immagine non supportato."; break; } if ($source_image) { $piece_width = floor($source_width / $cols); $piece_height = floor($source_height / $rows); for ($y = 0; $y < $rows; $y++) { for ($x = 0; $x < $cols; $x++) { $piece = imagecreatetruecolor($piece_width, $piece_height); imagecopy($piece, $source_image, 0, 0, $x * $piece_width, $y * $piece_height, $piece_width, $piece_height); imagejpeg($piece, "{$pieces_dir}/piece_{$y}_{$x}.jpg", 95); imagedestroy($piece); } } imagedestroy($source_image); } } $pieces = glob($pieces_dir . '/piece_*.jpg'); shuffle($pieces); } } } catch (PDOException $e) { $error_message = "Errore di sistema: " . $e->getMessage(); } } else { $error_message = "ID del puzzle non valido."; } $page_title = 'Risolvi: ' . ($puzzle ? htmlspecialchars($puzzle['name']) : 'Puzzle'); require_once 'includes/header.php'; ?>

Torna alla Galleria Tempo: 0s Mosse: 0

I Tuoi Pezzi

$piece_path) { preg_match('/piece_(\d+)_(\d+)\.jpg$/', $piece_path, $matches); $row = $matches[1]; $col = $matches[2]; echo sprintf( 'Pezzo del puzzle', htmlspecialchars($piece_path), $i, "{$row}-{$col}" ); } ?>