<?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');

use chillerlan\QRCode\Output\{
       QRCodeOutputException,
       QRImage
};
use chillerlan\QRCode\{
       QRCode,
       QROptions
};

include_once __DIR__ . DIRECTORY_SEPARATOR . 'Qrimagewithlogo.php';

//use function imagecopyresampled,
//             imagecreatefrompng,
//             imagesx,
//             imagesy,
//             is_file,
//             is_readable;

class LogoOptions extends QROptions {

       // size in QR modules, multiply with QROptions::$scale for pixel size
       protected $logoSpaceWidth;
       protected $logoSpaceHeight;

}

/**
 * Description of Qr
 *
 * @author andhy
 */
class Qr2 {

       public function with_logo($identity, $file_name = null) {
//        if (is_file(QRPATH . $identity . '.png')) {
//            return true; // nothing to do
//        }
//ambil logo
              $logopath = dirname(UPLOAD_PATH) . DIRECTORY_SEPARATOR . 'qr_icon.png';
              $options = new LogoOptions;

              $options->version = 7;
              $options->eccLevel = QRCode::ECC_H;
              $options->imageBase64 = false;
              $options->logoSpaceWidth = 15;
              $options->logoSpaceHeight = 15;
              $options->scale = 5;
              $options->imageTransparent = false;
              if (!$file_name) {
                     $file_name = $identity;
              }
              $qr = new Qrimagewithlogo($options, (new QRCode($options))->getMatrix($identity));
              // dump the output, with an additional logo
              $qr->dump(QRPATH . $file_name . '.png', $logopath);
              return QRPATH . $file_name . '.png';
       }

       public function with_name($identity, $name = null) {
              $QRLogo = imagecreatefrompng(QRPATH . $identity . '.png');
              $border = imagecreatefrompng(realpath(dirname(UPLOAD_PATH) . DIRECTORY_SEPARATOR . 'qr_name_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);
              }
              imagepng($border, QRPATH . $identity . '_share.png', 9);
       }

       public function with_border($identity, $name = null, $optic = null, $phone = null) {
//         if (is_file(QRPATH . $identity . '_share.png')) {
//            return true; // nothing to do
//        }
              $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);
       }

}
