[GD] Generate images in bmp format (imagebmp)_PHP tutorial

WBOY
Release: 2016-07-13 17:35:06
Original
1246 people have browsed it

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:

/**
* Create bmp format pictures
*
* @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 Image resource
* @param string $filename If you want to save as a file, Please specify the file name, if it is empty, it will be output directly in the browser
* @param integer $bit Image quality (1, 4, 8, 16, 24, 32 bits)
* @param integer $compression compression method, 0 means no compression, 1 uses RLE8 compression algorithm for compression
*
* @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]) . "";
        }
       
        // 位图数据
        $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("", $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 = "";
                $same_num   = 0;
                for ($i = 0; $i <= $width; $i ++)
                {
                    $index = imagecolorat($im, $i, $j);
                    if ($index !== $last_index || $same_num > 255)
               &nb

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/508374.htmlTechArticleGD库里没有生成bmp图片的函数,所以自己写了一个,这个函数尚有一个压缩算法没有写,不过已经够用了。需要的同学可以看看。 int image...

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template