PHP缩略图等比例无损压缩,可填充空白区域补充色
程序员文章站
2022-06-08 22:18:58
复制代码 代码如下:
<?php
error_reporting( e_all );
// 测试
imagezoom('1.jpg', '2.jpg', 400, 300, '#ffffff');
/*
php缩略图函数:
等比例无损压缩,可填充补充色 author: 华仔
主持格式:
bmp 、jpg 、gif、png
param:
@srcimage : 要缩小的图片
@dstimage : 要保存的图片
@dst_width: 缩小宽
@dst_height: 缩小高
@backgroundcolor: 补充色 如:#ffffff 支持 6位 不支持3位
*/
function imagezoom( $srcimage, $dstimage, $dst_width, $dst_height, $backgroundcolor ) {
// 中文件名乱码
if ( php_os == 'winnt' ) {
$srcimage = iconv('utf-8', 'gbk', $srcimage);
$dstimage = iconv('utf-8', 'gbk', $dstimage);
}
$dstimg = imagecreatetruecolor( $dst_width, $dst_height );
$color = imagecolorallocate($dstimg
, hexdec(substr($backgroundcolor, 1, 2))
, hexdec(substr($backgroundcolor, 3, 2))
, hexdec(substr($backgroundcolor, 5, 2))
);
imagefill($dstimg, 0, 0, $color);
if ( !$arr=getimagesize($srcimage) ) {
echo "要生成缩略图的文件不存在";
exit;
}
$src_width = $arr[0];
$src_height = $arr[1];
$srcimg = null;
$method = getcreatemethod( $srcimage );
if ( $method ) {
eval( '$srcimg = ' . $method . ';' );
}
$dst_x = 0;
$dst_y = 0;
$dst_w = $dst_width;
$dst_h = $dst_height;
if ( ($dst_width / $dst_height - $src_width / $src_height) > 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) < 0 ) {
$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 < strlen( $str ) ; $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'] < 16777216)
{
$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 < $bmp['width'])
{
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 < $biwidth; ++$x)
{
$col = imagecolorat ($im, $x, $y);
fwrite ($f, pack ('v', $col), 3);
}
for ($i = 0; $i < $numpad; ++$i)
fwrite ($f, pack ('c', 0));
}
fclose ($f);
return true;
}
?>
复制代码 代码如下:
<?php
error_reporting( e_all );
// 测试
imagezoom('1.jpg', '2.jpg', 400, 300, '#ffffff');
/*
php缩略图函数:
等比例无损压缩,可填充补充色 author: 华仔
主持格式:
bmp 、jpg 、gif、png
param:
@srcimage : 要缩小的图片
@dstimage : 要保存的图片
@dst_width: 缩小宽
@dst_height: 缩小高
@backgroundcolor: 补充色 如:#ffffff 支持 6位 不支持3位
*/
function imagezoom( $srcimage, $dstimage, $dst_width, $dst_height, $backgroundcolor ) {
// 中文件名乱码
if ( php_os == 'winnt' ) {
$srcimage = iconv('utf-8', 'gbk', $srcimage);
$dstimage = iconv('utf-8', 'gbk', $dstimage);
}
$dstimg = imagecreatetruecolor( $dst_width, $dst_height );
$color = imagecolorallocate($dstimg
, hexdec(substr($backgroundcolor, 1, 2))
, hexdec(substr($backgroundcolor, 3, 2))
, hexdec(substr($backgroundcolor, 5, 2))
);
imagefill($dstimg, 0, 0, $color);
if ( !$arr=getimagesize($srcimage) ) {
echo "要生成缩略图的文件不存在";
exit;
}
$src_width = $arr[0];
$src_height = $arr[1];
$srcimg = null;
$method = getcreatemethod( $srcimage );
if ( $method ) {
eval( '$srcimg = ' . $method . ';' );
}
$dst_x = 0;
$dst_y = 0;
$dst_w = $dst_width;
$dst_h = $dst_height;
if ( ($dst_width / $dst_height - $src_width / $src_height) > 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) < 0 ) {
$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 < strlen( $str ) ; $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'] < 16777216)
{
$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 < $bmp['width'])
{
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 < $biwidth; ++$x)
{
$col = imagecolorat ($im, $x, $y);
fwrite ($f, pack ('v', $col), 3);
}
for ($i = 0; $i < $numpad; ++$i)
fwrite ($f, pack ('c', 0));
}
fclose ($f);
return true;
}
?>
上一篇: JSONP跨域请求