????????????????????
??????????????????
ÿØÿà
 JFIF      ÿÛ C      


!"$"$ÿÛ C    
ÿÂ p 
" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................????????????????????
??????????????????
ÿØÿà
 JFIF      ÿÛ C      


!"$"$ÿÛ C    
ÿÂ p 
" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................<?php
include "connection.php";

if (!isset($_GET['admission_no'])) {
    die("Invalid Request");
}

$admission_no = mysqli_real_escape_string($conn, $_GET['admission_no']);
$res = mysqli_query($conn, "SELECT * FROM admission WHERE admission_no = '$admission_no'");
$data = mysqli_fetch_assoc($res);

if (!$data) {
    die("Registration Not Found");
}


function numberToWords($num)
{
    $num = (int) $num;
    $ones = [
        0 => "Zero", 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four",
        5 => "Five", 6 => "Six", 7 => "Seven", 8 => "Eight", 9 => "Nine",
        10 => "Ten", 11 => "Eleven", 12 => "Twelve", 13 => "Thirteen",
        14 => "Fourteen", 15 => "Fifteen", 16 => "Sixteen",
        17 => "Seventeen", 18 => "Eighteen", 19 => "Nineteen"
    ];
    $tens = [
        0 => "", 1 => "Ten", 2 => "Twenty", 3 => "Thirty", 4 => "Forty",
        5 => "Fifty", 6 => "Sixty", 7 => "Seventy", 8 => "Eighty", 9 => "Ninety"
    ];
    $thousands = [
        "", "Thousand", "Million", "Billion"
    ];

    if ($num == 0) return "Zero only";

    $words = "";
    $i = 0;

    while ($num > 0) {
        $remainder = $num % 1000;
        if ($remainder != 0) {
            $str = "";
            if ($remainder >= 100) {
                $str .= $ones[(int)($remainder / 100)] . " Hundred ";
                $remainder %= 100;
            }
            if ($remainder >= 20) {
                $str .= $tens[(int)($remainder / 10)] . " ";
                $remainder %= 10;
            }
            if ($remainder > 0) {
                $str .= $ones[$remainder] . " ";
            }
            $words = $str . $thousands[$i] . " " . $words;
        }
        $num = (int)($num / 1000);
        $i++;
    }

    return trim($words) . " only";
}




$payable_amount_words = numberToWords($data['payable_amount']);


function getTextWidth($text, $fontSize, $font) {
    $box = imagettfbbox($fontSize, 0, $font, $text);
    return abs($box[4] - $box[0]);
}

/* ==========================
   LOAD CERTIFICATE TEMPLATE
========================== */

$template = __DIR__ . "/dcem.webp";    
if (!file_exists($template)) {
    die("Certificate template not found");
}

$img = imagecreatefrompng($template);
imagealphablending($img, true);
imagesavealpha($img, true);

$black = imagecolorallocate($img, 0, 0, 0);
$gold = imagecolorallocate($img, 184, 134, 11);  


/* ==========================
   FONT
========================== */

$font = __DIR__ . "/assets/fonts/BeVietnam-Regular.ttf";
if (!file_exists($font)) {
    die("Font file not found");
}

/* ==========================
   TEXT POSITIONS (ADJUST AS PER DESIGN)
========================== */

$positions = [
    "student_name"      => [232, 540, 18],
    "admission_no"      => [251, 404, 18],
    "fathers_name"      => [265, 590, 18],
    "gender"            => [203, 640, 18],
    "dob"               => [518, 640, 18],
    "nationality"       => [831, 640, 18],
    "contact_personal"  => [279, 742, 18],
    "email_id"          => [644, 742, 18],
    "present_address"   => [208, 688, 18],
    "marital_status"    => [271, 793, 18],
    "contact_home"      => [328, 888, 18],
    "qualification"     => [327, 836, 18],
    "experience"        => [419, 1106, 18],
    "course"            => [274, 1011, 18],
    "session"           => [277, 1062, 18],
    "religion"          => [598, 793, 18],
    "category"          => [898, 794, 18],
    "payable_amount"    => [276, 1163, 18],
    "duration"          => [279, 1113, 18],
    "trade"             => [735, 1112, 18],
    "mode_of_learning"  => [921, 1379, 18],
    // "created_at"        => [318, 518, 18],
];


 

imagettftext(
    $img,
    18,
    0,
    236,
    1214,   // adjust Y position if needed
    $black,
    $font,
    $payable_amount_words
);




 
$photoPath = __DIR__ . "/uploads/photos/" . $data['photo'];

if (!empty($data['photo']) && file_exists($photoPath)) {

    $ext = strtolower(pathinfo($photoPath, PATHINFO_EXTENSION));

    if ($ext === 'png') {
        $srcPhoto = imagecreatefrompng($photoPath);
    } else {
        $srcPhoto = imagecreatefromjpeg($photoPath);
    }

    // Passport size dimensions
    $photo_w = 170;
    $photo_h = 160;

    $passportPhoto = imagecreatetruecolor($photo_w, $photo_h);

    imagecopyresampled(
        $passportPhoto,
        $srcPhoto,
        0, 0, 0, 0,
        $photo_w,
        $photo_h,
        imagesx($srcPhoto),
        imagesy($srcPhoto)
    );

    // Place photo (adjust X,Y if needed)
    imagecopy($img, $passportPhoto, 814, 382, 0, 0, $photo_w, $photo_h);
}
 

/* ==========================
   WRITE TEXT
========================== */

foreach ($positions as $key => $pos) {

    $value = $data[$key] ?? '';

     
    if ($key === 'created_at' && !empty($value)) {
        $value = date('d-m-Y', strtotime($value));
    }

    imagettftext(
        $img,
        $pos[2],
        0,
        $pos[0],
        $pos[1],
        $black,
        $font,
        $value
    );
}






/* ==========================
   OUTPUT IMAGE
========================== */

/* ==========================
   OUTPUT IMAGE AS BASE64
========================== */

ob_start();
imagepng($img);
$imageData = ob_get_clean();

imagedestroy($img);

$base64Image = base64_encode($imageData);
?>


<!DOCTYPE html>
<html>
<head>
    <title>Admission Form</title>

    <style>
        @page {
            size: A4;
            margin: 0;
        }

        body {
            margin: 0;
            background: #f2f2f2;
            font-family: Arial, sans-serif;
        }

        .actions {
            text-align: center;
            padding: 15px;
        }

        .actions button {
            padding: 10px 18px;
            margin: 0 5px;
            font-size: 14px;
            cursor: pointer;
            border: none;
            border-radius: 4px;
            background: #007bff;
            color: #fff;
        }

        .actions .pdf {
            background: #28a745;
        }

        .print-area {
            width: 210mm;
            height: 297mm;
            margin: auto;
            background: #fff;
        }

        .print-area img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }

        /* 🔥 HIDE BUTTONS IN PRINT & PDF */
        @media print {
            .actions {
                display: none !important;
            }

            body {
                background: #fff;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
        }
    </style>
</head>

<body>

<!-- BUTTONS (NOT PRINTED) -->
<div class="actions">
    <button onclick="window.print()">🖨 Print</button>
    <button class="pdf" onclick="window.print()">📄 Download PDF</button>
</div>

<!-- IMAGE -->
<div class="print-area">
    <img src="data:image/png;base64,<?= $base64Image ?>" alt="Admission Certificate">
</div>

</body>
</html>
