2010年5月28日 星期五

GD_week14

HTML的部份:

零件編號: 設計者姓名: 零件長度: 零件寬度:
PHP的部份:
<?php
// week14_action.php
// 處理 week14_form.htm 所送回來的表單資料,並藉以直接顯示出對應的圖檔

// 取得表單傳回來的變數值
$number = $_POST["number"];
$name = $_POST["name"];
$length = $_POST["length"];
$width = $_POST["width"];

// 以下開始根據所取得的表單資料 show 出圖檔
    /*由於處理程式,直接假設取值不會有問題,因此不使用任何 echo 指令,傳送文字檔資料,
      因此 output buffer 中不會有資料,因此無需使用 ob_clean()進行清除*/
    header("Content-type: image/png");
//1.設定圖片大小 --------------------------------------------------------//
    $im = imagecreatetruecolor($length,$width);
    //$bgcolor = ImageColorAllocate ($im, Red(0~255), Green(0~255), Blue(0~255));
//2.設定背景顏色填入淺藍色底色 ------------------------------------------//
    $bgcolor = ImageColorAllocate ($im, 153, 204, 255);
    imagefill($im, 0, 0, $bgcolor);
    
//3.用線來畫邊框 --------------------------------------------------------//
    $black = ImageColorAllocate ($im, 0, 0, 0);
    // 畫框的一些參數
    $thick = 20;
    $margin = 40;
    // 畫最上方平行線
    $startx = 0 + $margin;
    $starty = 0 + $margin;
    $endx = $length - $margin;
    $endy = $starty;
    //imagelinethick($image,x1,y1,x2,y2,顏色,粗細);
    imagelinethick($im,$startx,$starty,$endx,$endy,$black,$thick);
    
//4.寫上編號與姓名 ------------------------------------------------------//
    $string = "Number:".$number."\nName:".$name;
    $font = "fireflysung.ttf";
    //imagettftext(resource $image,大小,角度,X座標,Y座標,顏色,字型,文字)
    ImageTTFText($im, 20, 0, ($length/2)-40,($width/2)-10, $black, $font, $string);
    
//5.畫箭頭 --------------------------------------------------------------//
    $red = ImageColorAllocate ($im, 255, 0, 0);
    //arrow($image,x1,y1,x2,y2,箭頭長度,箭頭寬度,顏色);
    arrow($im, 150,150, 350,150, $length/50, $width/50, $red);
    
//6.結束繪圖 ------------------------------------------------------------//
    imagepng($im);
    imagedestroy($im);

//-----------------------------------------------------------------------

//以下函式直接取自 http://www.php.net/manual/en/function.imageline.php
function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
    if ($thick == 1) {
        return imageline($image, $x1, $y1, $x2, $y2, $color);
    }
    $t = $thick / 2 - 0.5;
    if ($x1 == $x2 || $y1 == $y2) {
        return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
    }
    $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
    $a = $t / sqrt(1 + pow($k, 2));
    $points = array(
        round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
        round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
        round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
        round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
    );
    imagefilledpolygon($image, $points, 4, $color);
    return imagepolygon($image, $points, 4, $color);
}

// 以下函式直接取自 http://php.net/manual/en/function.imageline.php
function arrow($im, $x1, $y1, $x2, $y2, $alength, $awidth, $color) {
    $distance = sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2));
    $dx = $x2 + ($x1 - $x2) * $alength / $distance;
    $dy = $y2 + ($y1 - $y2) * $alength / $distance;
    $k = $awidth / $alength;
    $x2o = $x2 - $dx;
    $y2o = $dy - $y2;
    $x3 = $y2o * $k + $dx;
    $y3 = $x2o * $k + $dy;
    $x4 = $dx - $y2o * $k;
    $y4 = $dy - $x2o * $k;
    imagelinethick($im, $x1, $y1, $dx, $dy, $color,3);
    imagefilledpolygon($im, array($x2, $y2, $x3, $y3, $x4, $y4), 3, $color);
}
?>

沒有留言:

張貼留言