PHP生成自适应大小的缩略图类及使用方法分享

把下面的代码直接复制,新建一个文件叫做 thumbnailimage.php  ,文件名最好不要用大写,把以下代码复制进去:

复制代码 代码如下:


<?php

define ( 'MAX_IMG_SIZE', 100000 );

// Supported image types
define ( 'THUMB_JPEG', 'image/jpeg' );
define ( 'THUMB_PNG', 'image/png' );
define ( 'THUMB_GIF', 'image/gif' );

// Interlacing modes
define ( 'INTERLACE_OFF', 0 );
define ( 'INTERLACE_ON', 1 );

// Output modes
define ( 'STDOUT', '' );

// Empty constants
define ( 'NO_LOGO', '' );
define ( 'NO_LABEL', '' );

// Logo and label positioning
define ( 'POS_LEFT', 0 );
define ( 'POS_RIGHT', 1 );
define ( 'POS_CENTER', 2 );
define ( 'POS_TOP', 3 );
define ( 'POS_BOTTOM', 4 );

// Error messages
define ( 'E_001', 'File <b>%s</b> do not exist' );
define ( 'E_002', 'Failed reading image data from <b>%s</b>' );
define ( 'E_003', 'Cannot create the copy of <b>%s</b>' );
define ( 'E_004', 'Cannot copy the logo image' );
define ( 'E_005', 'Cannot create final image' );

// ****************************************************************************
// CLASS DEFINITION
// ****************************************************************************

class ThumbnailImage
{

// ****************************************************************************
// PUBLIC PROPERTIES
// ****************************************************************************

var $src_file;  // source image file
  var $dest_file; // destination image file
  var $dest_type;  // destination image type

var $interlace; // destination image interlacing mode
  var $jpeg_quality; // quality of resulting JPEG

var $max_width; // maximal thumbnail width
  var $max_height; // maximal thumbnail height
  var $fit_to_max; // enlarge small images?

var $logo; // array of logo parameters
  var $label; // array of label parameters

// ****************************************************************************
// CLASS CONSTRUCTOR
// ****************************************************************************

/*
    Description:
      Defines default values for properties.
    Prototype:
      void ThumbImg ( string src_file = '' )
    Parameters:
      src_file - source image filename
  */
  function ThumbnailImage ( $src_file = '' )
  {

$this->src_file = $src_file;
    $this->dest_file = STDOUT;
    $this->dest_type = THUMB_JPEG;

$this->interlace = INTERLACE_OFF;
    $this->jpeg_quality = -1;

$this->max_width = 100;
    $this->max_height = 90;
    $this->fit_to_max = FALSE;

$this->logo['file'] = NO_LOGO;
    $this->logo['vert_pos'] = POS_TOP;
    $this->logo['horz_pos'] = POS_LEFT;

$this->label['text'] = NO_LABEL;
    $this->label['vert_pos'] = POS_BOTTOM;
    $this->label['horz_pos'] = POS_RIGHT;
    $this->label['font'] = '';
    $this->label['size'] = 20;
    $this->label['color'] = '#000000';
    $this->label['angle'] = 0;

}

// ****************************************************************************
// PRIVATE METHODS
// ****************************************************************************

/*
    Description:
      Extracts decimal color components from hex color string.
    Prototype:
      array ParseColor ( string hex_color )
    Parameters:
      hex_color - color in '#rrggbb' format
    Return:
      Decimal values for red, green and blue color components.
  */
  function ParseColor ( $hex_color )
  {

if ( strpos ( $hex_color, '#' ) === 0 )
      $hex_color = substr ( $hex_color, 1 );

$r = hexdec ( substr ( $hex_color, 0, 2 ) );
    $g = hexdec ( substr ( $hex_color, 2, 2 ) );
    $b = hexdec ( substr ( $hex_color, 4, 2 ) );

return array ( $r, $g, $b );

}

/*
    Description:
      Retrives image data as a string.
      Thanks to Luis Larrateguy for the idea of this function.
    Prototype:
      string GetImageStr ( string image_file )
    Parameters:
      image_file - filename of image
    Return:
      Image file contents string.
  */
  function GetImageStr ( $image_file )
  {

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/97e300c08f208424a80bdc23f1abcf40.html