[GD] Create a BMP format picture (imagebmp) _php Tutorial

Source: Internet
Author: User
Tags bmp image

GD library does not produce BMP image function, so wrote a, this function has a compression algorithm is not written, but already enough. The students who need it can take a look.

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

$im: Image Resources
$filename: If you want to save as a file, specify a filename, or empty it directly in the browser output
$bit: Image quality (1, 4, 8, 16, 24, 32 bit)
$compression: compression, 0 uncompressed, 1 compressed using RLE8 compression algorithm

Note: This function still requires the support of the GD library.

Demo:

$im = Imagecreatefrompng ("Test.png");
Imagebmp ($im);
Imagedestroy ($im);
Source:

/**
* Create BMP format Picture
*
* @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 Resources
* @param string $filename If you want to save as a file, specify a filename, or empty it directly in the browser output
* @param integer $bit image quality (1, 4, 8, 16, 24, 32 bit)
* @param integer $compression compression, 0 uncompressed, 1 compressed using RLE8 compression algorithm
*
* @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 = = +)//todo:32 bit
{
$bit = 24;
}

$bits = POW (2, $bit);

Adjust Palette
Imagetruecolortopalette ($im, True, $bits);
$width = Imagesx ($im);
$height = Imagesy ($im);
$colors _num = Imagecolorstotal ($im);

if ($bit <= 8)
{
Color Index
$rgb _quad =;
for ($i = 0; $i < $colors _num; $i + +)
{
$colors = Imagecolorsforindex ($im, $i);
$rgb _quad. = Chr ($colors [Blue]). Chr ($colors [green]). Chr ($colors [red]). "";
}

Bitmap data
$bmp _data =;

Non-compressed
if ($compression = = 0 | | $bit < 8)
{
if (!in_array ($bit, Array (1, 4, 8)))
{
$bit = 8;
}

$compression = 0;

The number of bytes per line must be a multiple of 4, padded.
$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 compression
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

http://www.bkjia.com/PHPjc/508374.html www.bkjia.com true http://www.bkjia.com/PHPjc/508374.html techarticle GD Library does not produce BMP image function, so wrote a, this function has a compression algorithm is not written, but already enough. The students who need it can take a look. int image ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.