<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require 'pdf/fpdf.php';

class PDF extends FPDF {

    const DPI = 100;
    const MM_IN_INCH = 25.4;
    const A4_HEIGHT = 57;
    const A4_WIDTH = 40;
    const MAX_WIDTH = 120;
    const MAX_HEIGHT = 100;

    function pixelsToMM($val) {
        return $val * self::MM_IN_INCH / self::DPI;
    }

    function resizeToFit($imgFilename) {
        list($width, $height) = getimagesize($imgFilename);
        $widthScale = self::MAX_WIDTH / $width;
        $heightScale = self::MAX_HEIGHT / $height;
        $scale = min($widthScale, $heightScale);
        return array(
            round($this->pixelsToMM($scale * $width)),
            round($this->pixelsToMM($scale * $height))
        );
    }

    function centreImage($img) {
        list($width, $height) = $this->resizeToFit($img);
        // you will probably want to swap the width/height
        // around depending on the page's orientation
        $this->Image(
                $img, (self::A4_HEIGHT - $width) / 2, (self::A4_WIDTH - $height) / 2, $width, $height
        );
    }

    function Header() {
        $queryDup = "select * from m_setting  order by master_id desc limit 1 ";
        if (DB::getInstance()->checkRows($queryDup)) {
            $res_lists = DB::getInstance()->query($queryDup);
            foreach ($res_lists->results() as $res_lists) :
                $school_name = $res_lists->schoo_name;
                $mottto = $res_lists->mottto;
                $tel = $res_lists->tel;
                $email = $res_lists->email;
                $location = $res_lists->location;
                $logo = $res_lists->logo_path;
            endforeach;
        }
        //$this->Image($logo, 10, 6, 30);
        $this->centreImage($logo);
        $this->Ln(10);
        $this->SetFont('Arial', 'B', 15);
        $this->SetTextColor(125, 233, 223);
        $this->MultiCell(0, 0, $school_name . " S.S.S", 0, 'C');
        $this->Ln(10);
        $this->SetTextColor(192, 189, 164);
        $this->SetFont('Arial', '', 10);
        $this->MultiCell(0, 0, ' >> ' . $mottto . ' << ', 0, 'C');
        $this->Ln(5);
        $this->MultiCell(0, 0, $location, 0, 'C');
        $this->Ln(5);
        $this->MultiCell(0, 0, $email, 0, 'C');
        $this->Ln(5);
        $this->MultiCell(0, 0, $tel, 0, 'C');
        $this->SetDrawColor(229, 229, 226);
        $this->SetLineWidth(2.0);
        $this->Line(20, 55, 210 - 20, 55);
        $this->Ln(15);
    }

    function Footer() {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
        $this->SetTextColor(192, 189, 164);
        
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/nb [ Generated by: ' . $_SESSION['school_user_full_name'] . " ]", 0, 0, 'C');
    
        $this->Ln(5);
    }

}

?>