php 原生图片压缩方法

 `1.  //-----------ImageCheck.class.php------------------
2.  getcreatemethod($srcimage);
34.  if ( $method ) {
35.  eval( '$srcimg ='.$method.';');
36.  }
37.  $dst_x = 0;
38.  $dst_y = 0;
39.  $dst_w = $dst_width;
40.  $dst_h = $dst_height;
41.  if ( ($dst_width / $dst_height - $src_width / $src_height) > 0 ) {
42.  $dst_w = $src_width * ( $dst_height / $src_height );
43.  $dst_x = ( $dst_width - $dst_w ) / 2;
44.  } elseif ( ($dst_width / $dst_height - $src_width / $src_height) < 0 ) {
45.  $dst_h = $src_height * ( $dst_width / $src_width );
46.  $dst_y = ( $dst_height - $dst_h ) / 2;
47.  }
48.  imagecopyresampled($dstimg,$srcimg,$dst_x,$dst_y,0,0,$dst_w,$dst_h,$src_width,$src_height);
49.  // 保存格式
50.  $arr = array(
51.  'jpg' => 'imagejpeg', 
52.  'jpeg' => 'imagejpeg', 
53.  'png' => 'imagepng', 
54.  'gif' => 'imagegif', 
55.  'bmp' => 'imagebmp'
56.  );
57.  $suffix = strtolower(array_pop(explode('.', $dstimage ) ) );
58.  if (!in_array($suffix,array_keys($arr))) {
59.  echo "保存的文件名错误";
60.  exit;
61.  } else {
62.  eval( $arr[$suffix] . '($dstimg, "'.$dstimage.'");' );
63.  }
64.  imagejpeg($dstimg,$dstimage);
65.  imagedestroy($dstimg);
66.  imagedestroy($srcimg);
67.  }
68.  //*************************************************************
69.  public function getcreatemethod($file){
70.  $arr = array(
71.  '474946' => "imagecreatefromgif('$file')",
72.  'FFD8FF' => "imagecreatefromjpeg('$file')",
73.  '424D' => "imagecreatefrombmp('$file')",
74.  '89504E' => "imagecreatefrompng('$file')"
75.  );
76.  $fd = fopen( $file, "rb" );
77.  $data = fread( $fd, 3 );
78.  $data =$this->str2hex( $data );
79.  if( array_key_exists( $data, $arr ) ) {
80.  return $arr[$data];
81.  }else if( array_key_exists( substr($data, 0, 4), $arr ) ) {
82.  return $arr[substr($data, 0, 4)];
83.  }else{
84.  return false;
85.  }
86.  }
87.  //**********************************************************
88.  public function str2hex( $str ) {
89.  $ret = "";
90.  for( $i = 0; $i < strlen( $str ) ; $i++ ) {
91.  $ret .= ord($str[$i]) >= 16 ? strval( dechex( ord($str[$i]) ) ):'0'.strval( dechex( ord($str[$i]) ) );
92.  }
93.  return strtoupper( $ret );
94.  } 
95.  //***********************************************************
96.  public function imagecreatefrombmp($filename){
97.  if (! $f1 = fopen($filename,"rb")) return FALSE;
98.  $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
99.  if ($FILE['file_type'] != 19778) return FALSE;
100.  $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.'/Vcompression/Vsize_bitmap/Vhoriz_resolution'.'/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
101.  $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
102.  if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
103.  $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
104.  $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
105.  $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
106.  $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
107.  $BMP['decal'] = 4-(4*$BMP['decal']);
108.  if ($BMP['decal'] == 4) $BMP['decal'] = 0;
109.  $PALETTE = array();
110.  if ($BMP['colors'] < 16777216)
111.  {
112.  $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
113.  }
114.  $IMG = fread($f1,$BMP['size_bitmap']);
115.  $VIDE = chr(0);
116.  $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
117.  $P = 0;
118.  $Y = $BMP['height']-1;
119.  while ($Y >= 0)
120.  {
121.  $X=0;
122.  while ($X < $BMP['width'])
123.  {
124.  if ($BMP['bits_per_pixel'] == 24)
125.  $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
126.  else if ($BMP['bits_per_pixel'] == 16){
127.  $COLOR = unpack("n",substr($IMG,$P,2));
128.  $COLOR[1] = $PALETTE[$COLOR[1]+1];
129.  }else if ($BMP['bits_per_pixel'] == 8){
130.  $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
131.  $COLOR[1] = $PALETTE[$COLOR[1]+1];
132.  }else if ($BMP['bits_per_pixel'] == 4){
133.  $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
134.  if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
135.  $COLOR[1] = $PALETTE[$COLOR[1]+1];
136.  }else    if ($BMP['bits_per_pixel'] == 1){
137.  $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
138.  if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
139.  elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
140.  elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
141.  elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
142.  elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
143.  elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
144.  elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
145.  elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
146.  $COLOR[1] = $PALETTE[$COLOR[1]+1];
147.  }else
148.  return FALSE;
149.  imagesetpixel($res,$X,$Y,$COLOR[1]);
150.  $X++;
151.  $P += $BMP['bytes_per_pixel'];
152.  }
153.  $Y--;
154.  $P+=$BMP['decal'];
155.  }
156.  fclose($f1);
157.  return $res;
158.  } 
159.  //*********************************BMP保存函数*************************************************
160.  public function imagebmp ($im, $fn = false){
161.  if (!$im) return false;
162.  if ($fn === false) $fn = 'php://output';
163.  $f = fopen ($fn, "w");
164.  if (!$f) return false;
165.  $biWidth = imagesx ($im);
166.  $biHeight = imagesy ($im);
167.  $biBPLine = $biWidth * 3;
168.  $biStride = ($biBPLine + 3) & ~3;
169.  $biSizeImage = $biStride * $biHeight;
170.  $bfOffBits = 54;
171.  $bfSize = $bfOffBits + $biSizeImage;
172.  fwrite ($f, 'BM', 2);
173.  fwrite ($f, pack ('VvvV', $bfSize, 0, 0, $bfOffBits));
174.  fwrite ($f, pack ('VVVvvVVVVVV',40,$biWidth, $biHeight,1,24,0, $biSizeImage,0,0,0,0));
175.  $numpad = $biStride - $biBPLine;
176.  for ($y = $biHeight - 1; $y >= 0; --$y)
177.  {
178.  for ($x = 0; $x < $biWidth; ++$x)
179.  {
180.  $col = imagecolorat ($im, $x, $y);
181.  fwrite ($f, pack ('V', $col), 3);
182.  }
183.  for ($i = 0; $i < $numpad; ++$i)
184.  {
185.  fwrite ($f, pack ('C', 0));
186.  }
187.  fclose ($f);
188.  return true;
189.  } 
190.  }    
191.  }
192.  ?> 
193.  //----------调用-------------------------
194.  $image=new ImageCheck();
195.  $image->imagezoom("./Common/upload/news/20130712/liu.jpg","./Common/upload/news/20130712/liu.jpg",100,100,"#FFFFFF");
196.  同名将覆盖

复制代码

` 

 **【推广】** [【选型推荐】0基础1小时建站,](https://e.topthink.com/api/go/ec34217f6d305d7d1)

你可能感兴趣的:(php 原生图片压缩方法)