您好,欢迎访问一九零五行业门户网

php 完整图片按比例生成缩略图代码

sizelimit_x = 100;
$imgresize->sizelimit_y = 100;
$imgresize->keep_proportions = true;
$imgresize->output = 'png';
if( $imgresize->resize_image( 'c:/xampp/htdocs/wannabe/image_bin/public/treeshadows_001.gif' ) === false )
{
  echo 'error!';
}
else
{
  $imgresize->save_resizedimage( 'c:/xampp/htdocs/wannabe/image_bin/public/thumbnails/', 'treeshadows_001' );
  $imgresize->output_resizedimage();
}
$imgresize->destroy_resizedimage();
?>
// resize_img.php文件
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 );
  }
}
?>
其它类似信息

推荐信息