PHP缩略图

经常的时候我们会需要用到访问图片,这些需求一般是存在于用户上传图片之后,需要列表出所有他上传的图片。

这时候传回所有的原始大小图片固然没有问题,但是在网络比较差的情况下还是建议来读读本文件吧。

我们知道很多情况上传的档案就只是存在Server的本地的,这种情况下转换缩略图就有很多办法,比如可以使用命令调用本地的图片浏览工具的API,或者其他网络上提供的方法,这些方法要么就是支持的图片格式非常有限,要么就是header有问题等等。

并且但是现在我们的图片或者附件数据是存在数据库中的。由一个固定字符串字段存下来的。

我在网上找到一个很好使用的PHP缩图第三方类: imageclass.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; }

  }

 

  function resize_image( $image_path )

  {

    if( $this->hasGD === false ){ return false; }

    //no GD installed on the server!

 

    list($img_width, $img_height, $img_type, $img_attr) = @getimagesize( $image_path );

    //this is going to get the image width, height, and format

 

    if( ( $img_width != 0 ) || ( $img_width != 0 ) )

    //make sure it was loaded correctly

    {

      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; }

      //it wasn't able to load the image

    }

    else{ return false; }

    //something happened!

 

    if( $this->keep_proportions === true )

    {

      if( ($img_width-$this->sizelimit_x) > ($img_height-$this->sizelimit_y) )

      {

      //if the width of the img is greater than the size limit we scale by width

        $scalex = ( $this->sizelimit_x / $img_width );

        $scaley = $scalex;

      }

      else

      //if the height of the img is greater than the size limit we scale by height

      {

        $scalex = ( $this->sizelimit_y / $img_height );

        $scaley = $scalex;

      }

 

    }

    else

    {

      $scalex = ( $this->sizelimit_x / $img_width );

      $scaley = ( $this->sizelimit_y / $img_height );

      //just make the image fit the image size limit

 

      if( $scalex > 1 ){ $scalex = 1; }

      if( $scaley > 1 ){ $scaley = 1; }

      //don't make it so it streches the image

    }

 

    $new_width = $img_width * $scalex;

    $new_height = $img_height * $scaley;

 

    $this->resized_resource = @imagecreatetruecolor( $new_width, $new_height );

    //creates an image resource, with the width and height of the size limits (or new resized proportion )

 

    if( function_exists( 'imageantialias' )){ @imageantialias( $this->resized_resource, true ); }

    //helps in the quality of the image being resized

 

    @imagecopyresampled( $this->resized_resource, $this->image_resource, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height );

    //resize the iamge onto the resized resource

 

    @imagedestroy( $this->image_resource );

    //destory old 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 );

  }

 

}

 

?>

这一个类支持的格式几乎涵盖了网络上主流的所有格式,GIF、PNG、JPG

调用这个类的方法是:

<?php

    include("conn.php");

    include("imageclass.php");

 

    $result = odbc_exec($conn,"select * from ImageTable where id = ".htmlspecialchars($_REQUEST["id"]));

 

    while (odbc_fetch_row($result))

    {

        $Upload_File=odbc_result($result,"Upload_File");

        $File_Ext = odbc_result($result,"File_Ext");

    }

 

    if($_REQUEST["min"]==null)

    {

    $File_Ext=strtolower($File_Ext);

    switch ($File_Ext) {

      case ".pdf": $ctype="application/pdf"; break;

      case ".exe": $ctype="application/octet-stream"; break;

      case ".zip": $ctype="application/zip"; break;

      case ".doc": $ctype="application/msword"; break;

      case ".xls": $ctype="application/vnd.ms-excel"; break;

      case ".ppt": $ctype="application/vnd.ms-powerpoint"; break;

      case ".gif": $ctype="image/gif"; break;

      case ".png": $ctype="image/png"; break;

      case ".jpeg":

      case ".jpg": $ctype="image/jpeg"; break;

      default: $ctype="application/force-download";

    }

    $ContentType = sprintf("Content-Type:%s",$ctype);

    header($ContentType);

    echo $Upload_File;

    }

 

 

else if($_REQUEST["min"]!=null)

{

    $fp = fopen("c:\\image.tempimage","w-");

    fwrite($fp,$Upload_File);

    fclose($fp);

 

$imgresize = new resize_img();

$imgresize->sizelimit_x = 200;

$imgresize->sizelimit_y = 200;

$imgresize->keep_proportions = true;

$imgresize->output = 'jpg';

if( $imgresize->resize_image("c:\\image.tempimage") === false)

{

  echo 'ERROR!';

}

else

{

  $imgresize->output_resizedimage();

}

 

$imgresize->destroy_resizedimage();

}

odbc_close($conn);

return true;

 

?>

最后一定要记得如果上传了比较大的图片的话,一定要在最开始的地方加入这一句,ini_set("memory_limit", "180M");

因为PHP解释器默认能够给予的memory非常小,这里设置成180M,但是实测大于10M的图片貌似还可以,再大一点的就要报错了。

 

posted @ 2013-04-25 11:29  重庆Debug  阅读(226)  评论(0编辑  收藏  举报