登录  /  注册

[GD]生成bmp格式的图片_PHP

php中文网
发布: 2016-06-01 12:34:44
原创
1192人浏览过
 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     {
        // 颜色索引
        $rgb_quad = '';
        for ($i = 0; $i         {
            $colors = imagecolorsforindex($im, $i);
            $rgb_quad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0";
        }
       
        // 位图数据
        $bmp_data = '';
 
        // 非压缩
        if ($compression == 0 || $bit         {
            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                 {
                    $bin = 0;
                    $limit = $width - $i  
                    for ($k = 8 - $bit; $k >= $limit; $k -= $bit)
                    {
                        $index = imagecolorat($im, $i, $j);
                        $bin |= $index                         $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                 {
                    $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             {
                $index  = imagecolorat($im, $i, $j);
                $colors = imagecolorsforindex($im, $index);
               
                if ($bit == 16)
                {
                    $bin = 0  
                    $bin |= ($colors['red'] >> 3)                     $bin |= ($colors['green'] >> 3)                     $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;
}
参考资料:BMP文件格式分析 (出处没有找到…)

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号