| Server IP : 172.67.187.206 / Your IP : 172.71.28.155 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : E:/Inetpub/www/training/user/ |
Upload File : |
<?php
include("../session.php");
if ($role != 'user') {
header("Location: ../index.php");
exit;
}
include("../db.php");
// ตรวจสอบว่ามีการส่ง subject ที่ต้องการหรือไม่
if (!isset($_GET['subject'])) {
header("Location: dashboard.php");
exit;
}
// ถอดรหัส subject ID
$subject_id = base64_decode($_GET['subject']);
// ดึงข้อมูลผู้ใช้
$user_id = $_SESSION['user_id'];
$user_query = mysqli_query($conn, "SELECT * FROM users WHERE id = $user_id");
$user = mysqli_fetch_assoc($user_query);
// ดึงข้อมูลวิชาและกลุ่ม
$subject_query = mysqli_query($conn, "SELECT s.*, g.group_name
FROM subjects s
LEFT JOIN groups g ON s.group_id = g.user_id
WHERE s.id = $subject_id");
$subject = mysqli_fetch_assoc($subject_query);
// ตรวจสอบว่าผู้ใช้ได้สอบผ่านวิชานี้จริงหรือไม่
$result_query = mysqli_query($conn, "SELECT * FROM exam_results
WHERE user_id = $user_id
AND subject_id = $subject_id
AND passed = 1
ORDER BY test_date DESC LIMIT 1");
if (!$result = mysqli_fetch_assoc($result_query)) {
header("Location: dashboard.php");
exit;
}
// ฟังก์ชันแปลงเลขอารบิกเป็นเลขไทย
function thainumDigit($num) {
return str_replace(
array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'),
array('๐', '๑', '๒', '๓', '๔', '๕', '๖', '๗', '๘', '๙'),
$num
);
}
// ฟังก์ชันแปลงเดือนเป็นภาษาไทย
function thaiMonth($month) {
$thai_month = array(
1 => 'มกราคม', 2 => 'กุมภาพันธ์', 3 => 'มีนาคม', 4 => 'เมษายน',
5 => 'พฤษภาคม', 6 => 'มิถุนายน', 7 => 'กรกฎาคม', 8 => 'สิงหาคม',
9 => 'กันยายน', 10 => 'ตุลาคม', 11 => 'พฤศจิกายน', 12 => 'ธันวาคม'
);
return $thai_month[$month];
}
// ตรวจสอบว่า FPDF และ font ภาษาไทยมีในระบบหรือไม่
$font_path = '../assets/fonts/';
if (!file_exists('../pdf1.84/fpdf.php')) {
die("ไม่พบไฟล์ FPDF กรุณาติดตั้งก่อนใช้งาน");
}
// กำหนด Content-Type เป็น PDF
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="certificate_' . $subject['name'] . '.pdf"');
// เรียกใช้ FPDF
require('../pdf1.84/fpdf.php');
// สร้าง PDF
$pdf = new FPDF('L', 'mm', 'A4');
$pdf->SetAutoPageBreak(false);
$pdf->AddPage();
// เพิ่มฟอนต์ภาษาไทย
$pdf->AddFont('sarabun', '', 'THSarabun.php');
$pdf->AddFont('sarabunb', '', 'THSarabunB.php');
// กำหนดรูปแบบเกียรติบัตรตามกลุ่ม
$group_id = $subject['group_id'];
$background_image = '../img/hero68.png'; // รูปพื้นหลังเริ่มต้น
$border_color = array(26, 35, 126); // สีกรอบเริ่มต้น (RGB)
$title_color = array(26, 35, 126); // สีหัวเกียรติบัตร
$subject_color = array(150, 0, 0); // สีชื่อวิชา
$font_size_title = 48; // ขนาดฟอนต์หัวเกียรติบัตร
$font_size_name = 46; // ขนาดฟอนต์ชื่อผู้ได้รับ
// ปรับแต่งตามกลุ่ม
switch ($group_id) {
case 3: // กลุ่มอำนวยการ
$background_image = file_exists('../img/administration.png') ? '../img/administration.png' : $background_image;
$border_color = array(0, 102, 204); // สีน้ำเงิน
$title_color = array(50, 50, 50);
$subject_color = array(50, 50, 50);
$font_size_title = 50;
$font_size_name = 46;
break;
case 4: // กลุ่มบริหารงานบุคคล
$background_image = file_exists('../img/personal.png') ? '../img/personal.png' : $background_image;
$border_color = array(204, 0, 102); // สีชมพู
$title_color = array(50, 50, 50);
$subject_color = array(50, 50, 50);
$font_size_title = 48;
$font_size_name = 46;
break;
case 5: // กลุ่มนโยบายและแผน
$background_image = file_exists('../img/plan.png') ? '../img/plan.png' : $background_image;
$border_color = array(0, 102, 204); // สีน้ำเงิน
$title_color = array(0, 0, 204);
$subject_color = array(204, 0, 0);
$font_size_title = 50;
$font_size_name = 46;
break;
case 6: // กลุ่มส่งเสริมการจัดการศึกษา
$background_image = file_exists('../img/promotion.png') ? '../img/promotion.png' : $background_image;
$border_color = array(255, 153, 0); // สีส้ม
$title_color = array(50, 50, 50);
$subject_color = array(50, 50, 50);
$font_size_title = 46;
$font_size_name = 46;
break;
case 7: // กลุ่มนิเทศ ติดตามและประเมินผลการจัดการศึกษา
$background_image = file_exists('../img/supervision.png') ? '../img/supervision.png' : $background_image;
$border_color = array(0, 153, 0); // สีเขียว
$title_color = array(50, 50, 50);
$subject_color = array(50, 50, 50);
$font_size_title = 46;
$font_size_name = 46;
break;
case 8: // กลุ่มบริหารงานการเงินและทรัพย์สิน
$background_image = file_exists('../img/finance.png') ? '../img/finance.png' : $background_image;
$border_color = array(102, 51, 0); // สีน้ำตาล
$title_color = array(50, 50, 50);
$subject_color = array(50, 50, 50);
$font_size_title = 48;
$font_size_name = 46;
break;
case 9: // หน่วยตรวจสอบภายใน
$background_image = file_exists('../img/audit.png') ? '../img/audit.png' : $background_image;
$border_color = array(153, 0, 0); // สีแดง
$title_color = array(50, 50, 50);
$subject_color = array(50, 50, 50);
$font_size_title = 48;
$font_size_name = 46;
break;
case 10: // กลุ่มพัฒนาครูและบุคลากรทางการศึกษา
$background_image = file_exists('../img/develop.png') ? '../img/develop.png' : $background_image;
$border_color = array(0, 153, 153); // สีเขียวอมฟ้า
$title_color = array(50, 50, 50);
$subject_color = array(50, 50, 50);
$font_size_title = 50;
$font_size_name = 46;
break;
case 11: // กลุ่มกฎหมายและคดี
$background_image = file_exists('../img/law.png') ? '../img/law.png' : $background_image;
$border_color = array(51, 51, 51); // สีเทา
$title_color = array(50, 50, 50);
$subject_color = array(50, 50, 50);
$font_size_title = 48;
$font_size_name = 46;
break;
case 12: // กลุ่ม ICT
$background_image = file_exists('../img/ict.png') ? '../img/ict.png' : $background_image;
$border_color = array(102, 0, 204); // สีม่วง
$title_color = array(50, 50, 50);
$subject_color = array(50, 50, 50);
$font_size_title = 52;
$font_size_name = 46;
break;
default: // กลุ่มอื่น ๆ
// ใช้ค่าเริ่มต้น
break;
}
// ลองใช้รูปภาพพื้นหลัง
if (file_exists($background_image)) {
$pdf->Image($background_image, 0, 0, 297, 210);
} else {
// วาดกรอบเกียรติบัตร
$pdf->SetLineWidth(1);
$pdf->SetDrawColor($border_color[0], $border_color[1], $border_color[2]);
$pdf->Rect(10, 10, 277, 190);
// กรอบด้านใน
$pdf->SetLineWidth(0.5);
$pdf->SetDrawColor($border_color[0], $border_color[1], $border_color[2]);
$pdf->Rect(15, 15, 267, 180);
}
// สร้างเลขที่เกียรติบัตร
$certificate_number = date('Y') . '-' . str_pad($user_id, 4, '0', STR_PAD_LEFT) . '-' . str_pad($subject_id, 4, '0', STR_PAD_LEFT);
$thai_certificate_number = 'เลขที่ พคศ.สพม.รบ. ' . thainumDigit($result['id']) . '/' . thainumDigit(date('Y') + 543);
// เพิ่มเลขที่เกียรติบัตร
$pdf->SetXY(222, 195);
$pdf->SetFont('sarabun', '', 14);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(65, 10, iconv('utf-8', 'cp874', $thai_certificate_number), 0, 1, 'R');
// ชื่อผู้ได้รับ
$pdf->SetFont('sarabunb', '', $font_size_name);
$pdf->SetTextColor($title_color[0], $title_color[1], $title_color[2]);
$pdf->SetXY(0, 77);
$pdf->Cell(297, 15, iconv('utf-8', 'cp874', $user['u_name']), 0, 1, 'C');
// ชื่อวิชา
$pdf->SetFont('sarabunb', '', 28);
$pdf->SetTextColor($subject_color[0], $subject_color[1], $subject_color[2]);
$pdf->SetXY(0, 108);
$pdf->Cell(297, 15, iconv('utf-8', 'cp874', $subject['name']), 0, 1, 'C');
// คะแนนที่ได้
$pdf->SetFont('sarabunb', '', 28);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetXY(53, 119);
$pdf->Cell(297, 15, iconv('utf-8', 'cp874', thainumDigit($result['percentage'])), 0, 1, 'C');
// วันที่สอบ
$test_date = strtotime($result['test_date']);
$thai_date = thainumDigit(date('j', $test_date)) . ' ' . thaiMonth(date('n', $test_date)) . ' พ.ศ. ' . thainumDigit(date('Y', $test_date) + 543);
$pdf->SetFont('sarabunb', '', 24);
$pdf->SetXY(0, 147);
$pdf->Cell(297, 10, iconv('utf-8', 'cp874', 'ให้ไว้ ณ วันที่ ' . $thai_date), 0, 1, 'C');
// แสดงผล PDF
$pdf->Output('I', 'certificate_' . $subject['name'] . '.pdf');
exit;
?>