PHP convert BMP format picture to JPG format Program _php tutorial

Source: Internet
Author: User
Tags fread
PHP Tutorial convert BMP format picture to JPG format program

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);

$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 first byte of image (4b)
// Bitmap Info Header
$ret. = Int_to_dword (+);//Size of Bitmapinfoheader (4b)
$ret. = Int_to_dword ($width);//WID Th 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 (n-CLR) or (2b)
$ret . = Int_to_dword ($rle); RLE compression (4b)
$ret. = Int_to_dword (0);//width x Height (4b)
$ret. = Int_to_dword (0);//Bixpelspermete R (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 <24)
{
$colortotal =imagecolorstotal ($IMG);
if ($istransparent) $colortotal-;

for ($p =0; $p < $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 < $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<>
{
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 >=0; $y-)
{
for ($x =0; $x < $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)%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
";
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
";
};

for ($x =0; $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)."
";*/
while (true)
{
Break
$y--;
$prefix =freadbyte ($f);
$suffix =freadbyte ($f);
$pocetb +=2;

$echoit =false;

if ($echoit) echo "prefix: $prefix suffix: $suffix
";
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 < $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 >0)
{
$pocet = $prefix;
$i = 0;
for ($r =0; $r < $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
";
};

for ($x =0; $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

include_once (' bmp.php ');
$image =imagecreatefrombmp (' a.bmp ');
Imagejpeg ($image, ' a.jpeg ');
Imagedestroy ($image);


http://www.bkjia.com/PHPjc/444910.html www.bkjia.com true http://www.bkjia.com/PHPjc/444910.html techarticle PHP Tutorial convert BMP format picture to JPG format program function imagebmp ($img, $file =, $rle =0) {$colorcount =imagecolorstotal ($img); $ Transparent=imagecolortransparent ($IMG); $istranspa ...

  • 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.