<?php

namespace frontend\extensions;

use Yii;

class VoucherPrime {
	
	public static function generatePDF($nome, $cognome, $cinema, $indirizzo, $data, $c) {

        $img_w = 2480;
        $img_h = 3508;

        $code = $c;

        $font = 'TIMSans-Regular.ttf';

        // $x = 320;
        // $fontSize = 12;
        // $fontSizeCodice = 18;
        // $fontSizeIndirizzo = 70;


        // $image = imagecreatefromjpeg(Yii::getAlias('@webroot/resources/vouchersmall.jpg'));

        // $blue = imagecolorallocate($image, 0, 69, 136);
        // // $grey = imagecolorallocate($image, 65, 65, 65);

        // imagettftext($image, $fontSize, 0, $x, 335, $blue, $font, $nome);
        // imagettftext($image, $fontSize, 0, $x, 382, $blue, $font, $cognome);
        // imagettftext($image, $fontSize, 0, $x, 432, $blue, $font, $cinema);        
        // imagettftext($image, $fontSize, 0, $x, 482, $blue, $font, $data);        
        

        // $tb = imagettfbbox($fontSizeCodice, 0, $font, $code);
        // $x = ceil( 526 - ($tb[2] / 2) ); // lower left X coordinate for text
        // imagettftext($image, $fontSizeCodice, 0, $x, 586, $blue, $font, $code);



        $x = 175;
        $y = 583;
        $fontSize = 12;
        $fontSizeCodice = 18;
        $fontSizeIndirizzo = 70;

        $image = imagecreatefromjpeg(Yii::getAlias('@webroot/resources/voucher-timprime.jpg'));

        $blue = imagecolorallocate($image, 0, 69, 136);
        // $grey = imagecolorallocate($image, 65, 65, 65);

        imagettftext($image, $fontSize, 0, $x, $y, $blue, $font, $nome);
        imagettftext($image, $fontSize, 0, $x, $y + 49, $blue, $font, $cognome);
        imagettftext($image, $fontSize, 0, $x, $y + 149, $blue, $font, $data);  


        // cinema font size
        $tb = imagettfbbox($fontSize, 0, $font, $cinema);
        if ($tb[2] > 300)
            $fontSize = $fontSize - 2;
        $tb = imagettfbbox($fontSize, 0, $font, $cinema);
        if ($tb[2] > 300)
            $fontSize = $fontSize - 2;
        $tb = imagettfbbox($fontSize, 0, $font, $cinema);
        if ($tb[2] > 300)
            $fontSize = $fontSize - 1;
        // var_dump($tb); exit;
        imagettftext($image, $fontSize, 0, $x, $y + 99, $blue, $font, $cinema);      

        $tb = imagettfbbox($fontSizeCodice, 0, $font, $code);
        $x = ceil( 385 - ($tb[2] / 2) ); // lower left X coordinate for text
        imagettftext($image, $fontSizeCodice, 0, $x, 880, $blue, $font, $code);


        // header('Content-Type: image/jpeg');

        
        //Get jpeg image contents
        ob_start();
        imagejpeg($image, NULL, 100);
        $image_obj = ob_get_contents();
        imagedestroy($image);
        ob_end_clean();

        //change the JPEG header to 300 pixels per inch
        // $image_obj = substr_replace($image_obj, pack("Cnn", 0x01, 300, 300), 13, 5);
//        file_put_contents(Yii::getAlias("@webroot/voucher/jpeg/$code.jpg"), $image_obj);
//
//        exec('convert '. Yii::getAlias("@webroot/voucher/jpeg/$code.jpg") .' '. Yii::getAlias("@webroot/voucher/pdf/$code.pdf"));
        
        file_put_contents(Yii::getAlias("@webroot/voucher_natale2016/jpeg/$code.jpg"), $image_obj);

        exec('convert '. Yii::getAlias("@webroot/voucher_natale2016/jpeg/$code.jpg") .' '. Yii::getAlias("@webroot/voucher_natale2016/pdf/$code.pdf"));
    }

}

?>