Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > สร้าง GD แล้วอยากให้อยู่กึ่งกลาง หน้าจอ ทำไงครับ สร้าง GD แล้วอยากให้อยู่กึ่งกลาง หน้าจอ ทำไงครับ



 

สร้าง GD แล้วอยากให้อยู่กึ่งกลาง หน้าจอ ทำไงครับ สร้าง GD แล้วอยากให้อยู่กึ่งกลาง หน้าจอ ทำไงครับ

 



Topic : 027839



โพสกระทู้ ( 81 )
บทความ ( 0 )



สถานะออฟไลน์




สร้าง GD แล้วอยากให้อยู่กึ่งกลาง หน้าจอ ทำไงครับ



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2009-05-30 15:50:32 By : darkninja View : 928 Reply : 13
 

 

No. 1



โพสกระทู้ ( 3,468 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter

ก็เซฟก่อนครับเซฟเป็นภาพก่อน แล้วใช้ html เอา






Date : 2009-05-30 16:07:46 By : pjgunner
 


 

No. 2



โพสกระทู้ ( 81 )
บทความ ( 0 )



สถานะออฟไลน์


แล้วจะ ให้มันอยู่กึ่งกลางเลยไม่ได้หรอครับ
Date : 2009-05-30 16:20:44 By : darkninja
 

 

No. 3



โพสกระทู้ ( 81 )
บทความ ( 0 )



สถานะออฟไลน์


รู้สึกว่า มัน มี อะไรผิด พลาดอะครับท่าน เอี่ยว ผมลองเรียง แบบ นี้ดู แต่มัน วาดให้ไม่หมด อะครับ ไม่รู้เป็น เพราะอะไร

Code
<?php

/**
* phpTreeGraph
* Linux filesystem hierarchy demo
* @author Mathias Herrmann
**/

//include GD rendering class
require_once('./classes/GDRenderer.php');

//create new GD renderer, optinal parameters: LevelSeparation, SiblingSeparation, SubtreeSeparation, defaultNodeWidth, defaultNodeHeight
$objTree = new GDRenderer(30, 10, 30, 50, 40);

//add nodes to the tree, parameters: id, parentid optional title, text, width, height, image(path)
$objTree->add(1,0,'/', '', 10, 20) ;
$objTree->add(2,1,'bin', 'nothing');
$objTree->add(3,1,'boot', 'nothing');
$objTree->add(4,1,'dev', 'nothing');
$objTree->add(5,3,'etc', '', 50, 20);
$objTree->add(6,4,'home', 'nothing');
$objTree->add(7,5,'lib', 'nothing');
$objTree->add(8,7,'lost+found', 'nothing', 100);
$objTree->add(9,8,'mnt', 'nothing');
$objTree->add(10,9,'proc', 'nothing');
$objTree->add(11,9,'root', 'nothing');
$objTree->add(12,10,'sbin', 'nothing');
$objTree->add(13,10,'tmp', 'nothing');
$objTree->add(14,10,'usr', '', 50, 20);
$objTree->add(15,2,'bin', 'nothing');




//$objTree->setNodeLinks(GDRenderer::LINK_BEZIER);

$objTree->setBGColor(array(255, 255, 255));
$objTree->setNodeTitleColor(array(0, 128, 255));
$objTree->setNodeMessageColor(array(0, 192, 255));
$objTree->setLinkColor(array(0, 64, 128));
//$objTree->setNodeLinks(GDRenderer::LINK_BEZIER);
$objTree->setNodeBorder(array(0, 0, 0), 2);
$objTree->setFTFont('/usr/share/fonts/truetype/msttcorefonts/arial.ttf', 10, 0, GDRenderer::CENTER);

$objTree->stream();

?>

Date : 2009-05-30 16:36:04 By : darkninja
 


 

No. 4



โพสกระทู้ ( 81 )
บทความ ( 0 )



สถานะออฟไลน์



Date : 2009-05-30 16:38:16 By : darkninja
 


 

No. 5



โพสกระทู้ ( 3,468 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter

$objTree->stream(); เปลี่ยน เป็น $objTree->save("{$your_file_name}.png");

<img src="<?="{$your_file_name}.png"?>" align="center">

http://gunner.freetzi.com
Date : 2009-05-30 16:46:48 By : pjgunner
 


 

No. 6



โพสกระทู้ ( 81 )
บทความ ( 0 )



สถานะออฟไลน์


คือผมขออภัย ครับ ผมถามผิด เอง คือ ว่า
ปัญหา มันมีอยู่ว่าGD มัน วาด ให้ผมไม่หมด อะครับดูจากภาพด้านบน บรรทัด ล่างสุดมันแหว่งๆ อะครับ
ตอนแรกผมก็นึกว่าเป็นเพราะ เราไม่ได้จัดกึ่งกลาง แต่ตอนนี้เข้าใจล่ะครับว่าปัญหาไม่ใช้ตรงนั้น แต่เป็น มันวาดมาไม่หมด ต่างห่าง ผมลอง ตัด ตัวบรรทัด

$objTree->add(2,1,'bin', 'nothing');
นี้ออก
ผลปรากฎว่า มาหมดทุกแถวเลยครับ แต่พอ ใส่เข้าไป ก็เป็น เหมือนเดิม ก็เลยงงๆ ว่า มันติด ปัญหาตรงไหน อะครับ
จึงมาขอนำแนะนำครับ
Date : 2009-05-30 16:52:51 By : darkninja
 


 

No. 7



โพสกระทู้ ( 3,468 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter

ผมไม่แน่ใจนะ ว่ามันจะคำนวนความกว้างให้อัติโนมัติหรือเปล่า
$objTree = new GDRenderer(30, 10, 30, 50, 40);

พารามิเตอร์ที่ 4, 5 เป็น width กะ height ครับ ลองปรับ width ดู
Date : 2009-05-30 17:03:48 By : pjgunner
 


 

No. 8



โพสกระทู้ ( 81 )
บทความ ( 0 )



สถานะออฟไลน์


นี้คือ Class ที่มันเรียกมาใช้ครับ


Code
<?php

/**
* phpTreeGraph
*
*
* PHP version 5
* @copyright Mathias Herrmann 2007
* @author Mathias Herrmann <[email protected]>
* @author Alex Fomenko <[email protected]>
* @license LGPL
* *
* This PHP class is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This PHP class 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 *
* Lesser General Public License for more details. *
* *
* *
* *
*
*/


require_once('Tree.php');

class GDRenderer extends Tree
{
const LINK_DIRECT = 1;
const LINK_NORMAL = 2;
const LINK_BEZIER = 3;
const LINK_NONE = 4;
const CENTER = 1;
const LEFT = 5;
const RIGHT = 10;
const TOP = 16;
const BOTTOM = 33;

private $linktype;
private $bgColor = array(255, 255, 255);
private $nodeTitleColor = array(0, 128, 255);
private $nodeMessageColor = array(0, 64, 255);
private $borderColor = array();
private $linkColor = array(0, 0, 0);
private $borderWidth = 0;
private $bgImage;
private $textTitleColor = array(0, 0, 0);
private $textMessageColor = array(0, 0, 0);
private $ftFont;
private $ftFontSize;
private $ftFontAngle;
private $Align = self::CENTER;
private $img;


/**
* sets the style of the node connectors
* LINK_DIRECT draw direct lines
* LINK_NORMAL draw normal style
* LINK_BEZIER draw bezier lines
* @param int $type
*/
public function setNodeLinks($type)
{
$this->linktype = $type;
}


/**
* sets the backgroundcolor of the graph
* example array(255, 255, 225) is white
* @param array $arrColor
*/
public function setBGColor($arrColor)
{
$this->bgColor = $arrColor;
}


/**
* sets the node title backgroundcolor
* example array(255, 255, 225) is white
* @param array $arrColor
*/
public function setNodeTitleColor($arrColor)
{
$this->nodeTitleColor = $arrColor;
}


/**
* sets the node message backgroundcolor
* example array(255, 255, 225) is white
* @param array $arrColor
*/
public function setNodeMessageColor($arrColor)
{
$this->nodeMessageColor = $arrColor;
}


/**
* sets the node border
* @param array $arrColor
* @param int $border
*/
public function setNodeBorder($arrColor, $border)
{
$this->borderColor = $arrColor;
$this->borderWidth = $border;
}


/**
* sets the color of the connectors
* example array(0, 0, 0) is black
* @param array $arrColor
*/
public function setLinkColor($arrColor)
{
$this->linkColor = $arrColor;
}


/**
* sets the color of the title text
* example array(0, 0, 0) is black
* @param array $arrColor
*/
public function setTextTitleColor($arrColor)
{
$this->textTitleColor = $arrColor;
}


/**
* sets the color of the message text
* example array(0, 0, 0) is black
* @param array $arrColor
*/
public function setTextMessageColor($arrColor)
{
$this->textMessageColor = $arrColor;
}


/**
* use TrueType Fonts
* Align: CENTER,TOP,BOTTOM,LEFT,RIGHT ex. GDRenderer::TOP|GDRenderer::LEFT
* @param string $font
* @param int $size
* @param int $angle
* @param int $align
*/
public function setFTFont($font, $size, $angle = 0, $align = self::CENTER)
{
if(!file_exists($font))
{
return false;
}
$this->ftFont = $font;
$this->ftFontSize = $size;
$this->ftFontAngle = $angle;
$this->Align = $align;
}

protected function render()
{
if(!extension_loaded('gd'))
{
throw new Exception('GD not loaded!');
}
parent::render();
$this->img = imagecreate($this->getWidth(), $this->getHeight());
imagecolorallocate($this->img, $this->bgColor[0], $this->bgColor[1], $this->bgColor[2]);
$nodeTitleBG = imagecolorallocate($this->img, $this->nodeTitleColor[0], $this->nodeTitleColor[1], $this->nodeTitleColor[2]);
$nodeMessageBG = imagecolorallocate($this->img, $this->nodeMessageColor[0], $this->nodeMessageColor[1], $this->nodeMessageColor[2]);
$linkCol = imagecolorallocate($this->img, $this->linkColor[0], $this->linkColor[1], $this->linkColor[2]);
$textTitleColor = imagecolorallocate($this->img, $this->textTitleColor[0], $this->textTitleColor[1], $this->textTitleColor[2]);
$textMessageColor = imagecolorallocate($this->img, $this->textMessageColor[0], $this->textMessageColor[1], $this->textMessageColor[2]);
if($this->borderWidth > 0)
{
$borderCol = imagecolorallocate($this->img, $this->borderColor[0], $this->borderColor[1], $this->borderColor[2]);
}

// var_dump($this->root);exit;
while($this->hasNext())
{
$node = $this->next();
if(!is_null($node->image) && file_exists($node->image))
{
$path_parts = pathinfo($node->image);
$file_ext = strtolower($path_parts['extension']);
$this->imgsize = getimagesize($node->image);
switch ($file_ext)
{
case 'gif':
$strSourceImage = imagecreatefromgif($node->image);
break;

case 'jpg':
case 'jpeg':
$strSourceImage = imagecreatefromjpeg($node->image);
break;

case 'png':
$strSourceImage = imagecreatefrompng($node->image);
$gdinfo = gd_info();
if (version_compare($gdinfo["GD Version"], '2.0.1', '>='))
{
imageantialias($this->img, true);
imagealphablending($this->img, false);
imagesavealpha($this->img, true);
imagefilledrectangle($this->img, $node->x, $node->y, $node->w, $node->h/2, imagecolorallocatealpha($this->img, 255, 255, 255, 127));
}
break;
}
imagecopyresampled($this->img, $strSourceImage, $node->x, $node->y, 0, 0, $node->w, $node->h, $this->imgsize[0], $this->imgsize[1]);
}
else
{
if(!empty($node->message)){
imagefilledrectangle($this->img, $node->x, $node->y , $node->x + $node->w, $node->y + $node->h/2 , $nodeTitleBG);
imagefilledrectangle($this->img, $node->x, $node->y + $node->h/2 , $node->x + $node->w, $node->y + $node->h , $nodeMessageBG);


}else
imagefilledrectangle($this->img, $node->x, $node->y , $node->x + $node->w, $node->y + $node->h , $nodeTitleBG);
}
if($this->borderWidth > 0)
{
for ($i = 0; $i < $this->borderWidth; $i++)
{
imagerectangle($this->img, $node->x + $i, $node->y + $i, $node->x + $node->w - $i, $node->y + $node->h - $i, $borderCol);
}
}
switch($this->linktype)
{
case self::LINK_DIRECT:
foreach($node->links as $link)
{
imageline ( $this->img, $link['xa'], $link['ya'], $link['xd'], $link['yd'], $linkCol );
}
break;

case self::LINK_BEZIER:
foreach($node->links as $link)
{
for ($t=0;$t<=1;$t=$t+.001)
{
$xt = $link['xa'] * pow((1 - $t), 3) + $link['xb'] * 3 * $t * pow(1-$t, 2) + $link['xc'] * 3 * pow($t, 2) * (1-$t) + $link['xd'] * pow($t, 3);
$yt = $link['ya'] * pow((1 - $t), 3) + $link['yb'] * 3 * $t * pow(1-$t, 2) + $link['yc'] * 3 * pow($t, 2) * (1-$t) + $link['yd'] * pow($t, 3);
imagesetpixel($this->img, $xt, $yt, $linkCol);
}
}
break;
case self::LINK_NONE:
break;

default:
foreach($node->links as $link)
{
imageline ( $this->img, $link['xa'], $link['ya'], $link['xb'], $link['yb'], $linkCol );
imageline ( $this->img, $link['xb'], $link['yb'], $link['xc'], $link['yc'], $linkCol );
imageline ( $this->img, $link['xc'], $link['yc'], $link['xd'], $link['yd'], $linkCol );
}
}
// print_r($this->ftFont);exit;
if(!strlen($this->ftFont))
{
imagestring( $this->img, 4, $node->x + $this->borderWidth, $node->y, $node->title, $textTitleColor);
imagestring( $this->img, 4, $node->x + $this->borderWidth, $node->y+$node->h/2, $node->message, $textMessageColor);
}
else
{
$fttext = imageftbbox($this->ftFontSize, $this->ftFontAngle, $this->ftFont, $node->title);
$x = ($fttext[0] <= $fttext[6]) ? $fttext[0] : $fttext[6];
$y = ($fttext[5] >= $fttext[7]) ? $fttext[5] : $fttext[7];
$x2 = ($fttext[2] >= $fttext[4]) ? $fttext[2] : $fttext[4];
$y2 = ($fttext[1] >= $fttext[3]) ? $fttext[1] : $fttext[3];
$w['title'] = $x2 - $x;
$h['title'] = $y2 - $y;
$left['title'] = ($node->w - $w['title']) / 2;
if(!empty($node->message))
$top['title'] = ($node->h/2 - $h['title']) / 2;
else
$top['title'] = ($node->h - $h['title']) / 2;

$fttext = imageftbbox($this->ftFontSize, $this->ftFontAngle, $this->ftFont, $node->message);
$x = ($fttext[0] <= $fttext[6]) ? $fttext[0] : $fttext[6];
$y = ($fttext[5] >= $fttext[7]) ? $fttext[5] : $fttext[7];
$x2 = ($fttext[2] >= $fttext[4]) ? $fttext[2] : $fttext[4];
$y2 = ($fttext[1] >= $fttext[3]) ? $fttext[1] : $fttext[3];
$w['message'] = $x2 - $x;
$h['message'] = $y2 - $y;
$left['message'] = ($node->w - $w['message']) / 2;
$top['message'] = $node->h/2 + ($node->h/2 - $h['message']) / 2;

switch($this->Align)
{
case self::CENTER|self::CENTER :
if(!empty($node->message)){
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['title'], $node->y + $top['title'] + $h['title'], $textTitleColor , $this->ftFont , $node->title);
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['message'], $node->y + $top['message'] + $h['message'], $textMessageColor , $this->ftFont , $node->message);
}else
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['title'], $node->y + $top['title'] + $h['title'], $textTitleColor , $this->ftFont , $node->title);
break;
case self::CENTER|self::TOP:
case self::TOP :
if(!empty($node->message)){
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['title'], $node->y + $this->borderWidth + $h['title'], $textTitleColor , $this->ftFont , $node->title);
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['message'], $node->y + $node->h/2 + $this->borderWidth + $h['message'], $textMessageColor , $this->ftFont , $node->message);
}else
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['title'], $node->y + $this->borderWidth + $h['title'], $textTitleColor , $this->ftFont , $node->title);
break;
case self::CENTER|self::BOTTOM :
if(!empty($node->message)){
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['title'], $node->y + $node->h/2 - $this->borderWidth , $textTitleColor , $this->ftFont , $node->title);
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['message'], $node->y + $node->h - $this->borderWidth , $textMessageColor , $this->ftFont , $node->message);
}else
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['title'], $node->y + $node->h['title'] - $this->borderWidth , $textTitleColor , $this->ftFont , $node->title);
break;
case self::LEFT|self::CENTER :
if(!empty($node->message)){
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $this->borderWidth, $node->y + $top['title'] + $h['title'], $textTitleColor , $this->ftFont , $node->title);
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $this->borderWidth, $node->y + $top['message'] + $h['message'], $textMessageColor , $this->ftFont , $node->message);
}else
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $this->borderWidth, $node->y + $top['title'] + $h['title'], $textTitleColor , $this->ftFont , $node->title);
break;
case self::LEFT|self::TOP :
if(!empty($node->message)){
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $this->borderWidth, $node->y + $this->borderWidth + $h['title'] , $textTitleColor , $this->ftFont , $node->title);
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $this->borderWidth, $node->y + $node->h/2 + $this->borderWidth + $h['message'], $textMessageColor , $this->ftFont , $node->message);
}else
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $this->borderWidth, $node->y + $this->borderWidth + $h['title'] , $textTitleColor , $this->ftFont , $node->title);
break;
case self::LEFT|self::BOTTOM :
if(!empty($node->message)){
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle ,$node->x + $this->borderWidth, $node->y + $node->h/2 - $this->borderWidth, $textTitleColor , $this->ftFont , $node->title);
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $this->borderWidth, $node->y + $node->h - $this->borderWidth, $textMessageColor , $this->ftFont , $node->message);
}else
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle ,$node->x + $this->borderWidth, $node->y + $node->h - $this->borderWidth, $textTitleColor , $this->ftFont , $node->title);
break;
case self::RIGHT|self::CENTER :
case self::RIGHT :
if(!empty($node->message)){
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $node->w - $this->borderWidth - $w['title'], $node->y + $top['title'] + $h['title'], $textTitleColor , $this->ftFont , $node->title);
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $node->w - $this->borderWidth - $w['message'], $node->y + $top['message'] + $h['message'], $textMessageColor , $this->ftFont , $node->message);
}else
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $node->w - $this->borderWidth - $w['title'], $node->y + $top['title'] + $h['title'], $textTitleColor , $this->ftFont , $node->title);
break;
case self::RIGHT|self::TOP :
if(!empty($node->message)){
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $node->w - $this->borderWidth - $w['title'], $node->y + $this->borderWidth + $h['title'] , $textTitleColor , $this->ftFont , $node->title);
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $node->w - $this->borderWidth - $w['message'], $node->y + $node->h/2 + $this->borderWidth + $h['message'], $textMessageColor , $this->ftFont , $node->message);
}else
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $node->w - $this->borderWidth - $w['title'], $node->y + $this->borderWidth + $h['title'] , $textTitleColor , $this->ftFont , $node->title);
break;
case self::RIGHT|self::BOTTOM :
if(!empty($node->message)){
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle ,$node->x + $node->w - $this->borderWidth - $w['title'], $node->y + $node->h/2 - $this->borderWidth, $textTitleColor , $this->ftFont , $node->title);
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $node->w - $this->borderWidth - $w['message'], $node->y + $node->h - $this->borderWidth, $textMessageColor , $this->ftFont , $node->message);
}else
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle ,$node->x + $node->w - $this->borderWidth - $w['title'], $node->y + $node->h - $this->borderWidth, $textTitleColor , $this->ftFont , $node->title);
break;
default:
if(!empty($node->message)){
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['title'], $node->y + $this->borderWidth + $h['title'], $textTitleColor , $this->ftFont , $node->title);
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['message'], $node->y + $this->borderWidth + $h['message'], $textMessageColor , $this->ftFont , $node->message);
}else
imagefttext( $this->img , $this->ftFontSize , $this->ftFontAngle , $node->x + $left['title'], $node->y + $this->borderWidth + $h['title'], $textTitleColor , $this->ftFont , $node->title);
}
}
}
}


/**
* get the image as stream
*/
public function stream()
{
if(empty($this->img))
{
$this->render();
}
header('Content-type: image/png');
imagepng($this->img);
}


/**
* save the image to file
* @param string $file
*/

public function save($file)
{
if(empty($this->img))
{
$this->render();
}
imagepng($this->img, $file);
}
}
?>

Date : 2009-05-30 18:23:59 By : darkninja
 


 

No. 9



โพสกระทู้ ( 11,835 )
บทความ ( 10 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์


คงต้องแปะ Tree.php ให้คุณเอี่ยวเค้าด้วยนะครับ
Date : 2009-05-30 18:27:09 By : plakrim
 


 

No. 10



โพสกระทู้ ( 81 )
บทความ ( 0 )



สถานะออฟไลน์


555+ อะครับ
Tree.php
Code
<?php

/**
* phpTreeGraph
*
*
* PHP version 5
* @copyright Mathias Herrmann 2007
* @author Mathias Herrmann <[email protected]>
* @author Alex Fomenko <[email protected]>
* @license LGPL
* *
* This PHP class is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This PHP class 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 *
* Lesser General Public License for more details. *
* *
* *
* *
*
*/

if (version_compare(phpversion(), '5.0.0', '<'))
{
die('You need at least PHP version 5.0.0!');
}
require_once('Node.php');

class Tree{
private $maxLevelHeight = array();
private $maxLevelWidth = array();
private $previousLevelNode = array();
private $nodes = array();
private $root;
private $LevelSeparation;
private $SiblingSeparation;
private $SubtreeSeparation;
private $defaultHeight;
private $defaultWidth;
private $isRendered = false;
private $position = 0;
private $h = 0;
private $w = 0;
private $marginX;
private $marginY;


/**
* Constructor
* @param int $LevelSeparation optional
* @param int $SiblingSeparation optional
* @param int $SubtreeSeparation optional
* @param int $defaultWidth optional
* @param int $defaultHeight optional
* @param int $marginX optional
* @param int $marginY optional
*/
public function __construct($LevelSeparation=40, $SiblingSeparation=40, $SubtreeSeparation=80, $defaultWidth=40, $defaultHeight=40, $marginX = 5, $marginY = 5) {
$this->root = new Node(0, 0, 0, 0, "");
$this->LevelSeparation = $LevelSeparation;
$this->SiblingSeparation = $SiblingSeparation;
$this->SubtreeSeparation = $SubtreeSeparation;
$this->defaultHeight = $defaultHeight;
$this->defaultWidth = $defaultWidth;
$this->marginX = $marginX;
$this->marginY = $marginY;
}


/**
* function to add a node to the tree
* @param int id
* @param int $pid id of the parent node, false or 0 is there is no parent
* @param string $message optional
* @param int $w optional
* @param int $h optional
* @param string $image path to image optional
*/
public function add($id, $pid, $title='', $message='', $w=0, $h=0, $image = null ) {
$w = ($w == 0) ? $this->defaultWidth : $w;
$h = ($h == 0) ? $this->defaultHeight : $h;
if(empty($id))
die("Error id = $id");
if(isset($this->nodes[$id])){
$node = $this->nodes[$id];
$node->pid = ($pid !== false)? $pid : 0;
$node->w = $w;
$node->h = $h;
$node->title = $title;
$node->message = $message;
$node->image = $image;
if($pid){
unset($this->root->childs[$id]);
}
}else
$node = new Node($id, ($pid !== false)? $pid : 0, $w, $h, $title, $message, $image);
if(empty($pid)){
$pnode = $this->root;
$node->nodeParent = $pnode;
$pnode->childs[$id] = $node;
}elseif(isset($this->nodes[$pid])){
$pnode = $this->nodes[$pid];
$node->nodeParent = $pnode;
$pnode->childs[] = $node;
}else{
$this->add($pid, false, 'Not defined');
$pnode = $this->nodes[$pid];
$node->nodeParent = $pnode;
$pnode->childs[] = $node;
}
$this->nodes[$id] = $node;

}

private function firstwalk($node, $level){
$this->setLevelHeight($node, $level);
$this->setLevelWidth($node, $level);
$this->setNeighbors($node, $level);
if($node->numChilds()==0){
$leftSibling = $node->getLeftSibling();
if($leftSibling){
$node->prelim = $leftSibling->prelim + $leftSibling->w + $this->SiblingSeparation;
}else{
$node->prelim = 0;
}
}else{
$n = $node->numChilds();
for($i = 0; $i < $n; $i++){
$this->firstWalk($node->getChildAt($i), $level + 1);
}
$midPoint = $node->getChildrenCenter();
$midPoint -= $node->w / 2;
$leftSibling = $node->getLeftSibling();
if($leftSibling){
$node->prelim = $leftSibling->prelim + $leftSibling->w + $this->SiblingSeparation;
$node->modifier = $node->prelim - $midPoint;
$this->apportion($node, $level);
}else{
$node->prelim = $midPoint;
}
}
}

private function secondWalk($node, $level, $x=0, $y=0){
$xTmp = $node->prelim + $x;
$yTmp = $y;
$maxsizeTmp = ($level == 0) ? $this->defaultHeight : $this->maxLevelHeight[$level];
$node->x = $xTmp + $this->marginX;
$node->y = $yTmp - $this->defaultHeight;
$this->h = ($this->h > $node->y + $node->h) ? $this->h : $node->y + $node->h;
$this->w = ($this->w > $node->x + $node->w) ? $this->w : $node->x + $node->w + $this->marginY;

if($node->numChilds()){
$this->secondWalk($node->getChildAt(0), $level + 1, $x + $node->modifier, $y + $maxsizeTmp + (($level == 0) ? $this->marginY : $this->LevelSeparation));
}
$rightSibling = $node->getRightSibling();
if($rightSibling){
$this->secondWalk($rightSibling, $level, $x, $y);
}
}

private function apportion($node, $level){
$firstChild = $node->getChildAt(0);
$firstChildLeftNeighbor = $firstChild->leftNeighbor;
for($j = 1; $firstChild && $firstChildLeftNeighbor && $j <= $level;){
$modifierSumRight = 0;
$modifierSumLeft = 0;
$rightAncestor = $firstChild;
$leftAncestor = $firstChildLeftNeighbor;
for($l = 0; $l < $j; $l++){
$rightAncestor = $rightAncestor->nodeParent;
$leftAncestor = $leftAncestor->nodeParent;
$modifierSumRight += $rightAncestor->modifier;
$modifierSumLeft += $leftAncestor->modifier;
}

$totalGap = ($firstChildLeftNeighbor->prelim + $modifierSumLeft + $firstChildLeftNeighbor->w + $this->SubtreeSeparation) - ($firstChild->prelim + $modifierSumRight);
if($totalGap > 0){
$subtreeAux = $node;
$numSubtrees = 0;

for(; $subtreeAux && $subtreeAux !== $leftAncestor; $subtreeAux = $subtreeAux->getLeftSibling()){
$numSubtrees++;
}

if($subtreeAux){
$subtreeMoveAux = $node;
$singleGap = $totalGap / $numSubtrees;
for(; $subtreeMoveAux !== $leftAncestor; $subtreeMoveAux = $subtreeMoveAux->getLeftSibling()){
$subtreeMoveAux->prelim += $totalGap;
$subtreeMoveAux->modifier += $totalGap;
$totalGap -= $singleGap;
}
}
}
$j++;
if($firstChild->numChilds() == 0){
$firstChild = $this->getLeftmost($node, 0, $j);
}else{
$firstChild = $firstChild->getChildAt(0);
}
if($firstChild){
$firstChildLeftNeighbor = $firstChild->leftNeighbor;
}
}
}

private function setLevelHeight($node, $level){
if (!isset($this->maxLevelHeight[$level])){
$this->maxLevelHeight[$level] = 0;
}
if($this->maxLevelHeight[$level] < $node->h){
$this->maxLevelHeight[$level] = $node->h;
}
}

private function setLevelWidth($node, $level){
if (!isset($this->maxLevelWidth[$level])){
$this->maxLevelWidth[$level] = 0;
}
if($this->maxLevelWidth[$level] < $node->w){
$this->maxLevelWidth[$level] = $node->w;
}
}

private function setNeighbors($node, $level){
$node->leftNeighbor = (isset($this->previousLevelNode[$level])) ? $this->previousLevelNode[$level] : 0 ;
if($node->leftNeighbor){
$node->leftNeighbor->rightNeighbor = $node;
}
$this->previousLevelNode[$level] = $node;
}

private function getLeftmost($node, $level, $maxlevel){
if($level >= $maxlevel){
return $node;
}
if(($n=$node->numChilds())==0){
return false;
}
for($i = 0; $i < $n; $i++){
$iChild = $node->getChildAt($i);
$leftmostDescendant = $this->getLeftmost($iChild, $level + 1, $maxlevel);
if($leftmostDescendant){
return $leftmostDescendant;
}
}
return 0;
}

protected function render(){
sort($this->root->childs);
sort($this->nodes);
$this->nodes[$this->count()] = $this->nodes[0];
unset($this->nodes[0]);
$this->firstwalk($this->root, 0);
$this->secondWalk($this->root, 0, 0);
foreach($this->nodes as $node){
$node->getLinks();
}
$this->isRendered = true;
}

public function getWidth(){
if(!$this->isRendered){
$this->render();
}
return $this->w;
}

public function getHeight(){
if(!$this->isRendered){
$this->render();
}
return $this->h + $this->marginY;
}

public function count(){
return count($this->nodes);
}


/**
* iterator function to get the nodes
* @return mixed
*/
public function next(){
if(!$this->isRendered){
$this->render();
}
if(isset($this->nodes[$this->position+1])){
$this->position++;
return $this->nodes[$this->position];
}else{
return false;
}
}

/**
* @return boolean
*/
public function hasNext(){
if(!isset($this->nodes[$this->position+1]) ){
return false;
}
return true;
}

public function getNodeAt($i){
if(!isset($this->nodes[$i])){
return false;
}else{
return $this->nodes[$i];
}
}

public function setRootNode($id){
$this->nodes[$id]->nodeParent = $this->root;
$this->root->childs = array(1 => $this->nodes[$id]);
$showNodes = array();
$this->setShowNodes($this->nodes[$id], $showNodes);
foreach($this->nodes as $id => $node){
if(!in_array($id,$showNodes))
unset($this->nodes[$id]);
}
}

private function setShowNodes($node, &$showNodes){
$showNodes[] = $node->id;
if($node->numChilds()){
foreach($node->childs as $child){
$this->setShowNodes($child, $showNodes);
}
}
}
}

?>

Date : 2009-05-30 18:41:19 By : darkninja
 


 

No. 11



โพสกระทู้ ( 81 )
บทความ ( 0 )



สถานะออฟไลน์


แล้วนี้ก็
Node.php

Code
<?php

/**
* phpTreeGraph
*
*
* PHP version 5
* @copyright Mathias Herrmann 2007
* @author Mathias Herrmann <[email protected]>
* @author Alex Fomenko <[email protected]>
* @license LGPL
* *
* This PHP class is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This PHP class 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 *
* Lesser General Public License for more details. *
* *
* *
* *
*
*/



class Node
{
public $id = 0;
public $h = 0;
public $w = 0;
public $x = 0;
public $y = 0;
public $message;
public $title;
public $image;
public $leftNeighbor = 0;
public $rightNeighbor = 0;
public $prelim = 0;
public $modifier = 0;
public $childs = array();
public $nodeParent = 0;
public $links = array();

public function __construct($id, $pid, $w, $h, $title = '', $message = '', $image = null)
{
$this->id = $id;
$this->pid = $pid;
$this->w = $w;
$this->h = $h;
$this->title = $title;
$this->message = $message;
$this->image = $image;
}

public function numChilds()
{
return count($this->childs);
}

public function getLeftSibling()
{
if($this->leftNeighbor && $this->leftNeighbor->nodeParent === $this->nodeParent)
{
return $this->leftNeighbor;
}
else
{
return false;
}
}

public function getRightSibling()
{
if($this->rightNeighbor && $this->rightNeighbor->nodeParent === $this->nodeParent)
{
return $this->rightNeighbor;
}
else
{
return false;
}
}

public function getChildAt($i)
{
if(isset($this->childs[$i]))
{
return $this->childs[$i];
}
else
{
return false;
}
}

public function getChildrenCenter()
{
$node = $this->getChildAt(0);
$node1 = $this->getChildAt(count($this->childs)-1);
return $node->prelim + (($node1->prelim - $node->prelim) + $node1->w) / 2;
}

public function getLinks()
{
$xa = 0; $ya = 0; $xb = 0; $yb = 0; $xc = 0; $yc = 0; $xd = 0; $yd = 0;
$xa = $this->x + ($this->w / 2);
$ya = $this->y + $this->h;

foreach($this->childs as $child)
{
$xd = $xc = $child->x + ($child->w / 2);
$yd = $child->y;
$xb = $xa;
$yb = $yc = $ya + ($yd - $ya) / 2;
$this->links[$child->id]['xa'] = $xa;
$this->links[$child->id]['ya'] = $ya;
$this->links[$child->id]['xb'] = $xb;
$this->links[$child->id]['yb'] = $yb;
$this->links[$child->id]['xc'] = $xc;
$this->links[$child->id]['yc'] = $yc;
$this->links[$child->id]['xd'] = $xd;
$this->links[$child->id]['yd'] = $yd;
}
}

public function __toString()
{
return (string)$this->id;
}

}

?>

Date : 2009-05-30 18:41:53 By : darkninja
 


 

No. 12



โพสกระทู้ ( 3,468 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter

public function __construct($LevelSeparation=40, $SiblingSeparation=40, $SubtreeSeparation=80, $defaultWidth=40, $defaultHeight=40, $marginX = 5, $marginY = 5) {
$this->root = new Node(0, 0, 0, 0, "");
$this->LevelSeparation = $LevelSeparation;
$this->SiblingSeparation = $SiblingSeparation;
$this->SubtreeSeparation = $SubtreeSeparation;
$this->defaultHeight = $defaultHeight;
$this->defaultWidth = $defaultWidth;
$this->marginX = $marginX;
$this->marginY = $marginY;
}


ลองปรับ defaultWidth ดูครับ ไม่ก็ใส่ margin ลงไปด้วยครับ

ผมลองโปลดมาดูแล้วหละครับเรื่องแมพ เขาไม่ได้ ทำเมธอด bound มาให้ครับ คงต้องแก้เอาเอง

สังเกตดูที่ demo1.php แล้ว

$objTree = new GDRenderer(30, 10, 30, 50, 40);

แต่รูปออกมาได้กว้าง 240px ก็ไม่น่าจะมีปัญหานะครับ

หรือว่า ตัวนี้ยังไม่ stable ไม่แน่ใจเหมือนกันครับ



หรือว่า มันอาจต้องใส่ตามลำดับด้วยครับสังเกต โค้ดของคุณตรงนี้ครับ
$objTree->add(15,2,'bin', 'nothing'); ตรง พารามิเตอร์ที่สอง ตามตัวอย่างแล้วผมเห็นมันเรียงลำดับนะครับ


$objTree->add(1,0,'I\'m top', '', 10, 20);
$objTree->add(2,1,'bin', 'nothing');
$objTree->add(3,1,'boot', 'nothing');
$objTree->add(4,1,'dev', 'nothing');
$objTree->add(5,1,'etc', '', 50, 20);
$objTree->add(6,1,'home', 'nothing');
$objTree->add(7,1,'lib', 'nothing');
$objTree->add(8,1,'lost+found', 'nothing', 100);
$objTree->add(9,1,'mnt', 'nothing');
$objTree->add(10,1,'proc', 'nothing');
$objTree->add(11,1,'root', 'nothing');
$objTree->add(12,1,'sbin', 'nothing');
$objTree->add(13,1,'tmp', 'nothing');
$objTree->add(14,1,'usr', '', 50, 20);
$objTree->add(15,1,'var', '', 50, 20);
$objTree->add(16,5,'rc.d', 'nothing');
$objTree->add(17,5,'skel', 'nothing');
$objTree->add(18,5,'X11', 'nothing');
$objTree->add(19,14,'bin', 'nothing');
$objTree->add(20,14,'local', 'nothing');
$objTree->add(21,14,'include', 'nothing');
$objTree->add(22,14,'lib', 'nothing');
$objTree->add(23,14,'man', 'nothing');
$objTree->add(24,14,'sbin', 'nothing');
$objTree->add(25,14,'src', 'nothing');
$objTree->add(26,14,'X11 R6', 'nothing');
$objTree->add(27,15,'tmp', 'nothing');
$objTree->add(28,15,'spool', '', 50, 20);
$objTree->add(29,20,'bin', 'nothing');
$objTree->add(30,20,'sbin', 'nothing');
$objTree->add(31,25,'linux', 'nothing');
$objTree->add(32,28,'lpd', 'nothing');
$objTree->add(33,28,'mail', 'nothing');
$objTree->add(34,28,'uucp', 'nothing');
$objTree->add(35,28,'cron', 'nothing');

บายครับ คงไม่ได้เข้ามาอีกนาน
Date : 2009-05-30 18:51:02 By : pjgunner
 


 

No. 13



โพสกระทู้ ( 81 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณมากๆ ครับ ที่ช่วยเหลือ มาตลอก ครับ
ผมลองใส่ margin เข้าไป ผลปรากฏว่า ได้ผลครับ ออกมาหมดเลย ยังไง ก็ขอบคุณมากๆ ครับ
Date : 2009-05-30 19:03:54 By : darkninja
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สร้าง GD แล้วอยากให้อยู่กึ่งกลาง หน้าจอ ทำไงครับ สร้าง GD แล้วอยากให้อยู่กึ่งกลาง หน้าจอ ทำไงครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่