Avatar billede donslund Nybegynder
13. februar 2004 - 15:29 Der er 14 kommentarer og
1 løsning

Et lille problem - sikkert

Jeg har problemer med denne kode:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
    <TITLE>[Ingen titel]</TITLE>
</HEAD>
<BODY>
<?php

//include the class
include("class_array_to_image.inc");

//array to be shown as image

$array[head][0]='id';
$array[head][1]='name';
$array[1][0]=1;
$array[1][1]='Robert';
$array[2][0]=2;
$array[2][1]='Frank';

//reset($array);

/* params name:
array_to_image(array(), font, cellspacing,
        center, max_x, max_y, type,
        line_color,
        head_bgcolor, head_txt_color,
        even_bgcolor, even_txt_color,
        odd_bgcolor, odd_txt_color)
*/

//call the costructor whit parameters
$image= new array_to_image($array,3,5,
        true,600,600,'png',
        "0,0,0",
        "128,128,128", "0,0,0",
        "230,230,230", "0,0,0",
        "255,255,255", "0,0,0");

//set name to give to the image
$name = "testimage";
//store the image
$image->show_image($name);

//to show the image use this:
//$image->show_image();

?>



</BODY>
</HTML>

Den viser ingenting ud over de gængse html-tags i start og bund.
Avatar billede tefcke Nybegynder
13. februar 2004 - 15:42 #1
Og hvordan skulle vi hjælpe dig, når vi ikkeved hvad der sker i din klasse?
Avatar billede donslund Nybegynder
13. februar 2004 - 15:44 #2
Det kan du naturligvis godt have ret i. Så jeg vover pelsen og smider hele koden :-)


<?php
//
// Class to render array as image
//

/**
*
* Class array_to_image: show a bidimensional array as an image representing
* a table. Allows:
* - set max image size and automatly resize if the resulting image is to big
* - different background and text color for head and rows
* - alternate background abd text color for even and odd rows
* - PNG or JPG format
* - store to files or show directly
*
* @author      Roberto 'Frank' Franchini <f.rank@libero.it>
* @version      1.1
*
*/

class array_to_image
{
    /**
    * Array to be showed
    * @var  array
    * @access  public
    */
    var $arr;
    /**
    * Image handle
    * @var  handler
    * @access  private
    */
    var $image;   
    /**
    * Fonts and colors parameters
    * @var  font
    * @access  private
    */
    var $font;
    var $fontWidth;
    var $even_bgcolor;
    var $even_txt_color;
    var $odd_bgcolor;
    var $odd_txt_color;
    var $head_bgcolor;
    var $head_txt_color;
    var $line_color;
    //options
    var $cellspacing;
    var $center;  //if true center the text
    var $max_x;  //max dimension
    var $max_y;
    var $type;
    //private parameters (calculated)
    var $rows;  //number of rows and columns
    var $cols;
    var $dim_x; //width and height in pixel of the resulting image
    var $dim_y;
    var $row_height;  //height of each row in pixel
    //it's all OK?
    var $flag;

    //constructor
    function array_to_image($arr=array(), $font=2, $cellspacing=5,
                $center=true, $max_x=600, $max_y=600, $type='png',
                $line_color="0,0,0",
                $head_bgcolor="128,128,128", $head_txt_color="0,0,0",
                $even_bgcolor="230,230,230", $even_txt_color="0,0,0",
                $odd_bgcolor="214,214,214", $odd_txt_color="0,0,0")
    {
        $this->arr              = $arr;
        $this->font            = $font;
        $this->even_bgcolor    = $this->_setcolor($even_bgcolor);
        $this->even_txt_color  = $this->_setcolor($even_txt_color);
        $this->odd_bgcolor      = $this->_setcolor($odd_bgcolor);
        $this->odd_txt_color    = $this->_setcolor($odd_txt_color);
        $this->head_bgcolor    = $this->_setcolor($head_bgcolor);
        $this->head_txt_color  = $this->_setcolor($head_txt_color);
        $this->line_color      = $this->_setcolor($line_color);
        $this->cellspacing      = $cellspacing;
        $this->center          = $center;
        $this->max_x            = $max_x;
        $this->max_y            = $max_y;
        $this->type            = $type;
        $this->rows    = 0;
        $this->cols    = 0;
        $this->dim_x    = 0;
        $this->dim_y    = 0;
        $this->flag    = 1;
        $this->image    = 0;
    } //end constructor
   
    //destructor
    function _array_to_image()
    {
        $this->arr              = null;
        $this->font            = null;
        $this->even_bgcolor    = null;
        $this->even_txt_color  = null;
        $this->odd_bgcolor      = null;
        $this->odd_txt_color    = null;
        $this->head_bgcolor    = null;
        $this->head_txt_color  = null;
        $this->line_color      = null;
        $this->cellspacing      = null;
        $this->center          = null;
        $this->max_x            = null;
        $this->max_y            = null;
        $this->type            = null;
        $this->rows    = 0;
        $this->cols    = 0;
        $this->dim_x    = 0;
        $this->dim_y    = 0;
        $this->flag    = 1;
    } //end constructor
   
    /**
    * Show or store to the given file name the image
    * in the selected format. It calls _image_create() if
    * the handle isn't setted, and then calls _draw_table()
    * that draws the table on the handle.
    *
    * @param  string  $name      name of file where to store the image
    * @param  bool    $destroy    tells if the image handle should be destoyed
    * @access  public
    * @returns image              png or jpg image if $name is setted
    */
    function show_image($nome='', $destroy=true)
    {
        if (!$this->image) {
            $this->_create_image();
        }
        $this->_draw_table();
        switch ($this->type) {
        case 'png':
            if ($nome) {
                imagepng($this->image,$nome.".png");
            } else {
                header("Content-type: image/png");
                imagepng($this->image);
            }
            break;
        case 'jpeg':
            if ($nome) {
                imagejpeg ($this->image,$nome.".jpg");
            } else {
                header ("Content-type: image/jpeg");
                imagejpeg ($this->image);
            }
            break;
        }
        if ($destroy == true) {
            $this->_array_to_image();
            imagedestroy($this->image);
        }
    } //end func show_image

    /**
    * Create an associative array for color values
    *
    * @param    sting  $color comma separated value for Red,Green,Blue
    * @access  private
    * @returns  associative arrary of colors
    */
    function _setcolor($color)
    {
        $color  = explode(",",$color);
        $red    = $color[0];
        $green  = $color[1];
        $blue  = $color[2];
        return (array(red=>$red,green=>$green,blue=>$blue));
    }

    /**
    * Set colors for all types of cells
    *
    * @access  private
    * @returns  none
    */
    function _set_cells_color()
    {
        //setto i colori
        $this->even_bgcolor    = imagecolorallocate($this->image, $this->even_bgcolor[red], $this->even_bgcolor[green], $this->even_bgcolor[blue] );
        $this->even_txt_color  = imagecolorallocate($this->image, $this->even_txt_color[red], $this->even_txt_color[green], $this->even_txt_color[blue] );
        $this->odd_bgcolor      = imagecolorallocate($this->image, $this->odd_bgcolor[red], $this->odd_bgcolor[green], $this->odd_bgcolor[blue] );
        $this->odd_txt_color    = imagecolorallocate($this->image, $this->odd_txt_color[red], $this->odd_txt_color[green], $this->odd_txt_color[blue] );
        $this->line_color      = imagecolorallocate($this->image, $this->line_color[red], $this->line_color[green], $this->line_color[blue] );
        $this->head_bgcolor    = imagecolorallocate($this->image, $this->head_bgcolor[red], $this->head_bgcolor[green], $this->head_bgcolor[blue] );
        $this->head_txt_color  = imagecolorallocate($this->image, $this->head_txt_color[red], $this->head_txt_color[green], $this->head_txt_color[blue] );
        $line_color            = imagecolorallocate($this->image, $this->line_color[red], $this->line_color[green], $this->line_color[blue] );
    } //end func _setcolor

    /**
    * Create the image and if necesary resize it to fit the
    * max dimension required. This method only allocates graphic space
    * where to draw the table
    *
    * @access  private
    * @returns  none
    */

    function _create_image()
    {
        if ((!$this->cols) || (!$this->rows)) $this->_max_cells_width();
        if ((!$this->dim_x) || (!$this->dim_y)) $this->_image_dimension();
        //if calculated dimension of resulting image are bigger than
        // max dimension, drop down font dimension and cellspacing
        while (($this->dim_x > $this->max_x) or ($this->dim_y > $this->max_y)
                and ($this->font > 1 or $this->cellspacing > 1 )) {
            $this->cellspacing = ($this->cellspacing > 1) ? $this->cellspacing-1 : 1;
            $this->font = ($this->font > 1) ? $this->font-1 : 1;
            $this->_max_cells_width();
            $this->_image_dimension();
        }
        //now i can create the image
        $this->image = imagecreate($this->dim_x, $this->dim_y);
        $this->_set_cells_color();
    } //end func _create_image

    /**
    * Draw the table as image. Doesn't show any output.
    *
    * @access  private
    * @returns  none
    */
   
    function _draw_table()
    {
        $y2 = 0;
        $x2 = 0;
        reset($this->arr);
        $y1 = 0;
        $even = true;
        while (list($rowKey,$row) = each($this->arr) ) {
            $x1 = 0;
            if ($rowKey =='width') continue;
            /*'for' statement is necessary for draw cell's rectangle
            * on empty cells
            * This 'while' statement will not round empty cells:
            * while (list($colKey,$element) = each ($row)) {
            */
            for ($colKey = 0; $colKey < $this->cols ; $colKey++) {
                $element = $this->arr[$rowKey][$colKey];
                $col_width = $this->arr['width'][$colKey] * $this->fontWidth + 2*$this->cellspacing;
                $string = $element;
                if ($this->center) {
                    $str_width = strlen($string) * $this->fontWidth;
                    $shift = ($col_width - $str_width) / 2 - $this->cellspacing;
                }
                $x2 = $x1 + $col_width;
                $y2 = $y1 + $this->row_height;
                //if index point to table's head apply style
                if ($rowKey == 'head') {
                    imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $this->head_bgcolor);
                    ImageString($this->image, $this->font, $x1+$this->cellspacing+$shift, $y1+$this->cellspacing, $string, $this->head_txt_color);
                } elseif ($even) {
                    imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $this->even_bgcolor);
                    ImageString($this->image, $this->font, $x1+$this->cellspacing+$shift, $y1+$this->cellspacing, $string, $this->even_txt_color);
                } else {
                    imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $this->odd_bgcolor);
                    ImageString($this->image, $this->font, $x1+$this->cellspacing+$shift, $y1+$this->cellspacing, $string, $this->odd_txt_color);
                }
                imagerectangle($this->image, $x1, $y1, $x2, $y2, $this->line_color);
                $x1 = $x2; //next column
            }
            $y1 = $y2; //next row
            $even = !$even;
        }
    } //end func _draw_table
   

    /**
    * Claculate the pixel lenght and widht of resulting image
    * and set the parameters dim_x and dim_y
    *
    * @access  private
    * @return  bool
    */
    function _image_dimension()
    {
        $this->fontWidth = imagefontwidth($this->font);
        $font_height = imagefontheight($this->font);
        $tot_char_width = 0; //total width of array in character
        if (($this->cols == 0) || ($this->rows == 0)) {
            $this->_max_cells_width();
        }
        reset($this->arr);
        while (list($i,$num_char) = each($this->arr['width'])) {
            $tot_char_width += $num_char;
        }
        $this->row_height = $font_height + 2 * $this->cellspacing ;
        //resulting image's dimension in pixel
        $this->dim_x = $tot_char_width * $this->fontWidth + $this->cols * 2 * $this->cellspacing + 1;
        $this->dim_y = ($this->rows-1) * ($this->row_height) + 1;
        return($this->flag);
    }

    /**
    * Calculate the maximun width as number of character
    * for each column of the original bidimensional array
    * It adds a row to the array :
    * $this->array[width] = array();
    * and then sets the parameters $rows, $cols
    *
    * @access  private
    * @returns void
    */
    function _max_cells_width()
    {
        $this->arr['width'] = array();
        $this->rows = 0;
        $this->cols = 0;
        reset($this->arr);
        while (list($row_key,$row) = each($this->arr)) {
            while (list($col_key,$elem) = each($row)) {
                $width = strlen($elem);
                if (($this->arr['width'][$col_key] < $width) and ($row_key != 'width')) {
                    $this->arr['width'][$col_key] = $width;
                }
            }
            $this->rows++;
        }
        $this->cols = count($this->arr['width']);
    }//end func _max_cells_width

} //end of class array_to_image

?>
Avatar billede Slettet bruger
13. februar 2004 - 15:58 #3
Hvis du lod være med at undretrykke alle errors ville der nok komme noget, istedet for bare blank html..
Avatar billede tefcke Nybegynder
13. februar 2004 - 15:59 #4
Så vidt jeg kan se i klassen, så gør følgende:
$image->show_image($name);
at du skriver billedet til en fil (altså intet output)

Og:
$image->show_image();
outputter billedet!
Avatar billede tefcke Nybegynder
13. februar 2004 - 16:03 #5
Huh, hvor bliver der undertrykt errors?
Avatar billede donslund Nybegynder
13. februar 2004 - 16:27 #6
Nu lavede jeg det sidste om, så den

//set name to give to the image
$name = "testimage";
//store the image
//$image->show_image($name);

//to show the image use this:
$image->show_image();

i stedet for, hvilket affødte
http://www.kongeborgen.dk/projekt100/diagram2.php
Avatar billede tefcke Nybegynder
13. februar 2004 - 16:42 #7
Du må ikke outoutte til browser inden du kalder header().

Gør istedet som du gjorde før, og lav et normalt img tag og indsæt det billed der bliver genereret!
Avatar billede donslund Nybegynder
13. februar 2004 - 16:56 #8
print "<IMG SRC=\"$name\" BORDER=\"0\" ALIGN=\"bottom\" ALT=\"\">";

Sådan så?
Avatar billede donslund Nybegynder
13. februar 2004 - 16:57 #9
Giver ikke noget billede, blot firkanten med det røde kryds.
Avatar billede tefcke Nybegynder
13. februar 2004 - 16:58 #10
Ja, men billedet kommer jo til at hedde $name.png
Avatar billede donslund Nybegynder
13. februar 2004 - 17:00 #11
print "<IMG SRC=\"$name.png\" BORDER=\"0\" ALIGN=\"bottom\" ALT=\"\">";

Så skete der noget.
Avatar billede tefcke Nybegynder
13. februar 2004 - 17:07 #12
Virker det?
Avatar billede donslund Nybegynder
13. februar 2004 - 17:26 #13
ja, det gør det, men den gør ikke det jeg vil have den til, så jeg er gået i krig med et andet kode eksempel. Jeg skal have lavet et søjle eller cirkel-diagram ud fra et db-udtræk.
Avatar billede donslund Nybegynder
15. februar 2004 - 21:52 #14
tefcke -> smid lige et svar, så får du point.
Avatar billede tefcke Nybegynder
15. februar 2004 - 23:27 #15
Yes sir!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester