<?php
// am_a4.php
// A4 webpage

 $contis="<html><head><style>
 @media print {
    body{
        width: 21cm;
        height: 29.7cm;
        margin: 30mm 45mm 30mm 45mm; 
        /* change the margins as you want them to be. */
   } 
}
    body{
        width: 21cm;
        height: 29.7cm;
        margin: 0mm 0mm 0mm 0mm; 
        /* change the margins as you want them to be. */
   } 
</style></head><body";
 $lcontis="<html><head><style>
 @media print {
    body{
        height: 21cm;
        width: 29.7cm;
        margin: 45mm 30mm 45mm 30mm; 
        /* change the margins as you want them to be. */
   } 
    body{
        height: 21cm;
        width: 29.7cm;
        margin: 0mm 0mm 0mm 0mm; 
        /* change the margins as you want them to be. */
   } 
}
</style></head><body";

 if (isset($_GET['clear'])) {
   if (file_exists("a4_content.txt")) {
   if (abs(filesize("a4_content.txt") - $_GET['clear']) < 9) {
   unlink("a4_content.txt");
   }
   }
 } else if (file_exists("a4_content.txt")) {
   $bpis=" background-position:left top;";
   $fis=file_get_contents("a4_content.txt");
   $backpos=explode(":", explode("/",$fis)[0])[1];
   if (strpos(explode(":", explode("/",$fis)[0])[1], "E") !== false) { $contis=$lcontis; }
   if (strpos($backpos, 'imag') === false) { // != 'left top') {  // left top=0 center top=1 ... I=1 M=2 A=4 G=8 
   if (strpos($backpos, 'Imag') !== false) { // == 'center top') {
     $bpis=" background-position:center top;";
   } else if (strpos($backpos, 'iMag') !== false) { // == 'center right') {
     $bpis=" background-position:center right;";
   } else if (strpos($backpos, 'IMag') !== false) { // == 'left center') {
     $bpis=" background-position:left center;";
   } else if (strpos($backpos, 'imAg') !== false) { // == 'center center') {
     $bpis=" background-position:center center;";
   } else if (strpos($backpos, 'ImAg') !== false) { // == 'right center') {
     $bpis=" background-position:right center;";
   } else if (strpos($backpos, 'iMAg') !== false) { // == 'left bottom') {
     $bpis=" background-position:left bottom;";
   } else if (strpos($backpos, 'IMAg') !== false) { // == 'center bottom') {
     $bpis=" background-position:center bottom;";
   } else if (strpos($backpos, 'imaG') !== false) { // == 'right bottom') {
     $bpis=" background-position:right bottom;";
   }
   }   
   
   $contis.=" style='background-image:URL(" . str_replace(":" . $backpos . "/", ":image/", trim($fis)) . "); background-repeat:no-repeat; background-size:contain; " . $bpis . "'></body></html>";
   if ($fis != trim($fis)) {
     unlink("a4_content.txt");
   } else {
     file_put_contents("a4_content.txt", ($fis . " "));
   }
 } else if (isset($_POST['a4_content'])) {
   file_put_contents("a4_content.txt", str_replace(" ","+",urldecode($_POST['a4_content'])));
   $gps="window.open(\"https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fwww.rjmprogramming.com.au%2FPHP%2Fam_a4.php&tab=desktop\",\"_blank\",\"top=50,left=50,height=900,width=1300\");";
   $contis.=" onload='" . $gps . "'></body></html>";
 } else {
   $contis.="></body></html>";
 }
 
 echo $contis;
 exit;
?>