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();


*/

沒有留言:

張貼留言