2011年8月4日 星期四

[PHP]取得檔案的副檔名[轉載]


//假設檔名
$filename = 'mypic.gif';
// 1. 使用陣列函式 "explode/end" 方式
$ext = end(explode('.', $filename));
// 2. 使用字串函式 "strrchr" 方式
$ext = substr(strrchr($filename, '.'), 1);
// 3. 使用字串函式 "strrpos" 方式
$ext = substr($filename, strrpos($filename, '.') + 1);
// 4. 使用正規式 "preg_replace" 方式
$ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
// 5. 特別(從來沒用過)方式
//   參考: http://php.about.com/od/finishedphp1/qt/file_ext_PHP.htm
$exts = split("[/\\.]", $filename);
$n = count($exts)-1;
$ext = $exts[$n];









pathinfo(path,options)
PATHINFO_DIRNAME:取得資料夾路徑 
PATHINFO_BASENAME:取得整個檔名(主+副) 
PATHINFO_EXTENSION:取得副檔名 
PATHINFO_FILENAME :取得主檔名
pathinfo() 返回一個關聯數組包含有 path 的信息

包括以下的數組元素:
[dirname] 
[basename] 
[extension]
[filename]

2011年8月2日 星期二

[PHP]用來-轉檔-縮圖-輸出

<?php
class resize_img
{
  var $image_path = '';
  //holds the image path
  var $sizelimit_x = 250;
  //the limit of the image width
  var $sizelimit_y = 250;
  //the limit of the image height
  var $image_resource = '';
  //holds the image resource
  var $keep_proportions = true;
  //if true it keeps the image proportions when resized
  var $resized_resource = '';
  //holds the resized image resource
  var $hasGD = false;
  var $output = 'SAME';
  //can be JPG, GIF, PNG, or SAME (same will save as old type)

  function resize_img()
  {
    if( function_exists('gd_info') ){ $this->hasGD = true; }  //---判斷GD函式是否支援
  }

  function resize_image( $image_path )
  {
    if( $this->hasGD === false ){ return false; }
    //沒有GD支援

    list($img_width, $img_height, $img_type, $img_attr) = @getimagesize( $image_path );
    //這是會得到的圖像的寬度,高度和格式

    if( ( $img_width != 0 ) || ( $img_width != 0 ) )
    //確保它是正確裝入
    {
      switch( $img_type )
      {
        case 1:
          //GIF
          $this->image_resource = @imagecreatefromgif( $image_path );
          if( $this->output == 'SAME' ){ $this->output = 'GIF'; }
          break;
        case 2:
          //JPG
          $this->image_resource = @imagecreatefromjpeg( $image_path );
          if( $this->output == 'SAME' ){ $this->output = 'JPG'; }
          break;
        case 3:
          //PNG
          $this->image_resource = @imagecreatefrompng( $image_path );
          if( $this->output == 'SAME' ){ $this->output = 'PNG'; }
      }
      if( $this->image_resource === '' ){ return false; }
      //它無法加載圖像
    }
    else{ return false; }
    //發生無法預期的錯誤

    if( $this->keep_proportions === true )
    {
      if( ($img_width-$this->sizelimit_x) > ($img_height-$this->sizelimit_y) )
      {
      //如果IMG寬度 大於規定的大小限制,我們的寬度將做縮減    
        $scalex = ( $this->sizelimit_x / $img_width );
        $scaley = $scalex;
      }
      else
      {
      //如果IMG高度 大於規定的大小限制,我們的高度將做縮減
        $scalex = ( $this->sizelimit_y / $img_height );
        $scaley = $scalex;
      }

    }
    else
    {
      $scalex = ( $this->sizelimit_x / $img_width );
      $scaley = ( $this->sizelimit_y / $img_height );
      //只是使圖像適合圖像的大小限制

      if( $scalex > 1 ){ $scalex = 1; }
      if( $scaley > 1 ){ $scaley = 1; }
      //圖像小於限制所以不做變更
    }

    $new_width = $img_width * $scalex;//計算新的寬
    $new_height = $img_height * $scaley;//計算新的高

    $this->resized_resource = @imagecreatetruecolor( $new_width, $new_height );
   //創建一個圖像資源,用寬度和高度的大小限制(或新的調整比例)

    if( function_exists( 'imageantialias' )){ @imageantialias( $this->resized_resource, true ); }
    //有助於在質量的圖像被調整判斷是否有此函式

    @imagecopyresampled( $this->resized_resource, $this->image_resource, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height );
    //調整張圖片上調整資源

    @imagedestroy( $this->image_resource );
    //清空舊的圖像資源

    return true;
  }
 //判斷輸出為什麼格式
  function save_resizedimage( $path, $name )
  {
    switch( strtoupper($this->output) )
    {
      case 'GIF':
        //GIF
        @imagegif( $this->resized_resource, $path . $name . '.gif' );
        break;
      case 'JPG':
        //JPG
        @imagejpeg( $this->resized_resource, $path . $name . '.jpg' );
        break;
      case 'PNG':
        //PNG
        @imagepng( $this->resized_resource, $path . $name . '.png' );
    }
  }
 //--此函式用來在頁面顯示圖片
  function output_resizedimage()
  {
    $the_time = time();
    header('Last-Modified: ' . date( 'D, d M Y H:i:s', $the_time ) . ' GMT');
    header('Cache-Control: public');

    switch( strtoupper($this->output) )
    {
      case 'GIF':
        //GIF
        header('Content-type: image/gif');
        @imagegif( $this->resized_resource );
        break;
      case 'JPG':
        //JPG
        header('Content-type: image/jpg');
        @imagejpeg( $this->resized_resource );
        break;
      case 'PNG':
        //PNG
        header('Content-type: image/png');
        @imagepng( $this->resized_resource );
    }
  }

  function destroy_resizedimage()
  {
    @imagedestroy( $this->resized_resource );
    @imagedestroy( $this->image_resource );
  }

}

?>












/*範例-----可以用來-轉檔-縮圖-輸出

$imgresize = new resize_img();

$imgresize->sizelimit_x = 100;
$imgresize->sizelimit_y = 100;
$imgresize->keep_proportions = true;
$imgresize->output = 'GIF';

 $filename = uniqid().rand(1000,9999);


if( $imgresize->resize_image( $_FILES['Filedata']['tmp_name'] ) === false )
{
  echo 'ERROR!';
}
else
{
  $imgresize->save_resizedimage( 'D:/www/test/thumbnails/', $filename );
}

$imgresize->destroy_resizedimage();


*/

[PHP]頁面連接函式

//一面多少個資料
$results_per_page = 50;
//多少個頁面
$num_pages = ceil(count($friend['data'])/$results_per_page);
//當前頁面
$cur_page = isset($_GET['page']) ? $_GET['page'] : 1;
//定位該頁面的第一筆資料
$skip = (($cur_page - 1 )*$results_per_page);

$friends=array();
//一次只拿50個資料
$friends = array_slice($friend['data'],$skip,$results_per_page);

//------------------------------------------------------------
function generate_page_links($cur_page, $num_pages) {
$page_links = '';
$before = $cur_page - 4;
$after = $cur_page + 4;
($before < 1 ? $before=1:'');
($after > $num_pages ? $after = $num_pages:'');

// If this page is not the first page, generate the "previous" link
if ($cur_page > 1) {
$page_links .= '<a href="' . $_SERVER['PHP_SELF'] .  '?page=' . ($cur_page - 1) . '"><-</a> ';
}
else {
$page_links .= '<- ';
}

// Loop through the pages generating the page number links
for ($before; $before <= $after ;$before++) {
if ($cur_page == $before) {
$page_links .= ' ' . $before;
}
else {
$page_links .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $before . '"> ' . $before . '</a>';
}
}

// If this page is not the last page, generate the "next" link
if ($cur_page < $after) {
$page_links .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($cur_page + 1) . '">-></a>';
}
else {
$page_links .= ' ->';
}

return $page_links;
}