Location: PHPKode > projects > jjfmapper > jjfmapper/lib/helpers.php
<?php #-*-Mode: php; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/*
    jjfMapper, a cartography program for PHP 4.
    Copyright (C) 2004  John J Foerch

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

//helpers.php
//This file contains functions that provide added convenience
//to the task at hand.  They are global in scope.  Many were
//copied directly from the php documentation and user comments
//on www.php.net, and I thank the authors named and unnamed
//for obviating some of the tricky spots in this language.
//
//JJF
//

include_once ('css_colors.php');

//array slice with key preservation,
//as published by "dams at php dot net" in the user comments
//of the php documentation.
function array_slice_key ($array, $offset) {
    if (! is_array ($array)) return false;
    if (func_num_args() == 3)
    {
        $length = func_get_arg(2);
        $length = max(0,intval($length));
    } else {
        $length = count($array);
    }
    $i = 0;
    $return = array();
    $keys = array_slice (array_keys ($array), $offset, $length);
    foreach( $keys as $key) {
        $return[$key] = $array[$key];
    }
    return $return;
}

/*
This function from Justin Greer, in the contributions section of the
imagecreatetruecolor entry in the php manual.
 */
function gdVersion() {
   static $gd_version_number = null;
   if ($gd_version_number === null) {
       // Use output buffering to get results from phpinfo()
       // without disturbing the page we're in.  Output
       // buffering is "stackable" so we don't even have to
       // worry about previous or encompassing buffering.
       ob_start();
       phpinfo(8);
       $module_info = ob_get_contents();
       ob_end_clean();
       if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i",
               $module_info,$matches)) {
           $gd_version_number = $matches[1];
       } else {
           $gd_version_number = 0;
       }
   }
   return $gd_version_number;
} 
if (defined ('JJFM_CHECK_GD_VERSION'))
{
    if (version_compare (gdVersion(),'2','>=')) define ('JJFM_USE_GD2',1);
    if (version_compare (gdVersion(),'2.0.1','>=')) define('JJFM_USE_GD201',1);
}
if (defined ('JJFM_CHECK_FONT_LIBRARY'))
{
    if (function_exists ('imagefttext') &&
        defined ('JJFM_USE_GD201'))
    {
        define ('JJFM_USE_FT',1);
    } elseif (function_exists ('imagettftext')) {
        define ('JJFM_USE_TTF',1);
    }
}



function lineweight ($weight, &$gd_resource) {
    if (defined ('JJFM_USE_GD201')) imagesetthickness($gd_resource,$weight);
}

//Function imagedashedline has been deprecated.  The
//PHP manual recommends the use of imagesetstyle and
//imageline in stead.  If jjfMapper is running under
//php >= 4.32 with GD 2 or higher, it will use alpha
//drawing to achieve a dashed line.  Otherwise, it will
//use the image background color for the spaces between
//the dashes.  The caller should always pass a background
//color, even though it will be ignored if alpha is
//supported.
//these functions are based on code published by
//"michi at marel dot at" in the user comments of the
//php manual.
function quasi_dashed_line ($image, $x0, $y0, $x1, $y1, $fg, $bg) {
    $st = array ($fg, $fg, $fg, $fg, $bg, $bg, $bg, $bg);
    imagesetstyle ($image, $st);
    imageline ($image, $x0, $y0, $x1, $y1, IMG_COLOR_STYLED);
}
function dashed_line ($image, $x0, $y0, $x1, $y1, $fg, $bg) {
    $bg = imagecolorallocatealpha ($image, 0, 0, 0, 127);
    $st = array ($fg, $fg, $fg, $fg, $bg, $bg, $bg, $bg);
    imagealphablending ($image, true);
    imagesetstyle ($image, $st);
    imageline ($image, $x0, $y0, $x1, $y1, IMG_COLOR_STYLED);
    imagealphablending ($image, false);
}
if (defined ('JJFM_USE_GD201') && version_compare (phpversion(),'4.3.2','>='))
    $jjfm_dashedline = 'dashed_line';
else $jjfm_dashedline = 'quasi_dashed_line';


//print_r_html
//published by ben, or "phpemail at supernaut dot org"
//in user contributions of the php manual.
function print_r_html ($r) {
    foreach ($r as $key => $val) {
        if (is_array ($val))
        {
            echo "[$key] = An Array:<BLOCKQUOTE>";
            print_r_html ($val);
            echo "</BLOCKQUOTE></P>";
        } else {
            echo "[$key] = '$val'<BR>";
        } //end if stmt
    }
} // end function print_r_html


//test for the intersection of two rectangles.
//returns true for an intersection, false for none.
function RectClip($x1,$y1,$x2,$y2,$x3,$y3,$x4,$y4) {
    if((($x1 >= $x3) && ($x1 <= $x4) && ($y1 >= $y3) && ($y1 <= $y4)) ||
       (($x2 >= $x3) && ($x2 <= $x4) && ($y2 >= $y3) && ($y2 <= $y4)) ||
       (($x1 >= $x3) && ($x1 <= $x4) && ($y2 >= $y3) && ($y2 <= $y4)) ||
       (($x2 >= $x3) && ($x2 <= $x4) && ($y1 >= $y3) && ($y1 <= $y4)) ||
       (($x3 >= $x1) && ($x3 <= $x2) && ($y3 >= $y1) && ($y3 <= $y2)) ||
       (($x4 >= $x1) && ($x4 <= $x2) && ($y4 >= $y1) && ($y4 <= $y2)) ||
       (($x1 >= $x3) && ($x2 <= $x4) && ($y3 >= $y1) && ($y4 <= $y2)) ||
       (($x3 >= $x1) && ($x4 <= $x2) && ($y3 <= $y1) && ($y4 >= $y2)))
    {
        return true;
    }
    return false;
}


function ColorStringToResource ($color, &$gd_resource) {
    global $css_colors;

    //convert to lower case
    $color = strtolower ($color);

    //get hex representation from name
    //or expand three digit hex to 6 digit.
    if (isset ($css_colors[$color]))
    {
        $color = $css_colors[$color];
    } else {
        //strip leading octothorp if present
        $color = ltrim ($color, '#');
        if (strlen ($color) == 3)
        {
            $color = $color[0] . $color[0] .
                $color[1] . $color[1] .
                $color[2] . $color[2];
        }
    }

    list ($r, $g, $b) = array(hexdec($color[0] . $color[1]),
                              hexdec($color[2] . $color[3]),
                              hexdec($color[4] . $color[5]));
    $idx = imagecolorallocate ($gd_resource, $r, $g, $b);
    return $idx;
}



function expand_symbols ($s, &$symbols) {
    //an invalid symbol causes this function to return boolean false.
    while (1)
    {
        $m = strpos ($s, '${');
        if ($m === false) return $s;

        $n = strpos ($s, '}', $m + 3);
        if ($n === false) return $s;

        $o = strtoupper (substr ($s, $m + 2, $n - ($m + 2)));
        if (! isset ($symbols[$o])) return false;

        $s = substr ($s, 0, $m) . $symbols[$o] . substr ($s, $n + 1);
    }
}




/*The following code is a workaround for php's unpack function
which does not have the capability of unpacking double precision
floats that were packed in a byte order other than the one of
the current machine.
*/
function big_endian_unpack ($format, $data) {
    $ar = unpack ($format, $data);
    $vals = array_values ($ar);
    $f = explode ('/', $format);
    $i = 0;
    foreach ($f as $f_k => $f_v) {
    $repeater = intval (substr ($f_v, 1));
    if ($repeater == 0) $repeater = 1;
    if ($f_v{1} == '*')
    {
        $repeater = count ($ar) - $i;
    }
    if ($f_v{0} != 'd') { $i += $repeater; continue; }
    $j = $i + $repeater;
    for ($a = $i; $a < $j; ++$a)
    {
        $p = pack ('d',$vals[$i]);
        $p = strrev ($p);
        list ($vals[$i]) = array_values (unpack ('d1d', $p));
        ++$i;
    }
    }
    $a = 0;
    foreach ($ar as $ar_k => $ar_v) {
    $ar[$ar_k] = $vals[$a];
    ++$a;
    }
    return $ar;
}

list ($jjfm_endiantest) = array_values (unpack ('L1L', pack ('V',1)));
if ($jjfm_endiantest != 1) define ('BIG_ENDIAN_MACHINE',1);
if (defined ('BIG_ENDIAN_MACHINE')) $unpack_workaround = 'big_endian_unpack';
else $unpack_workaround = 'unpack';

?>
Return current item: jjfmapper