现在的位置: 首页 > 综合 > 正文

php 中图像压缩处理类(二)imageutil.php

2013年09月24日 ⁄ 综合 ⁄ 共 1792字 ⁄ 字号 评论关闭

<?php

/*
 * @(#)imageutil.php
 *
 * Copyright 2007 SoftRoad. All Rights Reserved.
 *
 *====================================================================
 * 変更履歴
 *
 * 新規作成                            2007年5月11日   蒋 彪
 */

require ("bmp.php"); //http://blog.csdn.net/joliny/archive/2007/06/20/1659441.aspx

/**
 * イメージ操作共通クラス
 *
 * @author 蒋 彪
 */
class ImageUtil {

 /*
  * イメージ拡大・縮小
  * $uploadfile 絶対パス(元)
  * $thumbfile 絶対パス(先)
  * $width 新しいサイズ・幅
  * $height 新しいサイズ・高さ
  */
 function resize($uploadfile, $thumbfile, $width, $height) {

  $size = GetImageSize($uploadfile);
  if ($size[2] == 1)
   $im_in = imagecreatefromgif($uploadfile);
  if ($size[2] == 2)
   $im_in = imagecreatefromjpeg($uploadfile);
  if ($size[2] == 3)
   $im_in = imagecreatefrompng($uploadfile);
  if ($size[2] == 6) {
   $im_in = imagecreatefrombmp($uploadfile);
  }
  if ($width == "" || $width == 0) {
   $width = $size[0];
  }
  if ($height == "" || $height == 0) {
   $height = $size[1];
  }
  $im_out = ImageCreateTrueColor($width, $height);
  /*ImageCopyResized*/
  imagecopyresampled($im_out, $im_in, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
  Imagejpeg($im_out, $thumbfile);
  chmod($thumbfile, 0777);
  ImageDestroy($im_in);
  ImageDestroy($im_out);
 }

 function imgResize($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
  ImagePaletteCopy($dst_img, $src_img);
  $rX = $src_w / $dst_w;
  $rY = $src_h / $dst_h;
  $nY = 0;
  for ($y = $src_y; $y < $dst_h; $y++) {
   $oY = $nY;
   $nY = intval(($y +1) * $rY +.5);
   $nX = 0;
   for ($x = $src_x; $x < $dst_w; $x++) {
    $r = $g = $b = $a = 0;
    $oX = $nX;
    $nX = intval(($x +1) * $rX +.5);
    for ($i = $nY; -- $i >= $oY;) {
     for ($j = $nX; -- $j >= $oX;) {
      $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $j, $i));
      $r += $c['red'];
      $g += $c['green'];
      $b += $c['blue'];
      $a++;
     }
    }
    ImageSetPixel($dst_img, ($x + $dst_x - $src_x), ($y + $dst_y - $src_y), ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a));
   }
  }
 }

}
?>
 

抱歉!评论已关闭.