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

PHP缩略图等比例无损压缩,可填充空白区域补充色

php缩略图 等比例无损压缩,可填充空白区域补充色的实现代码,需要的朋友可以参考下。
代码如下:
0 ) {
$dst_w = $src_width * ( $dst_height / $src_height );
$dst_x = ( $dst_width - $dst_w ) / 2;
} elseif ( ($dst_width / $dst_height - $src_width / $src_height) $dst_h = $src_height * ( $dst_width / $src_width );
$dst_y = ( $dst_height - $dst_h ) / 2;
}
imagecopyresampled($dstimg, $srcimg, $dst_x
, $dst_y, 0, 0, $dst_w, $dst_h, $src_width, $src_height);
// 保存格式
$arr = array(
'jpg' => 'imagejpeg'
, 'jpeg' => 'imagejpeg'
, 'png' => 'imagepng'
, 'gif' => 'imagegif'
, 'bmp' => 'imagebmp'
);
$suffix = strtolower( array_pop(explode('.', $dstimage ) ) );
if (!in_array($suffix, array_keys($arr)) ) {
echo 保存的文件名错误;
exit;
} else {
eval( $arr[$suffix] . '($dstimg, '.$dstimage.');' );
}
imagejpeg($dstimg, $dstimage);
imagedestroy($dstimg);
imagedestroy($srcimg);
}
function getcreatemethod( $file ) {
$arr = array(
'474946' => imagecreatefromgif('$file')
, 'ffd8ff' => imagecreatefromjpeg('$file')
, '424d' => imagecreatefrombmp('$file')
, '89504e' => imagecreatefrompng('$file')
);
$fd = fopen( $file, rb );
$data = fread( $fd, 3 );
$data = str2hex( $data );
if ( array_key_exists( $data, $arr ) ) {
return $arr[$data];
} elseif ( array_key_exists( substr($data, 0, 4), $arr ) ) {
return $arr[substr($data, 0, 4)];
} else {
return false;
}
}
function str2hex( $str ) {
$ret = ;
for( $i = 0; $i $ret .= ord($str[$i]) >= 16 ? strval( dechex( ord($str[$i]) ) )
: '0'. strval( dechex( ord($str[$i]) ) );
}
return strtoupper( $ret );
}
// bmp 创建函数 php本身无
function imagecreatefrombmp($filename)
{
if (! $f1 = fopen($filename,rb)) return false;
$file = unpack(vfile_type/vfile_size/vreserved/vbitmap_offset, fread($f1,14));
if ($file['file_type'] != 19778) return false;
$bmp = unpack('vheader_size/vwidth/vheight/vplanes/vbits_per_pixel'.
'/vcompression/vsize_bitmap/vhoriz_resolution'.
'/vvert_resolution/vcolors_used/vcolors_important', fread($f1,40));
$bmp['colors'] = pow(2,$bmp['bits_per_pixel']);
if ($bmp['size_bitmap'] == 0) $bmp['size_bitmap'] = $file['file_size'] - $file['bitmap_offset'];
$bmp['bytes_per_pixel'] = $bmp['bits_per_pixel']/8;
$bmp['bytes_per_pixel2'] = ceil($bmp['bytes_per_pixel']);
$bmp['decal'] = ($bmp['width']*$bmp['bytes_per_pixel']/4);
$bmp['decal'] -= floor($bmp['width']*$bmp['bytes_per_pixel']/4);
$bmp['decal'] = 4-(4*$bmp['decal']);
if ($bmp['decal'] == 4) $bmp['decal'] = 0;
$palette = array();
if ($bmp['colors'] {
$palette = unpack('v'.$bmp['colors'], fread($f1,$bmp['colors']*4));
}
$img = fread($f1,$bmp['size_bitmap']);
$vide = chr(0);
$res = imagecreatetruecolor($bmp['width'],$bmp['height']);
$p = 0;
$y = $bmp['height']-1;
while ($y >= 0)
{
$x=0;
while ($x {
if ($bmp['bits_per_pixel'] == 24)
$color = unpack(v,substr($img,$p,3).$vide);
elseif ($bmp['bits_per_pixel'] == 16)
{
$color = unpack(n,substr($img,$p,2));
$color[1] = $palette[$color[1]+1];
}
elseif ($bmp['bits_per_pixel'] == 8)
{
$color = unpack(n,$vide.substr($img,$p,1));
$color[1] = $palette[$color[1]+1];
}
elseif ($bmp['bits_per_pixel'] == 4)
{
$color = unpack(n,$vide.substr($img,floor($p),1));
if (($p*2)%2 == 0) $color[1] = ($color[1] >> 4) ; else $color[1] = ($color[1] & 0x0f);
$color[1] = $palette[$color[1]+1];
}
elseif ($bmp['bits_per_pixel'] == 1)
{
$color = unpack(n,$vide.substr($img,floor($p),1));
if (($p*8)%8 == 0) $color[1] = $color[1] >>7;
elseif (($p*8)%8 == 1) $color[1] = ($color[1] & 0x40)>>6;
elseif (($p*8)%8 == 2) $color[1] = ($color[1] & 0x20)>>5;
elseif (($p*8)%8 == 3) $color[1] = ($color[1] & 0x10)>>4;
elseif (($p*8)%8 == 4) $color[1] = ($color[1] & 0x8)>>3;
elseif (($p*8)%8 == 5) $color[1] = ($color[1] & 0x4)>>2;
elseif (($p*8)%8 == 6) $color[1] = ($color[1] & 0x2)>>1;
elseif (($p*8)%8 == 7) $color[1] = ($color[1] & 0x1);
$color[1] = $palette[$color[1]+1];
}
else
return false;
imagesetpixel($res,$x,$y,$color[1]);
$x++;
$p += $bmp['bytes_per_pixel'];
}
$y--;
$p+=$bmp['decal'];
}
fclose($f1);
return $res;
}
// bmp 保存函数,php本身无
function imagebmp ($im, $fn = false)
{
if (!$im) return false;
if ($fn === false) $fn = 'php://output';
$f = fopen ($fn, w);
if (!$f) return false;
$biwidth = imagesx ($im);
$biheight = imagesy ($im);
$bibpline = $biwidth * 3;
$bistride = ($bibpline + 3) & ~3;
$bisizeimage = $bistride * $biheight;
$bfoffbits = 54;
$bfsize = $bfoffbits + $bisizeimage;
fwrite ($f, 'bm', 2);
fwrite ($f, pack ('vvvv', $bfsize, 0, 0, $bfoffbits));
fwrite ($f, pack ('vvvvvvvvvvv', 40, $biwidth, $biheight, 1, 24, 0, $bisizeimage, 0, 0, 0, 0));
$numpad = $bistride - $bibpline;
for ($y = $biheight - 1; $y >= 0; --$y)
{
for ($x = 0; $x {
$col = imagecolorat ($im, $x, $y);
fwrite ($f, pack ('v', $col), 3);
}
for ($i = 0; $i fwrite ($f, pack ('c', 0));
}
fclose ($f);
return true;
}
?>
其它类似信息

推荐信息