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

if ( $horz_pos == POS_CENTER )
      $x_pos = ceil ( $thumb_width / 2 - $logo_width / 2 );
    elseif ( $horz_pos == POS_RIGHT )
      $x_pos = $thumb_width - $logo_width;
    else
      $x_pos = 0;

if ( ! imagecopy ( $thumb_img, $logo_image, $x_pos, $y_pos, 0, 0,
      $logo_width, $logo_height ) )
      trigger_error( E_004, E_USER_ERROR );

}

/*
    Description:
      Adds label text to thumbnail.
    Prototype:
      void AddLabel ( int thumb_width, int thumb_height, resource &thumb_img )
    Parameters:
      thumb_width - width of thumbnail image
      thumb_height - height of thumbnail image
      thumb_img - thumbnail image identifier
  */
  function AddLabel ( $thumb_width, $thumb_height, &$thumb_img )
  {

extract ( $this->label );

list( $r, $g, $b ) = $this->ParseColor ( $color );
    $color_id = imagecolorallocate ( $thumb_img, $r, $g, $b );

$text_box = imagettfbbox ( $size, $angle, $font, $text );
    $text_width = $text_box [ 2 ] - $text_box [ 0 ];
    $text_height = abs ( $text_box [ 1 ] - $text_box [ 7 ] );

if ( $vert_pos == POS_TOP )
      $y_pos = 5 + $text_height;
    elseif ( $vert_pos == POS_CENTER )
      $y_pos = ceil( $thumb_height / 2 - $text_height / 2 );
    elseif ( $vert_pos == POS_BOTTOM )
      $y_pos = $thumb_height - $text_height;

if ( $horz_pos == POS_LEFT )
      $x_pos = 5;
    elseif ( $horz_pos == POS_CENTER )
      $x_pos = ceil( $thumb_width / 2 - $text_width / 2 );
    elseif ( $horz_pos == POS_RIGHT )
      $x_pos = $thumb_width - $text_width -5;

imagettftext ( $thumb_img, $size, $angle, $x_pos, $y_pos,
      $color_id, $font, $text );

}

/*
    Description:
      Output thumbnail image into the browser.
    Prototype:
      void OutputThumbImage ( resource dest_image )
    Parameters:
      dest_img - thumbnail image identifier
  */
  function OutputThumbImage ( $dest_image )
  {

imageinterlace ( $dest_image, $this->interlace );

header ( 'Content-type: ' . $this->dest_type );

if ( $this->dest_type == THUMB_JPEG )
      imagejpeg ( $dest_image, '', $this->jpeg_quality );
    elseif ( $this->dest_type == THUMB_GIF )
      imagegif($dest_image);
    elseif ( $this->dest_type == THUMB_PNG )
      imagepng ( $dest_image );

}

/*
    Description:
      Save thumbnail image into the disc file.
    Prototype:
      void SaveThumbImage ( string image_file, resource dest_image )
    Parameters:
      image_file - destination file name
      dest_img - thumbnail image identifier
  */
  function SaveThumbImage ( $image_file, $dest_image )
  {

imageinterlace ( $dest_image, $this->interlace );

if ( $this->dest_type == THUMB_JPEG )
      imagejpeg ( $dest_image, $this->dest_file, $this->jpeg_quality );
    elseif ( $this->dest_type == THUMB_GIF )
      imagegif ( $dest_image, $this->dest_file );
    elseif ( $this->dest_type == THUMB_PNG )
      imagepng ( $dest_image, $this->dest_file );

}

// ****************************************************************************
// PUBLIC METHODS
// ****************************************************************************

/*
    Description:
      Output thumbnail image into the browser or disc file according to the
      values of parameters.
    Prototype:
      void Output ( )
  */
  function Output()
  {

$src_image = $this->LoadImage($this->src_file, $src_width, $src_height);

$dest_size = $this->GetThumbSize($src_width, $src_height);

$dest_width=$dest_size[0];
    $dest_height=$dest_size[1];

$dest_image=imagecreatetruecolor($dest_width, $dest_height);
    if (!$dest_image)
      trigger_error(E_005, E_USER_ERROR);

imagecopyresampled( $dest_image, $src_image, 0, 0, 0, 0,
      $dest_width, $dest_height, $src_width, $src_height );

if ($this->logo['file'] != NO_LOGO)
      $this->AddLogo($dest_width, $dest_height, $dest_image);

if ($this->label['text'] != NO_LABEL)
      $this->AddLabel($dest_width, $dest_height, $dest_image);

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

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