<?php

/*
Church Sign Generator
Copyright (C) 2003, Ryland Sanders

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
get the text for each line from the query string, strip out backslashes
*/

$line[1] = stripslashes($_GET['line1']);
$line[2] = stripslashes($_GET['line2']);
$line[3] = stripslashes($_GET['line3']);
$line[4] = stripslashes($_GET['line4']);

/*
Character widths are put in an array, $widths. Note that the array is indexed by
the character's ASCII ordinal value. That makes it easy to retrieve the value
by using the ord() function in PHP to get ASCII values from each character in
the user's input. (The full ISO8859-1 character set is available, except where
noted.)

The letters in your chars.png should all be in one big vertical column, and the
height of the letters should correspond to the value of your line height in the
imagecopy() function. The letters should start at the top left corner (0,0),
and should be left-justified (smooshed up against the left edge). You'll notice
that the array contains the entire ISO8859-1 character set, so your chars.png
should as well.
*/

$widths32] =  4;  // space
$widths33] =  5;  // !
$widths34] =  7;  // "
$widths35] =  9;  // #
$widths36] =  8;  // $
$widths37] = 12;  // %
$widths38] = 10;  // &
$widths39] =  4;  // '
$widths40] =  5;  // (
$widths41] =  5;  // )
$widths42] =  6;  // *
$widths43] =  8;  // +
$widths44] =  4;  // ,
$widths45] =  5;  // -
$widths46] =  4;  // .
$widths47] =  5;  // /
$widths48] =  8;  // 0
$widths49] =  7;  // 1
$widths50] =  8;  // 2
$widths51] =  8;  // 3
$widths52] =  8;  // 4
$widths53] =  8;  // 5
$widths54] =  8;  // 6
$widths55] =  8;  // 7
$widths56] =  8;  // 8
$widths57] =  8;  // 9
$widths58] =  5;  // :
$widths59] =  5;  // ;
$widths60] =  9;  // <
$widths61] =  9;  // =
$widths62] =  9;  // >
$widths63] =  9;  // ?
$widths64] = 14;  // @
$widths65] = 10;  // A
$widths66] = 10;  // B
$widths67] = 10;  // C
$widths68] = 10;  // D
$widths69] =  9;  // E
$widths70] =  9;  // F
$widths71] = 11;  // G
$widths72] = 10;  // H
$widths73] =  5;  // I
$widths74] =  8;  // J
$widths75] = 11;  // K
$widths76] =  9;  // L
$widths77] = 11;  // M
$widths78] = 10;  // N
$widths79] = 11;  // O
$widths80] =  9;  // P
$widths81] = 11;  // Q
$widths82] = 10;  // R
$widths83] =  9;  // S
$widths84] =  9;  // T
$widths85] = 10;  // U
$widths86] = 10;  // V
$widths87] = 13;  // W
$widths88] = 10;  // X
$widths89] = 10;  // Y
$widths90] =  9;  // Z
$widths91] =  6;  // [
$widths92] =  5;  // \
$widths93] =  5;  // ]
$widths94] =  8;  // ^
$widths95] =  8;  // _
$widths96] =  5;  // `
$widths97] = 10;  // a
$widths98] = 10;  // b
$widths99] = 10;  // c
$widths[100] = 10;  // d
$widths[101] =  9;  // e
$widths[102] =  9;  // f
$widths[103] = 11;  // g
$widths[104] = 10;  // h
$widths[105] =  5;  // i
$widths[106] =  8;  // j
$widths[107] = 11;  // k
$widths[108] =  9;  // l
$widths[109] = 11;  // m
$widths[110] = 10;  // n
$widths[111] = 11;  // o
$widths[112] =  9;  // p
$widths[113] = 11;  // q
$widths[114] = 10;  // r
$widths[115] =  9;  // s
$widths[116] =  9;  // t
$widths[117] = 10;  // u
$widths[118] = 10;  // v
$widths[119] = 13;  // w
$widths[120] = 10;  // x
$widths[121] = 10;  // y
$widths[122] =  9;  // z
$widths[123] =  6;  // {
$widths[124] =  4;  // |
$widths[125] =  6;  // }
$widths[126] =  9;  // ~
$widths[127] =  0;  // nothing
$widths[128] =  8;  // €
$widths[129] =  0;  // nothing
$widths[130] =  4;  // ‚
$widths[131] =  9;  // ƒ
$widths[132] =  7;  // „
$widths[133] = 13;  // …
$widths[134] =  8;  // †
$widths[135] =  8;  // ‡
$widths[136] =  6;  // ˆ
$widths[137] = 14;  // ‰
$widths[138] = 10;  // Š
$widths[139] =  5;  // ‹
$widths[140] = 14;  // Œ
$widths[141] =  0;  // nothing
$widths[142] =  9;  // Ž
$widths[143] =  0;  // nothing
$widths[144] =  0;  // nothing
$widths[145] =  5;  // ‘
$widths[146] =  5;  // ’
$widths[147] =  7;  // “
$widths[148] =  7;  // ”
$widths[149] =  6;  // •
$widths[150] =  9;  // –
$widths[151] = 14;  // —
$widths[152] =  6;  // ˜
$widths[153] = 13;  // ™
$widths[154] = 10;  // š
$widths[155] =  6;  // ›
$widths[156] = 14;  // œ
$widths[157] =  0;  // nothing
$widths[158] =  9;  // Ž
$widths[159] = 10;  // Ÿ
$widths[160] =  4;  // space
$widths[161] =  5;  // ¡
$widths[162] =  8;  // ¢
$widths[163] =  9;  // £
$widths[164] =  9;  // ¤
$widths[165] =  9;  // ¥
$widths[166] =  4;  // ¦
$widths[167] =  8;  // §
$widths[168] =  6;  // ¨
$widths[169] = 11;  // ©
$widths[170] =  6;  // ª
$widths[171] =  8;  // «
$widths[172] =  9;  // ¬
$widths[173] =  6;  // ­
$widths[174] = 11;  // ®
$widths[175] = 10;  // ¯
$widths[176] =  7;  // °
$widths[177] = 10;  // ±
$widths[178] =  6;  // ²
$widths[179] =  6;  // ³
$widths[180] =  5;  // ´
$widths[181] = 10;  // µ
$widths[182] =  9;  // ¶
$widths[183] =  7;  // ·
$widths[184] =  5;  // ¸
$widths[185] =  5;  // ¹
$widths[186] =  6;  // º
$widths[187] =  8;  // »
$widths[188] = 12;  // ¼
$widths[189] = 12;  // ½
$widths[190] = 12;  // ¾
$widths[191] =  9;  // ¿
$widths[192] = 11;  // À
$widths[193] = 11;  // Á
$widths[194] = 11;  // Â
$widths[195] = 11;  // Ã
$widths[196] = 11;  // Ä
$widths[197] = 11;  // Å
$widths[198] = 14;  // Æ
$widths[199] = 10;  // Ç
$widths[200] =  9;  // È
$widths[201] =  9;  // É
$widths[202] =  9;  // Ê
$widths[203] =  9;  // Ë
$widths[204] =  5;  // Ì
$widths[205] =  5;  // Í
$widths[206] =  5;  // Î
$widths[207] =  5;  // Ï
$widths[208] = 10;  // Ð
$widths[209] = 10;  // Ñ
$widths[210] = 11;  // Ò
$widths[211] = 11;  // Ó
$widths[212] = 11;  // Ô
$widths[213] = 11;  // Õ
$widths[214] = 11;  // Ö
$widths[215] =  8;  // ×
$widths[216] = 11;  // Ø
$widths[217] = 10;  // Ù
$widths[218] = 10;  // Ú
$widths[219] = 10;  // Û
$widths[220] = 10;  // Ü
$widths[221] = 10;  // Ý
$widths[222] = 10;  // Þ
$widths[223] =  9;  // ß
$widths[224] = 11;  // à
$widths[225] = 11;  // á
$widths[226] = 11;  // â
$widths[227] = 11;  // ã
$widths[228] = 11;  // ä
$widths[229] = 11;  // å
$widths[230] = 14;  // æ
$widths[231] = 10;  // ç
$widths[232] =  9;  // è
$widths[233] =  9;  // é
$widths[234] =  9;  // ê
$widths[235] =  9;  // ë
$widths[236] =  5;  // ì
$widths[237] =  5;  // í
$widths[238] =  5;  // î
$widths[239] =  5;  // ï
$widths[240] = 10;  // Ð
$widths[241] = 10;  // ñ
$widths[242] = 11;  // ò
$widths[243] = 11;  // ó
$widths[244] = 11;  // ô
$widths[245] = 11;  // õ
$widths[246] = 11;  // ö
$widths[247] = 10;  // ÷
$widths[248] = 11;  // ø
$widths[249] = 10;  // ù
$widths[250] = 10;  // ú
$widths[251] = 10;  // û
$widths[252] = 10;  // ü
$widths[253] = 10;  // ý
$widths[254] = 10;  // þ
$widths[255] = 10;  // ÿ

/* load the characters image and the background sign image into memory */

$chars_img imagecreatefrompng("chars.png");
$basesign_img imagecreatefrompng("basesign.png");

/* get the width and height of the background image */

$src_w imagesx($basesign_img);
$src_h imagesy($basesign_img);

/* create a new image, that will be output to the browser */

$output_img imagecreatetruecolor($src_w$src_h);

/* copy the background image onto the output image */

imagecopy($output_img$basesign_img0,0,0,0$src_w$src_h);

/* Since the rows are centered horizontally on the sign, the x centerpoint gives a starting
point from which the x offset for each letter can be calculated. The y offset is simply the
distance from the top of the image to the top of the first row of letters. */

$x_center 170;
$y_offset 141;

/* Set maximum line width to keep characters from drifting off the edges
of the sign */

$max_line_width 186;

/* loop through each row */
for ($l 1$l 5$l++) {
    
$x_offset 0;
    
$truncate_to 0;

/* This first pass is to calculate the x offset for each row, since the width of the
line of characters is different for each row. It loops through the letters adds the
width of each letter to the x offset to get the beginning point for each line. */
    
for ($i 0$i strlen($line[$l]); $i++) {
        
$curchar substr($line[$l], $i1);

        
// if the x offset plus the width of the current character
        // don't exceed the max line width, add the width to the offset
        
if ($x_offset $widths[ord($curchar)] <= $max_line_width) {
            
$x_offset += $widths[ord($curchar)];
            
$truncate_to++;
        }
    }

/* lop off characters that would have exceeded max_line_width */
    
$line[$l] = substr($line[$l],0,$truncate_to);

/* since we're centering, we divide the offset by 2 */
    
$x_offset ceil($x_offset 2);

/* Now we loop through each character and copy it onto the background image. */

    
for ($i 0$i strlen($line[$l]); $i++) {
        
$curchar substr($line[$l], $i1);
        
// copy the region from the source image containing the letter we want
        // to the destination image
        
imagecopy(
            
$output_img,               // destination image: the image we're building
            
$chars_img,                // source image: chars1.png
            
$x_center $x_offset,     // destination X coord: the center minus the x offset
            
$y_offset,                 // destination Y coord: equal to y offset
            
0,                         // source image X coord: always zero
            
(ord($curchar) - 32) * 18// source image Y coord: character ASCII value - 32 times line height
            
16,                        // source area width
            
18                         // source area height
        
);
        
$x_offset -= $widths[ord($curchar)];
    }

/* When each row is complete, add 16 pixels to the Y offset to get the top of the new row. */
    
$y_offset += 16;
}

/* Now that the image is built, it gets sent to the browser. First, send out HTTP headers to
tell the browser a JPEG image is coming and that it should be called "churchsign.jpg". */

/*
determine how to send the image - whether it's for use inline, or to be downloaded
*/

$download $_GET['download'] + 0;

if (
$download == 1) {
    
// if it's to be downloaded, send it as an attachment
    // this will trigger a "Save As..." dialog box
    
header("Content-Type: image/jpeg");
    
header("Content-Disposition: attachment; filename=churchsign.jpg");
} else {
    
// if it's not to be downloaded, send it as an inline image
    
header("Content-Type: image/jpeg");
    
header("Content-Disposition: inline; filename=churchsign.jpg");
}

/* Now that we've sent the headers, send the actual image data.
The imagejpeg() function sends the output img to the browser. */

imagejpeg($output_img);

/* Housekeeping functions - destroy the scratch images to free up the memory they take up. */
imagedestroy($basesign_img);
imagedestroy($chars_img);
imagedestroy($output_img);

?>