<?php

/*
 * *************************************************
 * Copyright (C) 2020 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

defined('BASEPATH') OR exit('No direct script access allowed');
include APPPATH . "third_party/phpqrcode/qrlib.php";

/**
 * Description of Qr
 *
 * @author andhy
 */
class Qr extends QRcode {

    public static function with_logo($identity) {
        if (is_file(QRPATH . $identity . '.png')) {
            return true; // nothing to do
        }
//ambil logo
        $logopath = realpath(dirname(UPLOAD_PATH) . DIRECTORY_SEPARATOR . 'qr_icon.png');
//simpan file qrcode
        QRcode::png($identity, QRPATH . $identity . '.png', QR_ECLEVEL_H, 12, 2);


// ambil file qrcode
        $QR = imagecreatefrompng(QRPATH . $identity . '.png');

// memulai menggambar logo dalam file qrcode
        $logo = imagecreatefrompng($logopath);

        imagecolortransparent($logo, imagecolorallocatealpha($logo, 0, 0, 0, 127));
        imagealphablending($logo, false);
        imagesavealpha($logo, true);

        $QR_width = imagesx($QR);
        $QR_height = imagesy($QR);

        $logo_width = imagesx($logo);
        $logo_height = imagesy($logo);

// Scale logo to fit in the QR Code
//        $logo_qr_width = $QR_width / 8;
//        $scale = $logo_width / $logo_qr_width;
//        $logo_qr_height = $logo_height / $scale;
        $logo_qr_width = 50;
        $logo_qr_height = 50;
//        imagecopyresampled($QR, $logo, $QR_width / 2.3, $QR_height / 2.3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        imagecopyresampled($QR, $logo, $QR_width / 2.3, $QR_height / 2.3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);

// Simpan kode QR lagi, dengan logo di atasnya
        imagepng($QR, QRPATH . $identity . '.png', 9);
    }

    public static function with_border($identity, $name = null, $optic = null, $phone = null) {
        $QRLogo = imagecreatefrompng(QRPATH . $identity . '.png');
        // memulai menggambar logo dalam file qrcode
        $border = imagecreatefrompng(realpath(dirname(UPLOAD_PATH) . DIRECTORY_SEPARATOR . 'qr_border.png'));
        imagecopyresampled($border, $QRLogo, 235, 150, 0, 0, 250, 250, imagesx($QRLogo), imagesy($QRLogo));
        $font = realpath(dirname(dirname(UPLOAD_PATH)) . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR . 'Roboto' . DIRECTORY_SEPARATOR . 'Roboto-Regular.ttf');
        if ($name) {
            imagettftext($border, 13, 0, 310, 460, imagecolorallocate($border, 255, 255, 255), $font, $name);
        }
        if ($optic) {
            imagettftext($border, 13, 0, 310, 490, imagecolorallocate($border, 255, 255, 255), $font, $optic);
        }
        if ($phone) {
            imagettftext($border, 13, 0, 310, 520, imagecolorallocate($border, 255, 255, 255), $font, $phone);
        }
//        if ($seat) {
//            imagettftext($border, 13, 0, 310, 550, imagecolorallocate($border, 255, 255, 255), $font, $seat);
//        }
//        if ($pax) {
//            imagettftext($border, 13, 0, 310, 580, imagecolorallocate($border, 255, 255, 255), $font, $pax);
//        }
        // Simpan kode QR lagi, dengan logo di atasnya
        imagepng($border, QRPATH . $identity . '_share.png', 9);
    }

}
