PHP converts BMP format images into JPG format programs

Source: Internet
Author: User
Tags chr fread instance method ord pow reserved strlen
The code is as follows Copy Code

function Imagebmp ($img, $file = "", $rle =0)
{


$colorcount =imagecolorstotal ($IMG);

$transparent =imagecolortransparent ($IMG);
$istransparent = $transparent!=-1;


if ($istransparent) $colorcount--;

if ($colorcount ==0) {$colorcount =0; $bitcount = 24;};
if (($colorcount >0) and ($colorcount <=2)) {$colorcount =2; $bitcount = 1;};
if (($colorcount >2) and ($colorcount <=16)) {$colorcount =16; $bitcount = 4;};
if (($colorcount >16) and ($colorcount <=256)) {$colorcount =0; $bitcount = 8;};


$width =imagesx ($IMG);
$height =imagesy ($IMG);

$zbytek = ($width/(8/$bitcount)%4)%4;

if ($bitcount <24) $palsize =pow (2, $bitcount) *4;

$size = (Floor ($width/(8/$bitcount)) + $zbytek) * $height +54;
$size + + $palsize;
$offset =54+ $palsize;

Bitmap file Header


$ret = ' BM '; Header (2b)


$ret. = Int_to_dword ($size); Size of file (4b)


$ret. = Int_to_dword (0); Reserved (4b)


$ret. = Int_to_dword ($offset); BYTE location in the ' file which is ' A-a-4b


Bitmap Info Header


$ret. = Int_to_dword (40); Size of Bitmapinfoheader (4b)


$ret. = Int_to_dword ($width); Width of bitmap (4b)


$ret. = Int_to_dword ($height); Height of bitmap (4b)


$ret. = Int_to_word (1); biplanes = 1 (2b)


$ret. = Int_to_word ($bitcount); biBitCount = {1 (mono) or 4 (CLR) or 8 (256 CLR) or MIL} (2b)


$ret. = Int_to_dword ($rle); RLE Compression (4b)


$ret. = Int_to_dword (0); Width x Height (4b)


$ret. = Int_to_dword (0); Bixpelspermeter (4b)


$ret. = Int_to_dword (0); Biypelspermeter (4b)


$ret. = Int_to_dword (0); Number of palettes used (4b)


$ret. = Int_to_dword (0); Number of important colour (4b)


Image data

                $cc = $colorcount;
                $sl 1=strlen ($ret );
                if ($cc ==0) $cc = 256;
                if ($bitcount < )
                    {
                     $colortotal =imagecolorstotal ($img);
                      if ($istransparent) $colortotal--;

for ($p =0; $p &lt; $colortotal; $p + +)


{


$color =imagecolorsforindex ($img, $p);


$ret. =inttobyte ($color ["Blue"]);


$ret. =inttobyte ($color ["green"]);


$ret. =inttobyte ($color ["Red"]);


$ret. =inttobyte (0); Reserved


};

$CT = $colortotal;


for ($p = $colortotal; $p &lt; $cc; $p + +)


{


$ret. =inttobyte (0);


$ret. =inttobyte (0);


$ret. =inttobyte (0);


$ret. =inttobyte (0); Reserved


};


};


if ($bitcount <=8)
{

for ($y = $height-1; $y >=0; $y-)
{
$bwrite = "";
for ($x =0; $x < $width; $x + +)
{
$color =imagecolorat ($img, $x, $y);
$bwrite. =decbinx ($color, $bitcount);
if (strlen ($bwrite) ==8)
{
$retd. =inttobyte (Bindec ($bwrite));
$bwrite = "";
};
};

if ((strlen ($bwrite) <8) and (strlen ($bwrite)!=0))
{
$SL =strlen ($bwrite);
for ($t =0; $t <8-$sl; $t + +)
$sl. = "0";
$retd. =inttobyte (Bindec ($bwrite));
};
for ($z =0; $z < $zbytek; $z + +)
$retd. =inttobyte (0);
};
};

if (($rle ==1) and ($bitcount ==8))
{
for ($t =0; $t <strlen ($retd); $t +=4)
{
if ($t!=0)
if (($t)% $width ==0)
$ret. =CHR (0). chr (0);

   if (($t +5)% $width ==0)
   {
     $ret. =CHR (0). chr (5). substr ($ Retd, $t, 5). chr (0);
     $t +=1;
  }
   if (($t +6)% $width ==0)
    {
     $ret. =CHR (0). chr (6). substr ($retd, $t, 6);
     $t +=2;
   }
    Else
    {
     $ret. =CHR (0). chr (4). substr ($RETD, $t, 4);
   };
 };
  $ret. =CHR (0). chr (1);
}
Else
{
$ret. = $retd;
};


if ($bitcount ==24)
{
for ($z =0; $z < $zbytek; $z + +)
$dopl. =CHR (0);

for ($y = $height-1; $y &gt;=0; $y-)


{


for ($x =0; $x &lt; $width; $x + +)


{


$color =imagecolorsforindex ($img, Imagecolorat ($img, $x, $y));


$ret. =CHR ($color ["Blue"]). chr ($color ["green"]). chr ($color ["Red"]);


}


$ret. = $DOPL;


};

};

if ($file!= "")
{
$r = ($f =fopen ($file, "w"));
$r = $r and Fwrite ($f, $ret);
$r = $r and fclose ($f);
return $r;
}
Else
{
Echo $ret;
};
};


/*
*------------------------------------------------------------
* Imagecreatefrombmp
*------------------------------------------------------------
*-Reads image from a BMP file
*
* Parameters: $file-target file to load
*
* Returns:image ID
*/

function Imagecreatefrombmp ($file)
{
Global $currentbit, $echomode;

$f =fopen ($file, "R");
$header =fread ($f, 2);

if ($header = = "BM")
{
$size =freaddword ($f);
$reserved 1=freadword ($f);
$reserved 2=freadword ($f);
$firstbyteofimage =freaddword ($f);

$sizebitmapinfoheader =freaddword ($f);
$width =freaddword ($f);
$height =freaddword ($f);
$biplanes =freadword ($f);
$bibitcount =freadword ($f);
$rlecompression =freaddword ($f);
$widthxheight =freaddword ($f);
$bixpelspermeter =freaddword ($f);
$biypelspermeter =freaddword ($f);
$numberofpalettesused =freaddword ($f);
$numberofimportantcolors =freaddword ($f);

if ($bibitcount <24)
{
$img =imagecreate ($width, $height);
$colors =pow (2, $bibitcount);
for ($p =0; $p < $colors; $p + +)
{
$b =freadbyte ($f);
$g =freadbyte ($f);
$r =freadbyte ($f);
$reserved =freadbyte ($f);
$palette []=imagecolorallocate ($img, $r, $g, $b);
};


if ($rlecompression ==0)
{
$zbytek = (4-ceil (($width/(8/$bibitcount)))%4;

for ($y = $height-1; $y >=0; $y-)
{
$currentbit = 0;
for ($x =0; $x < $width; $x + +)
{
$c =freadbits ($f, $bibitcount);
Imagesetpixel ($img, $x, $y, $palette [$c]);
};
if ($currentbit!=0) {freadbyte ($f);};
for ($g =0; $g < $zbytek; $g + +)
Freadbyte ($f);
};

};
};


if ($rlecompression ==1)//$bi _rle8
{
$y = $height;

$POCETB = 0;

while (true)
{
$y--;
$prefix =freadbyte ($f);
$suffix =freadbyte ($f);
$pocetb +=2;

$echoit =false;

if ($echoit) echo "prefix: $prefix suffix: $suffix <br>";
if ($prefix ==0) and ($suffix ==1)) break;
if (feof ($f)) break;

while (!) ( ($prefix ==0) and ($suffix ==0))
{
if ($prefix ==0)
{
$pocet = $suffix;
$data. =fread ($f, $pocet);
$pocetb + + $pocet;
if ($pocetb%2==1) {freadbyte ($f); $pocetb + +;};
};
if ($prefix >0)
{
$pocet = $prefix;
for ($r =0; $r < $pocet; $r + +)
$data. =CHR ($suffix);
};
$prefix =freadbyte ($f);
$suffix =freadbyte ($f);
$pocetb +=2;
if ($echoit) echo "prefix: $prefix suffix: $suffix <br>";
};

for ($x =0; $x <strlen ($data); $x + +)
{
Imagesetpixel ($img, $x, $y, $palette [Ord ($data [$x])]);
};
$data = "";

};

};


if ($rlecompression ==2)//$bi _rle4
{
$y = $height;
$POCETB = 0;

/*while (!feof ($f))
echo Freadbyte ($f). " _ ". Freadbyte ($f)." <br> ";
while (true)
{
Break
$y--;
$prefix =freadbyte ($f);
$suffix =freadbyte ($f);
$pocetb +=2;

$echoit =false;

if ($echoit) echo "prefix: $prefix suffix: $suffix <br>";
if ($prefix ==0) and ($suffix ==1)) break;
if (feof ($f)) break;

while (!) ( ($prefix ==0) and ($suffix ==0))
{
if ($prefix ==0)
{
$pocet = $suffix;

$currentbit = 0;


for ($h =0; $h &lt; $pocet; $h + +)


$data. =CHR (Freadbits ($f, 4));


if ($currentbit!=0) freadbits ($f, 4);


$pocetb +=ceil (($pocet/2));


if ($pocetb%2==1) {freadbyte ($f); $pocetb + +;};


};


if ($prefix &gt;0)


{


$pocet = $prefix;


$i = 0;


for ($r =0; $r &lt; $pocet; $r + +)


{


if ($i%2==0)


{


$data. =CHR ($suffix%16);


}


Else


{


$data. =CHR (Floor ($suffix/16));


};


$i + +;


};


};


$prefix =freadbyte ($f);


$suffix =freadbyte ($f);


$pocetb +=2;


if ($echoit) echo "prefix: $prefix suffix: $suffix &lt;br&gt;";


};

for ($x =0; $x <strlen ($data); $x + +)
{
Imagesetpixel ($img, $x, $y, $palette [Ord ($data [$x])]);
};
$data = "";

};

};


if ($bibitcount ==24)
{
$img =imagecreatetruecolor ($width, $height);
$zbytek = $width% 4;

for ($y = $height-1; $y >=0; $y-)
{
for ($x =0; $x < $width; $x + +)
{
$b =freadbyte ($f);
$g =freadbyte ($f);
$r =freadbyte ($f);
$color =imagecolorexact ($img, $r, $g, $b);
if ($color ==-1) $color =imagecolorallocate ($img, $r, $g, $b);
Imagesetpixel ($img, $x, $y, $color);
}
for ($z =0; $z < $zbytek; $z + +)
Freadbyte ($f);
};
};
return $img;

};


Fclose ($f);


};

/*
* Helping functions:
*-------------------------
*
* Freadbyte ($file)-Reads 1 byte from $file
* Freadword ($file)-Reads 2 bytes (1 word) from $file
* Freaddword ($file)-Reads 4 bytes (1 dword) from $file
* Freadlngint ($file)-Same as Freaddword ($file)
* DECBIN8 ($d)-returns binary string of D zero filled to 8
* Retbits ($byte, $start, $len)-Returns bits $start-> $start + $len from $byte
* Freadbits ($file, $count)-reads next $count bits from $file
* Rgbtohex ($r, $g, $b)-Convert $r, $g, $b to Hex
* Int_to_dword ($n)-Returns 4 byte representation of $n
* Int_to_word ($n)-Returns 2 byte representation of $n
*/

function Freadbyte ($f)
{
Return Ord (Fread ($f, 1));
};

function Freadword ($f)
{
$b 1=freadbyte ($f);
$b 2=freadbyte ($f);
return $b 2*256+ $b 1;
};


function Freadlngint ($f)
{
Return Freaddword ($f);
};

function Freaddword ($f)
{
$b 1=freadword ($f);
$b 2=freadword ($f);
return $b 2*65536+ $b 1;
};

function Retbits ($byte, $start, $len)
{
$bin =decbin8 ($byte);
$r =bindec (substr ($bin, $start, $len));
return $r;

};

$currentbit = 0;
function Freadbits ($f, $count)
{
Global $currentbit, $smode;
$byte =freadbyte ($f);
$lastcbit = $currentbit;
$currentbit + + $count;
if ($currentbit ==8)
{
$currentbit = 0;
}
Else
{
Fseek ($f, Ftell ($f)-1);
};
Return Retbits ($byte, $lastcbit, $count);
};

Function Rgbtohex ($red, $green, $blue)
  {
   $hred =dechex ($red); if (strlen ($hred) ==1) $hred = "0$hred";
   $hgreen =dechex ($green); if (strlen ($hgreen) ==1) $hgreen = "0$hgreen";
   $hblue =dechex ($blue); if (strlen ($hblue) ==1) $hblue = "0$hblue";
   return ($hred. $hgreen. $hblue);
 };

function Int_to_dword ($n)
{
Return Chr ($n & 255). chr (($n >> 8) & 255) Chr (($n >>) & 255) Chr (($n >>) & 255);
}
function Int_to_word ($n)
{
Return Chr ($n & 255). chr (($n >> 8) & 255);
}


function Decbin8 ($d)
{
Return Decbinx ($d, 8);
};

function Decbinx ($d, $n)
{
$bin =decbin ($d);
$sbin =strlen ($bin);
for ($j =0; $j < $n-$sbin; $j + +)
$bin = "0$bin";
return $bin;
};

function Inttobyte ($n)
{
return Chr ($n);
};

Instance method

The code is as follows Copy Code
Include_once (' bmp.php ');
$image =imagecreatefrombmp (' a.bmp ');
Imagejpeg ($image, ' a.jpeg ');
Imagedestroy ($image);

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.