All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
C#控制语句中的switch语句用法
PHP基础:详解Apache中.htaccess文件的功能写法_php资料_编程技术
一条语句查询数据库中所以表的信息_[SQL,Server教程]
定义标题的最好方法_[Html教程]
C#教程:Socket类的属性、方法使用实例
使用ASP.NET,2.0中的ReportViewer控件_.net资料_编程技术
九 表格 Tables_[Html教程]
ASP.Net下两种全局变量的设置和读取方法_[Asp.Net教程]
ASP.NET底层架构探索之HttpHandlers_.net资料_编程技术
开发ASP.NET,Atlas服务器端Extender控件—基本概念以及预先需求_[Asp.Net教程]
Reading number is top 10 pictures
采访谢楠
Beauty ZhiHuiLin2
Hunan province aizhai super-large suspension bridge open to traffic and 4 world first1
9.3阅兵全景图1-抗战老兵和英雄连队梯队
In the world the most mysterious 21 place landscape4
The household of rural style is designed
西方气质的东方美女2
The money of more than 100 countries and regions1
Beautiful vacuum girl3
A resort photographed beautiful young woman change clothes process vomiting blood1
Download software ranking
Rio big adventure
Boxer Classic video1
linux安装大全
Boxer Classic video3
虚拟机汉化软件
Boxer's Top ten classic battle1
Tram sex maniac 2 (H) rar bag13
Adobe Flash Player(IE) 10.0.32.18 浏览器专用的FLASH插件
实战黑客不求人
WebService在.NET中的实战应用教学视频 → 第1集
delv published in(发表于) 2013/12/29 4:46:03 Edit(编辑)
生成bmp格式的图片(imagebmp)_php资料_编程技术

生成bmp格式的图片(imagebmp)_php资料_编程技术

[GD]生成bmp格式的图片(imagebmp)_php资料_编程技术-你的首页-uuhomepage.com

GD库里没有生成bmp图片的函数,所以自己写了一个,这个函数尚有一个压缩算法没有写,不过已经够用了。需要的同学可以看看。

int imagebmp ( resource image [, string filename [, int $bit [, int compression]]] )

$im: 图像资源
$filename: 如果要另存为文件,请指定文件名,为空则直接在浏览器输出
$bit: 图像质量(1、4、8、16、24、32位)
$compression: 压缩方式,0为不压缩,1使用RLE8压缩算法进行压缩

注意:这个函数仍然需要GD库的支持。

Demo:

$im = imagecreatefrompng("test.png");
imagebmp($im);
imagedestroy($im);
Source:

/**
* 创建bmp格式图片
*
* @author: legend(legendsky@hotmail.com)
* @link: http://www.ugia.cn/?p=96
* @description: create Bitmap-File with GD library
* @version: 0.1
*
* @param resource $im 图像资源
* @param string $filename 如果要另存为文件,请指定文件名,为空则直接在浏览器输出
* @param integer $bit 图像质量(1、4、8、16、24、32位)
* @param integer $compression 压缩方式,0为不压缩,1使用RLE8压缩算法进行压缩
*
* @return integer
*/
function imagebmp(&$im, $filename = '', $bit = 8, $compression = 0)
{
if (!in_array($bit, array(1, 4, 8, 16, 24, 32)))
{
$bit = 8;
}
else if ($bit == 32) // todo:32 bit
{
$bit = 24;
}

$bits = pow(2, $bit);

// 调整调色板
imagetruecolortopalette($im, true, $bits);
$width = imagesx($im);
$height = imagesy($im);
$colors_num = imagecolorstotal($im);

if ($bit <= 8)
{
// 颜色索引
$rgb_quad = '';
for ($i = 0; $i < $colors_num; $i ++)
{
$colors = imagecolorsforindex($im, $i);
$rgb_quad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0";
}

// 位图数据
$bmp_data = '';

// 非压缩
if ($compression == 0 || $bit < 8)
{
if (!in_array($bit, array(1, 4, 8)))
{
$bit = 8;
}

$compression = 0;

// 每行字节数必须为4的倍数,补齐。
$extra = '';
$padding = 4 - ceil($width / (8 / $bit)) % 4;
if ($padding % 4 != 0)
{
$extra = str_repeat("\0", $padding);
}

for ($j = $height - 1; $j >= 0; $j --)
{
$i = 0;
while ($i < $width)
{
$bin = 0;
$limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0;

for ($k = 8 - $bit; $k >= $limit; $k -= $bit)
{
$index = imagecolorat($im, $i, $j);
$bin |= $index << $k;
$i ++;
}

$bmp_data .= chr($bin);
}

$bmp_data .= $extra;
}
}
// RLE8 压缩
else if ($compression == 1 && $bit == 8)
{
for ($j = $height - 1; $j >= 0; $j --)
{
$last_index = "\0";
$same_num = 0;
for ($i = 0; $i <= $width; $i ++)
{
$index = imagecolorat($im, $i, $j);
if ($index !== $last_index || $same_num > 255)
{
if ($same_num != 0)
{
$bmp_data .= chr($same_num) . chr($last_index);
}

$last_index = $index;
$same_num = 1;
}
else
{
$same_num ++;
}
}

$bmp_data .= "\0\0";
}

$bmp_data .= "\0\1";
}

$size_quad = strlen($rgb_quad);
$size_data = strlen($bmp_data);
}
else
{
// 每行字节数必须为4的倍数,补齐。
$extra = '';
$padding = 4 - ($width * ($bit / 8)) % 4;
if ($padding % 4 != 0)
{
$extra = str_repeat("\0", $padding);
}

// 位图数据
$bmp_data = '';

for ($j = $height - 1; $j >= 0; $j --)
{
for ($i = 0; $i < $width; $i ++)
{
$index = imagecolorat($im, $i, $j);
$colors = imagecolorsforindex($im, $index);

if ($bit == 16)
{
$bin = 0 << $bit;

$bin |= ($colors['red'] >> 3) << 10;
$bin |= ($colors['green'] >> 3) << 5;
$bin |= $colors['blue'] >> 3;

$bmp_data .= pack("v", $bin);
}
else
{
$bmp_data .= pack("c*", $colors['blue'], $colors['green'], $colors['red']);
}

// todo: 32bit;
}

$bmp_data .= $extra;
}

$size_quad = 0;
$size_data = strlen($bmp_data);
$colors_num = 0;
}

// 位图文件头
$file_header = "BM" . pack("V3", 54 + $size_quad + $size_data, 0, 54 + $size_quad);

// 位图信息头
$info_header = pack("V3v2V*", 0x28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0);

// 写入文件
if ($filename != '')
{
$fp = fopen("test.bmp", "wb");

fwrite($fp, $file_header);
fwrite($fp, $info_header);
fwrite($fp, $rgb_quad);
fwrite($fp, $bmp_data);
fclose($fp);

return 1;
}

// 浏览器输出
header("Content-Type: image/bmp");
echo $file_header . $info_header;
echo $rgb_quad;
echo $bmp_data;

return 1;
}





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.