Choose a file to view:
index.php  word.php  functions.php


<?PHP
/* Copyright (c) 2011 Brad Greco
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/* word.php generates a PNG image of a periodic table word from $_GET['word'].
 * It has these options:
 *    - showmass=true: shows the molar mass of each element
 *    - shownum=true:     shows the number of each element
 *    - showname=true: shows the name of each element
 */

require 'functions.php';

$word $_GET["word"] ?? '';
$showname $_GET["showname"] ?? FALSE;
$shownum $_GET["shownum"] ?? FALSE;
$showmass $_GET["showmass"] ?? FALSE;
$size $_GET["size"] ?? 1;

$db db_connect();
$elements elements($dbfalse);
$e array_keys($elements);
$words explode(" "$word);
$number 1;
foreach(
$words as $word) {
    
$result periodic($word$e$size);
    for(
$i 0$i strlen($result); $i++) {
        if(
substr($result$i1) <= 'Z' && substr($result$i+11) > 'Z') {   // 2 character element
            
$current substr($result$i2);
            
$i++;
        } else if(
substr($result$i1) <= 'Z') {   // 1 character element
            
$current substr($result$i1);
        }
        
$symbols[$number] = $current;
        
$names[$number] = $elements[$current]['name'];
        
$masses[$number] = $elements[$current]['mass'];
        
$numbers[$number] = $elements[$current]['number'];
        
$number++;
    }
    
$number++;
}
$number--;

$BOX_WIDTH 70;
$BOX_HEIGHT 80;
$SYMBOL_SIZE 25;
$NAME_SIZE 8;
$NUM_SIZE 10;
$MASS_SIZE 9;

$sheight 50;
$numheight 15;
$nameheight 15;
$massheight 75;
if(
$shownum || $showname)
    
$sheight $sheight 5;
if(
$showname && $shownum) {
    
$sheight $sheight 5;
    
$numheight 30;
}


$img ImageCreate(($number 1) * $BOX_WIDTH ,$BOX_HEIGHT);

$white imagecolorallocate($img255255255);
$black imagecolorallocate($img000);

$font __DIR__."/arial.ttf";
$symbolfont __DIR__."/arialbd.ttf";

imageline($img00, ($number 1) * $BOX_WIDTH 10$black);
imageline($img0$BOX_HEIGHT 1, ($number 1) * $BOX_WIDTH 1$BOX_HEIGHT 1$black);

for(
$i 0$i $number$i++) {
    
imageline($img$BOX_WIDTH $i0$BOX_WIDTH $i$BOX_HEIGHT$black);
    if (isset(
$symbols[$i 1])) {
        
$box imagettfbbox($SYMBOL_SIZE0$symbolfont$symbols[$i 1]);
        
imagettftext($img$SYMBOL_SIZE0$BOX_WIDTH $i - (($box[4] - $box[6])/2) + $BOX_WIDTH/1$sheight$black$symbolfont$symbols[$i 1]);
        if(
$showname) {
            
$box imagettfbbox($NAME_SIZE0$font$names[$i 1]);
            
imagettftext($img$NAME_SIZE0$BOX_WIDTH $i - (($box[4] - $box[6])/2) + $BOX_WIDTH/1$nameheight$black$font$names[$i 1]);
        }
        if(
$shownum) {
            
$box imagettfbbox($NUM_SIZE0$symbolfont$numbers[$i 1]);
            
imagettftext($img$NUM_SIZE0$BOX_WIDTH $i - (($box[4] - $box[6])/2) + $BOX_WIDTH/1$numheight$black$symbolfont$numbers[$i 1]);
        }
        if(
$showmass) {
            
$box imagettfbbox($MASS_SIZE0$font$masses[$i 1]);
            
imagettftext($img$MASS_SIZE0$BOX_WIDTH $i - (($box[4] - $box[6])/2) + $BOX_WIDTH/1$massheight$black$font$masses[$i 1]);
        }
    }
}

Header('Content-type:image/png');
ImagePNG($img);
ImageDestroy($img);
?>
'Z') { // 2 character element $current = substr($result, $i, 2); $i++; } else if(substr($result, $i, 1) <= 'Z') { // 1 character element $current = substr($result, $i, 1); } $symbols[$number] = $current; $names[$number] = $elements[$current]['name']; $masses[$number] = $elements[$current]['mass']; $numbers[$number] = $elements[$current]['number']; $number++; } $number++; } $number--; $BOX_WIDTH = 70; $BOX_HEIGHT = 80; $SYMBOL_SIZE = 25; $NAME_SIZE = 8; $NUM_SIZE = 10; $MASS_SIZE = 9; $sheight = 50; $numheight = 15; $nameheight = 15; $massheight = 75; if($shownum || $showname) $sheight = $sheight + 5; if($showname && $shownum) { $sheight = $sheight + 5; $numheight = 30; } $img = ImageCreate(($number - 1) * $BOX_WIDTH + 1 ,$BOX_HEIGHT); $white = imagecolorallocate($img, 255, 255, 255); $black = imagecolorallocate($img, 0, 0, 0); $font = __DIR__."/arial.ttf"; $symbolfont = __DIR__."/arialbd.ttf"; imageline($img, 0, 0, ($number - 1) * $BOX_WIDTH + 1, 0, $black); imageline($img, 0, $BOX_HEIGHT - 1, ($number - 1) * $BOX_WIDTH + 1, $BOX_HEIGHT - 1, $black); for($i = 0; $i < $number; $i++) { imageline($img, $BOX_WIDTH * $i, 0, $BOX_WIDTH * $i, $BOX_HEIGHT, $black); if (isset($symbols[$i + 1])) { $box = imagettfbbox($SYMBOL_SIZE, 0, $symbolfont, $symbols[$i + 1]); imagettftext($img, $SYMBOL_SIZE, 0, $BOX_WIDTH * $i - (($box[4] - $box[6])/2) + $BOX_WIDTH/2 - 1, $sheight, $black, $symbolfont, $symbols[$i + 1]); if($showname) { $box = imagettfbbox($NAME_SIZE, 0, $font, $names[$i + 1]); imagettftext($img, $NAME_SIZE, 0, $BOX_WIDTH * $i - (($box[4] - $box[6])/2) + $BOX_WIDTH/2 - 1, $nameheight, $black, $font, $names[$i + 1]); } if($shownum) { $box = imagettfbbox($NUM_SIZE, 0, $symbolfont, $numbers[$i + 1]); imagettftext($img, $NUM_SIZE, 0, $BOX_WIDTH * $i - (($box[4] - $box[6])/2) + $BOX_WIDTH/2 - 1, $numheight, $black, $symbolfont, $numbers[$i + 1]); } if($showmass) { $box = imagettfbbox($MASS_SIZE, 0, $font, $masses[$i + 1]); imagettftext($img, $MASS_SIZE, 0, $BOX_WIDTH * $i - (($box[4] - $box[6])/2) + $BOX_WIDTH/2 - 1, $massheight, $black, $font, $masses[$i + 1]); } } } Header('Content-type:image/png'); ImagePNG($img); ImageDestroy($img); ?>