placemap.class.php 5.61 KB
<?php
putenv('GDFONTPATH=' . realpath('.')); // Kerrotaan GD:lle että fontti on samassa hakemistossa
# header("Content-type: image/png"); // lähetetään tiedot 

class paikkakartta {

    var $i; // imagehandler
    
    var $mp             = "1"; // Kerroin
    var $main_width     = "800";
    var $main_height    = "600";
    var $place_width    = "10";
    var $place_height   = "10";
    
    // Taustan värit
    var $bg_r           = "255";
    var $bg_g           = "255";
    var $bg_b           = "255";
    // Vapaan paikan väri
    var $p_free_r       = "255";
    var $p_free_g       = "0";
    var $p_free_b       = "0";
    // Varatun paikan väri
    var $p_nfree_r      = "0";
    var $p_nfree_g      = "255";
    var $p_nfree_b      = "0";
    // Oman paikan väri
    var $p_own_r        = "0";
    var $p_own_g        = "0";
    var $p_own_b        = "255";
    // Infoarean väri
    var $info_r         = "255";
    var $info_g         = "0";
    var $info_b         = "100";


    #function __construct() {
    #    $this->write_base();
    #}

    /* Kirjotteleepi pohjakartan ja sen reunat jos niin olemme halunneet*/ 
    function write_base() {
        header("Content-type: image/png");

        $this->main_width = $this->main_width * $this->mp;
        $this->main_height = $this->main_height * $this->mp;
        $this->i = imagecreate($this->main_width, $this->main_height);

        $this->bg_color = imagecolorallocate($this->i, $this->bg_r, $this->bg_g, $this->bg_b); // Taustaväri
        
        $this->black = imagecolorallocate($this->i, 0,0,0);

        $this->p_free = imagecolorallocate($this->i, $this->p_free_r, $this->p_free_g, $this->p_free_b);
        $this->p_nfree = imagecolorallocate($this->i, $this->p_nfree_r, $this->p_nfree_g, $this->p_nfree_b);
        $this->p_own = imagecolorallocate($this->i, $this->p_own_r, $this->p_own_g, $this->p_own_b);
        
        $this->info_color = imagecolorallocate($this->i, $this->info_r, $this->info_g, $this->info_b);

        imagefilledrectangle($this->i, 0,0, $this->width, $this->height, $this->bg_color);
    }

    function create_image() { // Luo itse kuvan, tätä pitää kutsua kun kaikki on luotu.
        imagepng($this->i);
        imagedestroy($this->i);
    }

    function write_place($x, $y, $status = NULL) {
        if($status == 1) { // jos on oma
            $color = $this->p_own;
        } elseif($status == 2) {// Jos on varattu
            $color = $this->p_nfree;
        } else { // muutenhan se on vapaa!
            $color = $this->p_free;
        }

        imagefilledrectangle( // piirretään itse paikka
            $this->i, 
            $x * $this->mp, 
            $y * $this->mp, 
            ($x + $this->place_width) * $this->mp, 
            ($y + $this->place_height) * $this->mp, 
            $color);

        imageline( // Vasen reunaviiva
            $this->i,
            $x * $this->mp,
            $y * $this->mp,
            $x * $this->mp,
            ($y + $this->place_height) * $this->mp,
            $this->black);

        imageline( // ylä reunaviiva
            $this->i,
            $x * $this->mp,
            $y * $this->mp,
            ($x + $this->place_width)* $this->mp,
            $y * $this->mp,
            $this->black);

       imageline( // Ala reunaviiva
            $this->i,
            ($x + $this->place_width) * $this->mp,
            ($y + $this->place_height) * $this->mp,
            $x * $this->mp,
            ($y + $this->place_height) * $this->mp,
            $this->black);

        imageline( // oikea reunaviiva
            $this->i,
            ($x + $this->place_width) * $this->mp,
            ($y + $this->place_height) * $this->mp,
            ($x + $this->place_width)* $this->mp,
            $y * $this->mp,
            $this->black); 
    } // end of func write_place()

    function write_info($x, $y, $w, $h, $txt, $color=NULL) { // Piirtää infoneliön
        if(!$color) {
            $color = $this->info_color;
        }
        imagefilledrectangle( // piirretään itse paikka
            $this->i,
            $x * $this->mp,
            $y * $this->mp,
            ($x + $w) * $this->mp,
            ($y + $h) * $this->mp,
            $color);

        imageline( // Vasen reunaviiva
            $this->i,
            $x * $this->mp,
            $y * $this->mp,
            $x * $this->mp,
            ($y + $h) * $this->mp,
            $this->black);

        imageline( // ylä reunaviiva
            $this->i,
            $x * $this->mp,
            $y * $this->mp,
            ($x + $w)* $this->mp,
            $y * $this->mp,
            $this->black);

        imageline( // Ala reunaviiva
             $this->i,
             ($x + $w) * $this->mp,
             ($y + $h) * $this->mp,
             $x * $this->mp,
             ($y + $h) * $this->mp,
             $this->black);

        imageline( // oikea reunaviiva
            $this->i,
            ($x + $w) * $this->mp,
            ($y + $h) * $this->mp,
            ($x + $w)* $this->mp,
            $y * $this->mp,
            $this->black);
        
        imagestring($this->i, 2, ($x + 5) * $this->mp, ($y + 5) * $this->mp, $txt, $this->black);
    } // end of func write_info()

    function write_text($x, $y, $text, $size=2) {
        imagestring($this->i, $size, $x, $y, $text, $this->black);
    }


    function imagecenteredstring($image, $font_size, $text, $text_color){
        $img_w = imagesx($image);
        $img_h = imagesy($image);
        $text = "$img_w $img_h";
        imagestring($image, $font_size, round(($img_w/2)-((strlen($text)*imagefontwidth($font_size))/2), 1), round(($img_h/2)-(imagefontheight($font_size)/2)), $text, $text_color);
    }

} // end of class
?>