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


Posted in PHP onJune 10, 2011
<?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 相关文章推荐
用PHP编程语言开发动态WAP页面
Oct 09 PHP
PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析
Aug 15 PHP
php线性表顺序存储实现代码(增删查改)
Feb 16 PHP
discuz加密解密函数使用方法和中文注释
Jan 21 PHP
Windows中使用计划任务自动执行PHP程序实例
May 09 PHP
PHP中设置一个严格30分钟过期Session面试题的4种答案
Jul 30 PHP
smarty内置函数{loteral}、{ldelim}和{rdelim}用法实例
Jan 22 PHP
php实现图片上传并利用ImageMagick生成缩略图
Mar 14 PHP
ThinkPHP整合datatables实现服务端分页的示例代码
Feb 10 PHP
PHP实现统计代码行数小工具
Sep 19 PHP
php post换行的方法
Feb 03 PHP
php计数排序算法的实现代码(附四个实例代码)
Mar 31 PHP
PHP网站备份程序代码分享
Jun 10 #PHP
php在页面中调用fckeditor编辑器的方法
Jun 10 #PHP
PHP中遍历stdclass object的实现代码
Jun 09 #PHP
IIS7.X配置PHP运行环境小结
Jun 09 #PHP
php基础学习之变量的使用
Jun 09 #PHP
php学习之数据类型之间的转换介绍
Jun 09 #PHP
php学习之运算符相关概念
Jun 09 #PHP
You might like
PHP三元运算符的结合性介绍
2012/01/10 PHP
PHP笔记之:日期函数的使用介绍
2013/04/24 PHP
php给一组指定关键词添加span标签的方法
2015/03/31 PHP
详解Yii2 定制表单输入字段的标签和样式
2017/01/04 PHP
超棒的javascript页面顶部卷动广告效果
2007/12/01 Javascript
js中cookie的使用详细分析
2008/05/28 Javascript
JQuery从头学起第二讲
2010/07/04 Javascript
Jquery倒数计时按钮setTimeout的实例代码
2013/07/04 Javascript
js和php如何获取当前url的内容
2013/09/22 Javascript
javascript预加载图片、css、js的方法示例介绍
2013/10/14 Javascript
Textarea根据内容自适应高度
2013/10/28 Javascript
JavaScript中的条件判断语句使用详解
2015/06/03 Javascript
jQuery实现图片预加载效果
2015/11/27 Javascript
微信小程序(三):网络请求
2017/01/13 Javascript
Express使用html模板的详细代码
2017/09/18 Javascript
详解基于vue-cli3.0如何构建功能完善的前端架子
2018/10/09 Javascript
Node使用Selenium进行前端自动化操作的代码实现
2019/10/10 Javascript
使用vscode快速建立vue模板过程详解
2019/10/10 Javascript
JS实现滑动拼图验证功能完整示例
2020/03/29 Javascript
[04:22]DSPL第二期精彩集锦:残血反杀!
2014/12/10 DOTA
[00:32]DOTA2上海特级锦标赛 COL战队宣传片
2016/03/04 DOTA
[00:17]天涯墨客一技能展示
2018/08/25 DOTA
python分割和拼接字符串
2013/11/01 Python
python中引用与复制用法实例分析
2015/06/04 Python
浅谈Python黑帽子取代netcat
2018/02/10 Python
python中matplotlib的颜色及线条控制的示例
2018/03/16 Python
实例讲解python中的序列化知识点
2018/10/08 Python
用Python从0开始实现一个中文拼音输入法的思路详解
2019/07/20 Python
python使用PIL和matplotlib获取图片像素点并合并解析
2019/09/10 Python
Python 使用SFTP和FTP实现对服务器的文件下载功能
2020/12/17 Python
html5实现滑块功能之type=&quot;range&quot;属性
2020/02/18 HTML / CSS
如何使用PHP session
2015/04/21 面试题
统计系教授推荐信
2014/02/28 职场文书
月度优秀员工获奖感言
2014/08/16 职场文书
收银员岗位职责范本
2015/04/07 职场文书
MongoDB数据库部署环境准备及使用介绍
2022/03/21 MongoDB