<?php

session_start();
include 'db.php';
if ((isset($_SESSION['logged_in']) && isset($_SESSION['admin_id']))||(isset($_SESSION['user_id'])&&isset($_SESSION['name'])))
    { 
        if(isset($_SESSION['admin_id']) && isset($_SESSION['logged_in'])){
            $id = $_SESSION['admin_id'];
            $admin_id = $_SESSION['admin_id'];
        }else if(isset($_SESSION['user_id'])){
            $id = $_SESSION['user_id'];
            $admin_id = $_SESSION['admin_id'];
        }
        $name = $_POST['name'];
        $query = mysqli_query($con, "SELECT * FROM template_preview WHERE admin_fk='".$admin_id."'");
        if($query)
        {
            $row = mysqli_fetch_assoc($query);
            $templateName=$row['template_image'];
            $template="../assets/img/templates/".$templateName;
            $fontSize = $row['font_size'];
            $textAngle=0;
            $xCoordinate=$row['x_coordinate'];
            $yCoordinate=$row['y_coordinate'];
            $textColor=$row['font_color'];
            $colors= explode(",", $textColor);
            $fontfile=$row['font_type'];
            $fontpath = "../assets/fonts/".$fontfile.".ttf";
            if (isset($template) && isset($fontSize) && isset($xCoordinate) &&isset($yCoordinate)&& isset($textColor) &&isset($fontpath)) {
                $font = realpath($fontpath);
                // echo $font;
                $path = $templateName;
                $ext = pathinfo($path, PATHINFO_EXTENSION);
                
                if($ext=='jpg' || $ext=='jpeg')
                {
                    $image = imagecreatefromjpeg($template);
                }else if($ext=='png'){
                    $image = imagecreatefrompng($template);
                }
                
                $color = imagecolorallocate($image, $colors[0], $colors[1], $colors[2]);
                imagettftext($image, $fontSize, $textAngle, $xCoordinate, $yCoordinate, $color, $font, $name);
                $file = $name."_".$id;
                // imagejpeg($image, "../assets/img/certificates/" . $file . ".jpg");
                // imagejpeg($image);
                // imagedestroy($image);
                // echo $image;

                ob_start(); // Let's start output buffering.
                imagejpeg($image); //This will normally output the image, but because of ob_start(), it won't.
                $contents = ob_get_contents(); //Instead, output above is saved to $contents
                ob_end_clean(); //End the output buffer.

                $base64 = base64_encode($contents);


                // $_SESSION['certificate'] = "../assets/img/certificates/" . $file . ".jpg";
                // if(isset($_SESSION['certificate'])){
                echo(json_encode(array('status'=>'success','result' => $base64,'filename' => $file)));
                     
                // }

            }else{
                echo(json_encode(array('status'=>'error','result' => 'Styling variables not given')));
            }

        }else{
            echo(json_encode(array('status'=>'error','result' => 'Upload the template first')));
        }      
        
}
else
{
	echo(json_encode(array('status'=>'error','result' => 'Login to continue')));
}




?>