顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

imagecreatefrombmp用PHP讀取BMP

<?php
    function imagecreatefrombmp($p_sFile)
    {
        //    Load the image into a string
        $file    =    fopen($p_sFile,"rb");
        $read    =    fread($file,10);
        while(!feof($file)&&($read<>""))
            $read    .=    fread($file,1024);
       
        $temp    =    unpack("H*",$read);
        $hex    =    $temp[1];
        $header    =    substr($hex,0,108);
        //    Process the header
        //    Structure: http://www.fastgraph.com/help/bmp_header_format.html
        if (substr($header,0,4)=="424d")
        {
            //    Cut it in parts of 2 bytes
            $header_parts    =    str_split($header,2);
            //    Get the width        4 bytes
            $width            =    hexdec($header_parts[19].$header_parts[18]);
            //    Get the height        4 bytes
            $height            =    hexdec($header_parts[23].$header_parts[22]);
            //    Unset the header params
            unset($header_parts);
        }
        //    Define starting X and Y
        $x                =    0;
        $y                =    1;
        //    Create newimage
        $image            =    imagecreatetruecolor($width,$height);
        //    Grab the body from the image
        $body            =    substr($hex,108);
        //    Calculate if padding at the end-line is needed
        //    Divided by two to keep overview.
        //    1 byte = 2 HEX-chars
        $body_size        =    (strlen($body)/2);
        $header_size    =    ($width*$height);
        //    Use end-line padding? Only when needed
        $usePadding        =    ($body_size>($header_size*3)+4);
        //    Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
        //    Calculate the next DWORD-position in the body
        for ($i=0;$i<$body_size;$i+=3)
        {
            //    Calculate line-ending and padding
            if ($x>=$width) {
                //    If padding needed, ignore image-padding
                //    Shift i to the ending of the current 32-bit-block
                if ($usePadding)
                    $i    +=    $width%4;
                //    Reset horizontal position
                $x    =    0;
                //    Raise the height-position (bottom-up)
                $y++;
                //    Reached the image-height? Break the for-loop
                if ($y>$height)
                    break;
            }
            //    Calculation of the RGB-pixel (defined as BGR in image-data)
            //    Define $i_pos as absolute position in the body
            $i_pos    =    $i*2;
            $r        =    hexdec($body[$i_pos+4].$body[$i_pos+5]);
            $g        =    hexdec($body[$i_pos+2].$body[$i_pos+3]);
            $b        =    hexdec($body[$i_pos].$body[$i_pos+1]);
            $gray     =    (int)($r + $g + $b)/3;
            //    Calculate and draw the pixel
            $color    =    imagecolorallocate($image,$gray,$gray,$gray);
            imagesetpixel($image,$x,$height-$y,$color);
            //    Raise the horizontal position
            $x++;
        }
        //    Unset the body / free the memory
        unset($body);
        //    Return image-object
        return $image;
    }
    
    

Minimum spanning tree用PHP寫

<?php
    class Edge
    {
        public $v1;
        public $v2;
        public $weight;
       
        function Edge($v1, $v2, $weight)
        {
            $this->v1 = $v1;
            $this->v2 = $v2;
            $this->weight = $weight;
        }
    }
   
    class Vertex
    {
        public $nRank;
        public $vRoot;
        public $name;

        function Vertex($name)
        {
            $this->name = $name;
            $this->nRank = 0;
            $this->vRoot = $this;
        }

        function GetRoot()
        {
            if ($this->vRoot != $this)
                $this->vRoot = $this->vRoot->GetRoot();
            return $this->vRoot;
        }
    }
   
    class Kruskal
    {
        var $all_vertex = array();
        var $all_edge = array();
       
        function create()
        {
               //create
        }

        function edge_sort()
        {
            function cmp($a, $b)
            {
                if ($a->weight == $b->weight)
                    return 0;
                return ($a->weight < $b->weight) ? -1 : 1;
            }
            usort($this->all_edge, "cmp");
        }
       
        function Join(Vertex $vRoot1, Vertex $vRoot2)
        {
            if ($vRoot2->nRank < $vRoot1->nRank)
                $vRoot2->vRoot = $vRoot1;
            else {
                $vRoot1->vRoot = $vRoot2;
                if ($vRoot1->nRank == $vRoot2->nRank)
                    $vRoot1->nRank++;
            }
        }
       
        function mst_create()
        {
            $mst_vertex = array();

            $this->edge_sort();

            for ($i=0;$i<count($this->all_edge);$i++) {
                $v1 = $this->all_edge[$i]->v1->GetRoot();
                $v2 = $this->all_edge[$i]->v2->GetRoot();

                if($v1->name==$v2->name)
                    continue;
               
                $this->Join($v1, $v2);
               
                array_push($mst_vertex, $this->all_edge[$i]);
            }
            return $mst_vertex;
        }
    }

CodeIgniter安裝

1.codeigniter 下載 http://codeigniter.com/


2.檔案裡面有
codeigniter_版本號碼
 └┬application-你撰寫存放的地方
├system-系統核心的地方(最好不要修改)
├user_guide-說明書
├index.php-設定基本參數
└license.txt-權利書


3.將application、system、index.php複製到網頁目錄

4.設定rewrite
linux:
a2enmod rewrite
windows:
修改apache的httpd.conf將
#LoadModule rewrite_module modules/mod_rewrite.so
前面的#去掉

設定完成都要重新啟動apache


5.建立.htaccess
linux:
直接在跟index.php同層打 vi .htaccess
輸入下面code存檔即可
windows:
因為windows不支援沒檔名的檔案創立
所以在application或是在system裡面都有此檔案
複製到跟index.php同層
用筆記本開起編輯

將.htaccess
內容編輯為
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

RewriteBase / 要設定為你放在跟目錄哪裡
例如:你開啟網頁的網址為http://localhost/index.php
那就是RewriteBase /
要是網址為http://localhost/CI/index.php
就要改成RewriteBase /CI/

RewriteCond %{REQUEST_URI} ^system.*
RewriteCond %{REQUEST_URI} ^application.*
是用來保護程式的假設你資料夾名稱有改
上面的名稱也要改
這樣才會保護到你的程式

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
是用來判斷是否有資料夾或是資料
例如:你在網址打http://localhost/css/
你下面沒有css的資料夾他就會導向其他地方
假設下面有css的資料夾他就不會導向

RewriteRule ^(.*)$ index.php?/$1 [L]
將RewriteBase後面的所有東西
都導向index.php?
例如: http://localhost/CI/welcome
在系統裡面就會變成 http://localhost/CI/index.php?/welcome
外面仍然是http://localhost/CI/welcome


6.設定config.php
放在application→config
將config.php裡面修改
$config['base_url']    = 'http://localhost/';
改成你自己的domain name
有放在目錄下要加上去
$config['index_page'] = 'index.php';
將index.php去除

7.設定database.php
放在application→config
將config.php裡面修改
$db['default']['hostname'] = 'localhost';//databaase的位置

$db['default']['username'] = 'root';//帳號
$db['default']['password'] = 'root123';//密碼
$db['default']['database'] = '';//資料庫名稱
$db['default']['dbdriver'] = 'mysql';//資料庫類型


8.開啟網頁
首先先輸入正常的跟目錄網址
要是看到跟下面一樣的圖
就是成功一半了
接下來就在和後面加上welcome也是跟下面一樣代表就是安裝成功了

smarty-1

smarty
是一個可以將php與html分開的libs下載處:http://www.smarty.net/download.php
首頁:http://www.smarty.net/

安裝方法:
解壓縮到網頁的資料夾底下
資料夾名稱位子隨便只要你記住

使用方法:

在要使用smarty的php網頁code裡
加入
require_once('C:/www/lib/Smarty.class.php');//smarty一定要include的檔案

$smarty = new Smarty(); //新增一個新物件

//這裡要設定,也要先開資料夾給它們
$smarty->template_dir = 'C:/www/templates/'; //這裡是指向你樣板放的地方,也就是HTML的地方
$smarty->compile_dir = 'C:/www/templates_c/'; //這裡是指向暫存檔,一定要有的
$smarty->config_dir = 'C:/www/configs/'; //這個我不知道耶,沒用過
$smarty->cache_dir = 'C:/www/cache/'; //這個我也不知道耶,也沒用過

$smarty->left_delimiter = '<{'; //這裡一定要設,因為預設為{},很容易衝突到
$smarty-right_delimiter= '}>'; //這是用在HTML裡,使smarty知道它是變數

//$smarty->debugging = true; //他會跳出視窗,顯現出詳細內容,變數,使用的HTML等等
//可以先註解起來,要用的時候再取消會比較方便

接下來要傳值給smarty的變數裡使用的語法
$smarty->assign('title', $title);
'title' 是在HTML裡的設的變數名稱
$title 是要傳給它的值

接下來就是合成HTML的語法
$smarty->display('smarty.htm');
smarty.htm 為你放在templates底下你想要合成的檔案名稱

還要有HTML的檔案
名稱副檔名都隨便
只要是html語法就可以
在HTML裡想要被smarty讀到都用<{}>來括起來 //前提是有做上面的設定
這樣smarty才能讓php和html合成起來放在templates_c裡面

<{$var}> //為變數,只要有assign給smarty,就會合成上去


範例:
smarty.htm
<html>
  <head>
    <title><{$title}></title>
  </head>
  <body>
    <{$body}>
  </body>
</html>
smarty.php
<?php
  require_once('C:/www/lib/Smarty.class.php');
  $smarty->template_dir = 'C:/www/templates/';
  $smarty->compile_dir = 'C:/www/templates_c/';
  $smarty->config_dir = 'C:/www/configs/';
  $smarty->cache_dir = 'C:/www/cache/';
  $smarty->left_delimiter = '<{';
  $smarty-right_delimiter= '}>';
  //$smarty->debugging = true;
  $title = "這是smarty的測試網頁";
  $smarty->assign('title', $title);
  $smarty->assign('body', "這是body顯示的變數");
  $smarty->display('smarty.htm');
?>

unlink()

unlink()
將伺服器的某個檔案刪除
使用方法
unlink(伺服器路徑);

del.php
<?php
  $DestDIR = "c:/www";
  $ServerFilename = "test.txt";
  $fh = fopen($DestDIR . "/" . $ServerFilename, 'a'); //注一
  fwrite($fh, 'Hello world!'); //注一
  unlink($DestDIR . "/" . $ServerFilename);
?>

*注一 : 為C語言的語法,在PHP裡可以用。

copy()

將伺服器的檔案複製到目的地
通常使用於檔案上傳
使用方法
copy(來源檔案,目標檔案);

範例:
upload.htm
<html>
  <head>
    <title>上傳範例</title>
  </head>
  <body>
    <form enctype="multipart/form-data" action="upload.php" method="post">
    檔案路徑:<input type="file" name="file_path"><br>
    <input type="submit" name="Submit" value="上傳">
  </body>
</html>


upload.php
<?php
  $DestDIR = "C:/www";
  $ServerFilename = "data";
  if (!empty($_FILES['file_path']['name'])) {
    if ($_FILES['file_path']['size'] > 1024*1024*2) {
      if ($_FILES['file_path']['type'] == "image/jpeg") {
        check = copy($_FILES['file_path']['tmp_name'], $DestDIR . "/" . $ServerFilename . ".jpg");
        if(check)
          echo "以上傳完成";
        else
          echo "上傳失敗";
      }
      else
        echo "檔案類型不對";
    }
    else
      echo "上傳檔案太大";
  }
  else
  {
    echo "沒有檔案";
    echo "錯誤資訊:" . $_FILES['file_path']['error'];
  }
?>

$_FILES

在form裡需加入
enctype="multipart/form-data"
才會上傳
上傳後使用$_FILES才可以讀取
使用方法
$_FILES[名稱][參數名稱]

$_FILES['file_path']['name'] >> 上傳檔案的名稱
$_FILES['file_path']['tmp_name'] >> 上傳後在伺服器的路徑和名稱
$_FILES['file_path']['size'] >> 上傳檔案的大小
$_FILES['file_path']['type'] >> 上傳檔案的類型
$_FILES['file_path']['error'] >> 錯誤訊息

範例:
upload.htm
<html>
  <head>
    <title>上傳範例</title>
  </head>
  <body>
    <form enctype="multipart/form-data" action="upload.php" method="post">
    檔案路徑:<input type="file" name="file_path"><br>
    <input type="submit" name="Submit" value="上傳">
  </body>
</html>

upload.php
<?php
  if(!empty($_FILES['file_path']['name'])) { //有上傳檔案
    if ($_FILES['file_path']['size'] > 1024*1024*2) { //最大2MB 
      echo "以上傳完成";
      echo "在伺服器裡的位置和名子:" . $_FILES['file_path']['tmp_name'];
      echo "檔案名子:" . $_FILES['file_path']['name'];
      echo "檔案大小:" . $_FILES['file_path']['size'] . "btyes";
      echo "檔案類型:" . $_FILES['file_path']['type'];
    }
    else
      echo "上傳檔案太大";
  } else {
    echo "沒有檔案";
    echo "錯誤資訊:" . $_FILES['file_path']['error'];
  }
?>