<?php

  
/* start makeBGpic(000) program */
 
if(isset($_GET['fonts'])) 
{
    $fonts=$_GET['fonts'];
}
else
{
    //$fonts='ArialAlphaNumeric36pt';
    $fonts='ArialNova16pt';
}
$filemap="../lfm/$fonts.txt";
if(!file_exists($filemap))
{
    die("cannot find '$filemap' - aborting");
}
include($filemap);                    // font file map
$fontFile='../lfm/images/'.$fonts.'.png';    // input font file
if(!file_exists($fontFile))
{
    die("cannot find '$fontFile' - aborting");
}
if(isset($_GET['chap']))
{
    $chap=$_GET['chap'];
}
else
{
    die('makeBGpic.php requires chap=xxxx identifying the calling chapter - aborting!');
}
$chap = str_replace('.php','',$chap);
$imgFileName   = $chap.'_BG.png';     // output file name

// default values - can be overridden by parms passed
$negate        = 'y';
$colorizeRed   = '240';
$colorizeGreen = '240';
$colorizeBlue  = '240';
$rotateDegrees = '0';
$paddingTop    = '20';
$paddingBottom = '30';
$paddingLeft   = '16';
$paddingRight  = '16';
$charSpacing   = '3';
$maxWidth      = '200';
$uppercase     = 'n';

if(isset($_GET['negate']))
{
    $negate=$_GET['negate'];
}

if(isset($_GET['uppercase']))
{
    $uppercase=$_GET['uppercase'];
}

if(isset($_GET['maxWidth']))
{
    $maxWidth =$_GET['maxWidth'];
}

if(isset($_GET['charSpacing']))
{
    $charSpacing=$_GET['charSpacing'];
}

if(isset($_GET['paddingRight']))
{
    $paddingRight=$_GET['paddingRight'];
}

if(isset($_GET['paddingLeft']))
{
    $paddingLeft=$_GET['paddingLeft'];
}

if(isset($_GET['paddingBottom']))
{
    $paddingBottom=$_GET['paddingBottom'];
}

if(isset($_GET['paddingTop']))
{
    $paddingTop=$_GET['paddingTop'];
}

if(isset($_GET['rotateDegrees']))
{
    $rotateDegrees=$_GET['rotateDegrees'];
}

if(isset($_GET['colorizeBlue']))
{
    $colorizeBlue=$_GET['colorizeBlue'];
    if(!isset($_GET['colorizeGreen']))
    {
        die('if you specify colorize for one color, you must specify it for all three (red, green, & blue)');
    }
}

if(isset($_GET['colorizeGreen']))
{
    $colorizeGreen=$_GET['colorizeGreen'];
    if(!isset($_GET['colorizeRed']))
    {
        die('if you specify colorize for one color, you must specify it for all three (red, green, & blue)');
    }
}

if(isset($_GET['colorizeRed']))
{
    $colorizeRed=$_GET['colorizeRed'];
    if(!isset($_GET['colorizeBlue']))
    {
        die('if you specify colorize for one color, you must specify it for all three (red, green, & blue)');
    }
}
if(strtolower($colorizeRed)     == 'y')
{
    $colorizeRed    = 255;
}
if(strtolower($colorizeGreen)   == 'y')
{
    $colorizeGreen  = 255;
}
if(strtolower($colorizeBlue)    == 'y')
{
    $colorizeBlue   = 255;
}
if(strtolower($colorizeRed)     == 'n')
{
    $colorizeRed    = 0;
}
if(strtolower($colorizeGreen)   == 'n')
{
    $colorizeGreen  = 0;
}
if(strtolower($colorizeBlue)    == 'n')
{
    $colorizeBlue   = 0;
}
if( (ctype_digit($colorizeRed))
    AND
    (ctype_digit($colorizeGreen))
    AND
    (ctype_digit($colorizeBlue)) )
{
    // values are OK
}
else
{
    echo "NOT DIGITS-colorizeRed=$colorizeRed , colorizeGreen=$colorizeGreen , colorizeBlue=$colorizeBlue";
    die('colorize color values must each be numbers from 0 to 255 (or "y" or "n", which equal 255 and 0 respectively)');
}

if(
    ($colorizeRed   >= 0)
    AND
    ($colorizeGreen >= 0)
    AND
    ($colorizeBlue  >= 0)
    AND
    ($colorizeRed   < 256)
    AND
    ($colorizeGreen < 256)
    AND
    ($colorizeBlue  < 256) )
{
    // values are OK
}
else
{
    echo "OUT OF RANGE-colorizeRed=$colorizeRed , colorizeGreen=$colorizeGreen , colorizeBlue=$colorizeBlue";
    die('colorize color values must each be numbers from 0 to 255 (or "y" or "n", which equal 255 and 0 respectively)');
}

if(!ctype_digit($maxWidth))
{
    die("maxWidth ('$maxWidth') must be a numeric value");
}

if(!ctype_digit($charSpacing))
{
    die("charSpacing ('$charSpacing') must be a numeric value");
}

list($width, $textHeight, $type, $attr) = getimagesize($fontFile);

// Create image instance
$src = imagecreatefrompng($fontFile)
  or die('makeBGpic.php  - unable to access source image; $fontFile');

// find background color (color of a space char)
$BGcolor = imagecolorat($src,$fontMap[' ']['offset'], 0);

$textMsg = $chap;
if( (!isset($fontMap['a']['offset']))  // no lowercase letters in font
    OR (substr($uppercase,0,1) == 'y') )
{
    $textMsg = strtoupper($textMsg);
}

$textChars = str_split($textMsg);


// calculate width total
$totalWidth = $paddingLeft;
foreach ($textChars as $textChar)
{
    if(!isset($fontMap[$textChar]['offset']))
    {
        $textChar = ' ';
    }
    if($textChar == '_')
    {
        $textChar = ' ';
    }
    $totalWidth=$totalWidth+$fontMap[$textChar]['width']+$charSpacing;
}

// padding on right

$totalWidth=$totalWidth+$paddingRight;

$dest = imagecreatetruecolor($totalWidth, $paddingTop+$textHeight+$paddingBottom);
 
imagefill($dest, 0, 0, $BGcolor);

$destOffset = 0;

// padding on left
imagecopy($dest, $src, $destOffset, $paddingTop, $fontMap[' ']['offset'], 0, $paddingLeft, $textHeight);
$destOffset=$destOffset+$paddingLeft;

// build the text message image
foreach ($textChars as $textChar)
{
    if(!isset($fontMap[$textChar]['offset']))
    {
        $textChar = ' ';
    }
    if($textChar == '_')
    {
        $textChar = ' ';
    }
    imagecopy($dest, $src, $destOffset, $paddingTop, $fontMap[$textChar]['offset'], 0, $fontMap[$textChar]['width'] , $textHeight);
    $destOffset=$destOffset+$fontMap[$textChar]['width'];
    imagecopy($dest, $src, $destOffset, $paddingTop, $fontMap[' ']['offset'], 0, $charSpacing, $textHeight);
    $destOffset=$destOffset+$charSpacing;
}

// padding on right
imagecopy($dest, $src, $destOffset, $paddingTop, $fontMap[' ']['offset'], 0, $paddingRight, $textHeight);
$destOffset=$destOffset+$paddingRight;

// resize the image
if($totalWidth > $maxWidth)
{
    $resizedDest=imagescale($dest,$maxWidth,-1,IMG_BILINEAR_FIXED);
}
else
{
    $resizedDest =  $dest;
}

// rotate the image
if($rotateDegrees <> 0)
{
    $resizedDest = imagerotate($resizedDest, $rotateDegrees, $BGcolor);
}

// negate and colorize the image - R, G, B
imagefilter($resizedDest, IMG_FILTER_GRAYSCALE);
if(substr($negate,0,1) == 'y')
{
    imagefilter($resizedDest, IMG_FILTER_NEGATE);
}
imagefilter($resizedDest, IMG_FILTER_COLORIZE, $colorizeRed, $colorizeGreen, $colorizeBlue);


imagepng($resizedDest,'images/'.$imgFileName);
 
//echo "<img src='images/$imgFileName'>";

//  try to set the filemtime to year 1980 for this BG image
//  so it does not trigger 'backup of images overdue' message on control panel
touch(__DIR__."/images/$imgFileName",strtotime('1980-01-01 06:00:01'));

//

// free memory
imagedestroy($dest);
imagedestroy($src);
@imagedestroy($resizedDest);
echo '<script>window.close();</script>';
?>