php轉換bmp圖片位深度,bmp圖片位深修改,imagecreatefrombmp、imagebmp php處理bmp文件

   =>  

1、使用 (php>7.0)

//加載bmp文件
$im = imagecreatefrombmp("D:/wwwroot/bmp_logo.bmp");
//調用bmp轉換方法
imagebmp_custom($im, "D:/wwwroot/bmp_logo_new.bmp",16);
//釋放與 image 關聯的內存
imagedestroy($im);

 
/**
 * 轉自:https://www.cnblogs.com/mengdejun/p/imagecreatefrombmp_imagebmp.html
 * 格式組成典型的BMP圖像文件由四部分組成:
 * 1:位圖頭文件數據結構,它包含BMP圖像文件的類型、顯示內容等信息;
 * 2:位圖信息數據結構,它包含有BMP圖像的寬、高、壓縮方法,以及定義顏色等信息;
 * 3:調色板,這個部分是可選的,有些位圖需要調色板,有些位圖,比如真彩色圖(24位的BMP)就不需要調色板;
 * 4:位圖數據,這部分的內容根據BMP位圖使用的位數不同而不同,在24位圖中直接使用RGB,而其他的小於24位的使用調色板中顏色索引值。
 * @param object $im bmp文件內容
 * @param string $filename 轉換後文件保存名稱
 * @param int $bit 位深度
 * @param int $compression 是否壓縮
 * @return bool
 */
function imagebmp_custom(&$im, $filename = '', $bit = 8, $compression = 0)
{
    if (!in_array($bit, array(1, 4, 8, 16, 24, 32)))
    {
        $bit = 8;  //記錄每個像素所佔計算機字節的位數,默認爲8位
    }
    else if ($bit == 32) // todo:32 bit
    {
        $bit = 24;
    }
    $bits = pow(2, $bit); //表示待創建的圖像一共由$bits種顏色組成

    // 將圖像調整爲調色板圖像,便於以後繪圖,如果是24位BMP位圖,那麼就不需要這一步
    /*調色板是被保存在一個RGBQUAD結構的數組中,該結構指出了每一種顏色的紅、綠、藍的分量值。
     *位數組中的每一個索引都對應於一個調色板項(即一個RGBQUAD結構),應用程序將根據這種對
     *應關係,將像素索引值轉換爲像素RGB值(真實的像素顏色)
     * */
    imagetruecolortopalette($im, true, $bits);
    $width  = imagesx($im);
    $height = imagesy($im);
    $colors_num = imagecolorstotal($im); //返回的一般都是256
    // 顏色索引
    $rgb_quad = '';
    if ($bit <= 8)//僅針對1,4,8位BMP位圖建立顏色索引
    {

        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 = '';

        // 非壓縮,即對RGB值不進行壓縮,讀取時順序爲BGR,高位在前,低位在後
        if ($compression == 0 || $bit < 8)
        {
            $compression = 0;

            // 每行字節數必須爲4的倍數,補齊。
            $extra = '';
            $padding = 32 - ( $width * $bit ) % 32 ;
            if ($padding % 32 != 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($filename, "wb");
        fwrite($fp, $file_header);
        fwrite($fp, $info_header);
        if($rgb_quad!=='')
            fwrite($fp, $rgb_quad);
        fwrite($fp, $bmp_data);
        fclose($fp);
        return true;
    }

   /* // 瀏覽器輸出
    header("Content-Type: image/bmp");
    echo $file_header . $info_header;
    echo $rgb_quad;
    echo $bmp_data;
    return true;*/
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章