????????????????????
??????????????????
ÿØÿà
 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['id'])) {
    die("Invalid Request");
}

$id = intval($_GET['id']);
$res = mysqli_query($conn, "SELECT * FROM certificate WHERE id = $id");
$data = mysqli_fetch_assoc($res);

if (!$data) {
    die("Certificate Not Found");
}

function getTextWidth($text, $fontSize, $font) {
    $box = imagettfbbox($fontSize, 0, $font, $text);
    return abs($box[4] - $box[0]);
}

/* ==========================
   LOAD CERTIFICATE TEMPLATE
========================== */

$template = __DIR__ . "/certificate.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); // Dark Golden Yellow


/* ==========================
   FONT
========================== */

$font = __DIR__ . "/assets/fonts/ARIAL.ttf";
if (!file_exists($font)) {
    die("Font file not found");
}

/* ==========================
   TEXT POSITIONS (ADJUST AS PER DESIGN)
========================== */

$positions = [
    "name" => [1013, 957, 50],
    "fname" => [1013, 1065, 50],
    "course" => [1013, 1289, 50],
    "session"       => [1013, 1401, 50],
    "grade"       => [2369, 1401, 50],
    "issue"     => [257, 1725, 50],
    "certno"     => [2797, 820, 50],
     
];



$photoPath = __DIR__ . "/cms/uploads/certificates/" . $data['pic'];

if (!empty($data['pic']) && file_exists($photoPath)) {

    $ext = strtolower(pathinfo($photoPath, PATHINFO_EXTENSION));

    if ($ext === 'png') {
        $srcPhoto = imagecreatefrompng($photoPath);
    } else {
        $srcPhoto = imagecreatefromjpeg($photoPath);
    }

    // Passport size dimensions
    $photo_w = 430;
    $photo_h = 406;

    $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, 2797, 865, 0, 0, $photo_w, $photo_h);
}


// $studentName = strtoupper($data['name'] ?? '');

// $fontSize = 58;
// $startX   = 881;     
// $endX     = 2409;    
// $y        = 945;

// $textWidth = getTextWidth($studentName, $fontSize, $font);
// $availableWidth = $endX - $startX;

 
// if ($textWidth < $availableWidth) {
//     $x = $startX + ($availableWidth - $textWidth) / 2;
// } else {
//     $x = $startX;
// }

// imagettftext(
//     $img,
//     $fontSize,
//     0,
//     (int)$x,
//     $y,
//     $black,    
//     $font,
//     $studentName
// );







// $fName = strtoupper($data['fname'] ?? '');

// $fontSize = 58;
// $startX   = 877;     
// $endX     = 2465;    
// $y        = 1060;

// $textWidth = getTextWidth($fName, $fontSize, $font);
// $availableWidth = $endX - $startX;

 
// if ($textWidth < $availableWidth) {
//     $x = $startX + ($availableWidth - $textWidth) / 2;
// } else {
//     $x = $startX;
// }

// imagettftext(
//     $img,
//     $fontSize,
//     0,
//     (int)$x,
//     $y,
//     $black,    
//     $font,
//     $fName
// );




// $cName = strtoupper($data['course'] ?? '');

// $fontSize = 58;
// $startX   = 882;     
// $endX     = 2485;    
// $y        = 1286;

// $textWidth = getTextWidth($cName, $fontSize, $font);
// $availableWidth = $endX - $startX;

 
// if ($textWidth < $availableWidth) {
//     $x = $startX + ($availableWidth - $textWidth) / 2;
// } else {
//     $x = $startX;
// }

// imagettftext(
//     $img,
//     $fontSize,
//     0,
//     (int)$x,
//     $y,
//     $black,    
//     $font,
//     $cName
// );






 

/* ==========================
   WRITE TEXT
========================== */

foreach ($positions as $field => $pos) {
    $value = $data[$field] ?? '';
    imagettftext(
        $img,
        $pos[2],
        0,
        $pos[0],
        $pos[1],
        $black,
        $font,
        $value
    );
}

/* ==========================
   OUTPUT IMAGE
========================== */

header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
exit;
