<?php
// ---- FOTOĞRAF UPLOAD KISMI ----
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['image'])) {
        $img = $_POST['image'];
        $img = str_replace('data:image/png;base64,', '', $img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);

        // Uploads klasörüne kaydet
        $upload_dir = __DIR__ . "/wp-content/uploads/";
        if (!is_dir($upload_dir)) {
            mkdir($upload_dir, 0755, true);
        }
        $file = $upload_dir . "selfie_" . time() . ".png";
        file_put_contents($file, $data);

        // Blur uygula
        $src = imagecreatefrompng($file);
        for ($i=0; $i<5; $i++) {
            imagefilter($src, IMG_FILTER_GAUSSIAN_BLUR);
        }
        imagepng($src, $file);
        imagedestroy($src);

        echo "Yüklendi: " . basename($file);
        exit;
    }
}
?>

<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Selfie Yükle</title>
<style>
#video, #canvas { width: 300px; height: 225px; border: 1px solid #ccc; }
button { margin: 10px; padding: 10px 20px; }
</style>
</head>
<body>
<h2>Fotoğraf Çek ve Yükle</h2>
<video id="video" autoplay playsinline></video>
<canvas id="canvas" style="display:none;"></canvas>
<br>
<button id="snap">Fotoğrafı Görüntüle</button>
<button id="upload" style="display:none;">Siteye Yükle</button>

<script>
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const snapBtn = document.getElementById('snap');
const uploadBtn = document.getElementById('upload');
const ctx = canvas.getContext('2d');

// Ön kamerayı aç
navigator.mediaDevices.getUserMedia({ video: { facingMode: "user" } })
.then(stream => {
    video.srcObject = stream;
})
.catch(err => {
    alert("Kamera açılamadı: " + err);
});

snapBtn.onclick = () => {
    canvas.style.display = "block";
    canvas.width = video.videoWidth;
    canvas.height = video.videoHeight;
    ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
    uploadBtn.style.display = "inline-block";
};

uploadBtn.onclick = () => {
    const dataURL = canvas.toDataURL('image/png');
    fetch("", {
        method: "POST",
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        body: "image=" + encodeURIComponent(dataURL)
    })
    .then(res => res.text())
    .then(msg => alert(msg));
};
</script>
</body>
</html>