imagecreatefrombmp()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    #1

    imagecreatefrombmp()

    Someone posted a function to implement imagecreatefrom bmp()but it
    fails in imagecreatedfro mgd() in my test. Can the author check to see
    if theres a typo? Its very useful if it will work!

    Brian


    "Erwin Bon" <ernie@verkerk. nl> wrote in message news:<3fc9d260$ 0$4671$1b62eedf @news.euronet.n l>...[color=blue]
    > Hello,
    >
    > I need to be able to read windows bitmap files (*.bmp) into the GD-functions
    > in order to create a jpg-file from it. Is such a thing possible. I seem te
    > be unable to find the required functions.
    >
    > Hope you can help me.
    >
    > Regards, Erwin[/color]

    The GD library doesn't support .BMP files for some reason. I've
    written a function that converts .BMP files to .GD, a very simple
    format that GD uses.

    The following code implements imagecreatefrom bmp(). It first convert
    the BMP to GD, saving it to a temporary location, then open the image
    using imagecreatefrom gd().


    <?

    function ConvertBMP2GD($ src, $dest = false) {
    if(!($src_f = fopen($src, "rb"))) {
    return false;
    }
    if(!($dest_f = fopen($dest, "wb"))) {
    return false;
    }
    $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f,
    14));
    $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant",
    fread($src_f, 40));

    extract($info);
    extract($header );

    if($type != 0x4D42) { // signature "BM"
    return false;
    }

    $palette_size = $offset - 54;
    $ncolor = $palette_size / 4;
    $gd_header = "";
    // true-color vs. palette
    $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
    $gd_header .= pack("n2", $width, $height);
    $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
    if($palette_siz e) {
    $gd_header .= pack("n", $ncolor);
    }
    // no transparency
    $gd_header .= "\xFF\xFF\xFF\x FF";

    fwrite($dest_f, $gd_header);

    if($palette_siz e) {
    $palette = fread($src_f, $palette_size);
    $gd_palette = "";
    $j = 0;
    while($j < $palette_size) {
    $b = $palette{$j++};
    $g = $palette{$j++};
    $r = $palette{$j++};
    $a = $palette{$j++};
    $gd_palette .= "$r$g$b$a";
    }
    $gd_palette .= str_repeat("\x0 0\x00\x00\x00", 256 - $ncolor);
    fwrite($dest_f, $gd_palette);
    }

    $scan_line_size = (($bits * $width) + 7) >> 3;
    $scan_line_alig n = ($scan_line_siz e & 0x03) ? 4 - ($scan_line_siz e &
    0x03) : 0;

    for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
    // BMP stores scan lines starting from bottom
    fseek($src_f, $offset + (($scan_line_si ze + $scan_line_alig n) *
    $l));
    $scan_line = fread($src_f, $scan_line_size );
    if($bits == 24) {
    $gd_scan_line = "";
    $j = 0;
    while($j < $scan_line_size ) {
    $b = $scan_line{$j++ };
    $g = $scan_line{$j++ };
    $r = $scan_line{$j++ };
    $gd_scan_line .= "\x00$r$g$b ";
    }
    }
    else if($bits == 8) {
    $gd_scan_line = $scan_line;
    }
    else if($bits == 4) {
    $gd_scan_line = "";
    $j = 0;
    while($j < $scan_line_size ) {
    $byte = ord($scan_line{ $j++});
    $p1 = chr($byte >> 4);
    $p2 = chr($byte & 0x0F);
    $gd_scan_line .= "$p1$p2";
    } $gd_scan_line = substr($gd_scan _line, 0, $width);
    }
    else if($bits == 1) {
    $gd_scan_line = "";
    $j = 0;
    while($j < $scan_line_size ) {
    $byte = ord($scan_line{ $j++});
    $p1 = chr((int) (($byte & 0x80) != 0));
    $p2 = chr((int) (($byte & 0x40) != 0));
    $p3 = chr((int) (($byte & 0x20) != 0));
    $p4 = chr((int) (($byte & 0x10) != 0));
    $p5 = chr((int) (($byte & 0x08) != 0));
    $p6 = chr((int) (($byte & 0x04) != 0));
    $p7 = chr((int) (($byte & 0x02) != 0));
    $p8 = chr((int) (($byte & 0x01) != 0));
    $gd_scan_line .= "$p1$p2$p3$p4$p 5$p6$p7$p8";
    } $gd_scan_line = substr($gd_scan _line, 0, $width);
    }

    fwrite($dest_f, $gd_scan_line);
    }
    fclose($src_f);
    fclose($dest_f) ;
    return true;
    }

    function imagecreatefrom bmp($filename) {
    $tmp_name = tempnam("/tmp", "GD");
    if(ConvertBMP2G D($filename, $tmp_name)) {
    $img = imagecreatefrom gd($tmp_name);
    unlink($tmp_nam e);
    return $img;
    } return false;
    }

    $img = imagecreatefrom bmp("test24bit. bmp");
    imagejpeg($img, "test.jpg") ;

    ?>
    <img src="test.jpg">
  • Chung Leong

    #2
    Re: imagecreatefrom bmp()

    Make sure you have GD2 extension enabled.

    Uzytkownik "Brian" <brianc8876@aol .com> napisal w wiadomosci
    news:d080c1cd.0 402170619.17175 6e1@posting.goo gle.com...[color=blue]
    > Someone posted a function to implement imagecreatefrom bmp()but it
    > fails in imagecreatedfro mgd() in my test. Can the author check to see
    > if theres a typo? Its very useful if it will work!
    >
    > Brian
    >
    >
    > "Erwin Bon" <ernie@verkerk. nl> wrote in message[/color]
    news:<3fc9d260$ 0$4671$1b62eedf @news.euronet.n l>...[color=blue][color=green]
    > > Hello,
    > >
    > > I need to be able to read windows bitmap files (*.bmp) into the[/color][/color]
    GD-functions[color=blue][color=green]
    > > in order to create a jpg-file from it. Is such a thing possible. I seem[/color][/color]
    te[color=blue][color=green]
    > > be unable to find the required functions.
    > >
    > > Hope you can help me.
    > >
    > > Regards, Erwin[/color]
    >
    > The GD library doesn't support .BMP files for some reason. I've
    > written a function that converts .BMP files to .GD, a very simple
    > format that GD uses.
    >
    > The following code implements imagecreatefrom bmp(). It first convert
    > the BMP to GD, saving it to a temporary location, then open the image
    > using imagecreatefrom gd().
    >
    >
    > <?
    >
    > function ConvertBMP2GD($ src, $dest = false) {
    > if(!($src_f = fopen($src, "rb"))) {
    > return false;
    > }
    > if(!($dest_f = fopen($dest, "wb"))) {
    > return false;
    > }
    > $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f,
    > 14));
    > $info =[/color]
    unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyr
    es/Vncolor/Vimportant",[color=blue]
    > fread($src_f, 40));
    >
    > extract($info);
    > extract($header );
    >
    > if($type != 0x4D42) { // signature "BM"
    > return false;
    > }
    >
    > $palette_size = $offset - 54;
    > $ncolor = $palette_size / 4;
    > $gd_header = "";
    > // true-color vs. palette
    > $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
    > $gd_header .= pack("n2", $width, $height);
    > $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
    > if($palette_siz e) {
    > $gd_header .= pack("n", $ncolor);
    > }
    > // no transparency
    > $gd_header .= "\xFF\xFF\xFF\x FF";
    >
    > fwrite($dest_f, $gd_header);
    >
    > if($palette_siz e) {
    > $palette = fread($src_f, $palette_size);
    > $gd_palette = "";
    > $j = 0;
    > while($j < $palette_size) {
    > $b = $palette{$j++};
    > $g = $palette{$j++};
    > $r = $palette{$j++};
    > $a = $palette{$j++};
    > $gd_palette .= "$r$g$b$a";
    > }
    > $gd_palette .= str_repeat("\x0 0\x00\x00\x00", 256 - $ncolor);
    > fwrite($dest_f, $gd_palette);
    > }
    >
    > $scan_line_size = (($bits * $width) + 7) >> 3;
    > $scan_line_alig n = ($scan_line_siz e & 0x03) ? 4 - ($scan_line_siz e &
    > 0x03) : 0;
    >
    > for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
    > // BMP stores scan lines starting from bottom
    > fseek($src_f, $offset + (($scan_line_si ze + $scan_line_alig n) *
    > $l));
    > $scan_line = fread($src_f, $scan_line_size );
    > if($bits == 24) {
    > $gd_scan_line = "";
    > $j = 0;
    > while($j < $scan_line_size ) {
    > $b = $scan_line{$j++ };
    > $g = $scan_line{$j++ };
    > $r = $scan_line{$j++ };
    > $gd_scan_line .= "\x00$r$g$b ";
    > }
    > }
    > else if($bits == 8) {
    > $gd_scan_line = $scan_line;
    > }
    > else if($bits == 4) {
    > $gd_scan_line = "";
    > $j = 0;
    > while($j < $scan_line_size ) {
    > $byte = ord($scan_line{ $j++});
    > $p1 = chr($byte >> 4);
    > $p2 = chr($byte & 0x0F);
    > $gd_scan_line .= "$p1$p2";
    > } $gd_scan_line = substr($gd_scan _line, 0, $width);
    > }
    > else if($bits == 1) {
    > $gd_scan_line = "";
    > $j = 0;
    > while($j < $scan_line_size ) {
    > $byte = ord($scan_line{ $j++});
    > $p1 = chr((int) (($byte & 0x80) != 0));
    > $p2 = chr((int) (($byte & 0x40) != 0));
    > $p3 = chr((int) (($byte & 0x20) != 0));
    > $p4 = chr((int) (($byte & 0x10) != 0));
    > $p5 = chr((int) (($byte & 0x08) != 0));
    > $p6 = chr((int) (($byte & 0x04) != 0));
    > $p7 = chr((int) (($byte & 0x02) != 0));
    > $p8 = chr((int) (($byte & 0x01) != 0));
    > $gd_scan_line .= "$p1$p2$p3$p4$p 5$p6$p7$p8";
    > } $gd_scan_line = substr($gd_scan _line, 0, $width);
    > }
    >
    > fwrite($dest_f, $gd_scan_line);
    > }
    > fclose($src_f);
    > fclose($dest_f) ;
    > return true;
    > }
    >
    > function imagecreatefrom bmp($filename) {
    > $tmp_name = tempnam("/tmp", "GD");
    > if(ConvertBMP2G D($filename, $tmp_name)) {
    > $img = imagecreatefrom gd($tmp_name);
    > unlink($tmp_nam e);
    > return $img;
    > } return false;
    > }
    >
    > $img = imagecreatefrom bmp("test24bit. bmp");
    > imagejpeg($img, "test.jpg") ;
    >
    > ?>
    > <img src="test.jpg">[/color]


    Comment

    • Brian

      #3
      Re: imagecreatefrom bmp()

      I am running 4.3.0 with the extensions enabled. It fails on
      imagecreatefrom gd(), so its not creating a valid gd. I cut and pasted
      it directly and ran it as is.

      Brian


      "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<AvqdnX8dI OfINa_dRVn-gg@comcast.com> ...[color=blue]
      > Make sure you have GD2 extension enabled.
      >
      > Uzytkownik "Brian" <brianc8876@aol .com> napisal w wiadomosci
      > news:d080c1cd.0 402170619.17175 6e1@posting.goo gle.com...[color=green]
      > > Someone posted a function to implement imagecreatefrom bmp()but it
      > > fails in imagecreatedfro mgd() in my test. Can the author check to see
      > > if theres a typo? Its very useful if it will work!
      > >
      > > Brian
      > >
      > >
      > > "Erwin Bon" <ernie@verkerk. nl> wrote in message[/color]
      > news:<3fc9d260$ 0$4671$1b62eedf @news.euronet.n l>...[color=green][color=darkred]
      > > > Hello,
      > > >
      > > > I need to be able to read windows bitmap files (*.bmp) into the[/color][/color]
      > GD-functions[color=green][color=darkred]
      > > > in order to create a jpg-file from it. Is such a thing possible. I seem[/color][/color]
      > te[color=green][color=darkred]
      > > > be unable to find the required functions.
      > > >
      > > > Hope you can help me.
      > > >
      > > > Regards, Erwin[/color]
      > >
      > > The GD library doesn't support .BMP files for some reason. I've
      > > written a function that converts .BMP files to .GD, a very simple
      > > format that GD uses.
      > >
      > > The following code implements imagecreatefrom bmp(). It first convert
      > > the BMP to GD, saving it to a temporary location, then open the image
      > > using imagecreatefrom gd().
      > >
      > >
      > > <?
      > >
      > > function ConvertBMP2GD($ src, $dest = false) {
      > > if(!($src_f = fopen($src, "rb"))) {
      > > return false;
      > > }
      > > if(!($dest_f = fopen($dest, "wb"))) {
      > > return false;
      > > }
      > > $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f,
      > > 14));
      > > $info =[/color]
      > unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyr
      > es/Vncolor/Vimportant",[color=green]
      > > fread($src_f, 40));
      > >
      > > extract($info);
      > > extract($header );
      > >
      > > if($type != 0x4D42) { // signature "BM"
      > > return false;
      > > }
      > >
      > > $palette_size = $offset - 54;
      > > $ncolor = $palette_size / 4;
      > > $gd_header = "";
      > > // true-color vs. palette
      > > $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
      > > $gd_header .= pack("n2", $width, $height);
      > > $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
      > > if($palette_siz e) {
      > > $gd_header .= pack("n", $ncolor);
      > > }
      > > // no transparency
      > > $gd_header .= "\xFF\xFF\xFF\x FF";
      > >
      > > fwrite($dest_f, $gd_header);
      > >
      > > if($palette_siz e) {
      > > $palette = fread($src_f, $palette_size);
      > > $gd_palette = "";
      > > $j = 0;
      > > while($j < $palette_size) {
      > > $b = $palette{$j++};
      > > $g = $palette{$j++};
      > > $r = $palette{$j++};
      > > $a = $palette{$j++};
      > > $gd_palette .= "$r$g$b$a";
      > > }
      > > $gd_palette .= str_repeat("\x0 0\x00\x00\x00", 256 - $ncolor);
      > > fwrite($dest_f, $gd_palette);
      > > }
      > >
      > > $scan_line_size = (($bits * $width) + 7) >> 3;
      > > $scan_line_alig n = ($scan_line_siz e & 0x03) ? 4 - ($scan_line_siz e &
      > > 0x03) : 0;
      > >
      > > for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
      > > // BMP stores scan lines starting from bottom
      > > fseek($src_f, $offset + (($scan_line_si ze + $scan_line_alig n) *
      > > $l));
      > > $scan_line = fread($src_f, $scan_line_size );
      > > if($bits == 24) {
      > > $gd_scan_line = "";
      > > $j = 0;
      > > while($j < $scan_line_size ) {
      > > $b = $scan_line{$j++ };
      > > $g = $scan_line{$j++ };
      > > $r = $scan_line{$j++ };
      > > $gd_scan_line .= "\x00$r$g$b ";
      > > }
      > > }
      > > else if($bits == 8) {
      > > $gd_scan_line = $scan_line;
      > > }
      > > else if($bits == 4) {
      > > $gd_scan_line = "";
      > > $j = 0;
      > > while($j < $scan_line_size ) {
      > > $byte = ord($scan_line{ $j++});
      > > $p1 = chr($byte >> 4);
      > > $p2 = chr($byte & 0x0F);
      > > $gd_scan_line .= "$p1$p2";
      > > } $gd_scan_line = substr($gd_scan _line, 0, $width);
      > > }
      > > else if($bits == 1) {
      > > $gd_scan_line = "";
      > > $j = 0;
      > > while($j < $scan_line_size ) {
      > > $byte = ord($scan_line{ $j++});
      > > $p1 = chr((int) (($byte & 0x80) != 0));
      > > $p2 = chr((int) (($byte & 0x40) != 0));
      > > $p3 = chr((int) (($byte & 0x20) != 0));
      > > $p4 = chr((int) (($byte & 0x10) != 0));
      > > $p5 = chr((int) (($byte & 0x08) != 0));
      > > $p6 = chr((int) (($byte & 0x04) != 0));
      > > $p7 = chr((int) (($byte & 0x02) != 0));
      > > $p8 = chr((int) (($byte & 0x01) != 0));
      > > $gd_scan_line .= "$p1$p2$p3$p4$p 5$p6$p7$p8";
      > > } $gd_scan_line = substr($gd_scan _line, 0, $width);
      > > }
      > >
      > > fwrite($dest_f, $gd_scan_line);
      > > }
      > > fclose($src_f);
      > > fclose($dest_f) ;
      > > return true;
      > > }
      > >
      > > function imagecreatefrom bmp($filename) {
      > > $tmp_name = tempnam("/tmp", "GD");
      > > if(ConvertBMP2G D($filename, $tmp_name)) {
      > > $img = imagecreatefrom gd($tmp_name);
      > > unlink($tmp_nam e);
      > > return $img;
      > > } return false;
      > > }
      > >
      > > $img = imagecreatefrom bmp("test24bit. bmp");
      > > imagejpeg($img, "test.jpg") ;
      > >
      > > ?>
      > > <img src="test.jpg">[/color][/color]

      Comment

      • Chung Leong

        #4
        Re: imagecreatefrom bmp()

        Email me the bmp file and I'll take a look. It worked fine with my test
        image (256 colors).

        Uzytkownik "Brian" <brianc8876@aol .com> napisal w wiadomosci
        news:d080c1cd.0 402181618.32719 a51@posting.goo gle.com...[color=blue]
        > I am running 4.3.0 with the extensions enabled. It fails on
        > imagecreatefrom gd(), so its not creating a valid gd. I cut and pasted
        > it directly and ran it as is.
        >
        > Brian
        >
        >
        > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message[/color]
        news:<AvqdnX8dI OfINa_dRVn-gg@comcast.com> ...[color=blue][color=green]
        > > Make sure you have GD2 extension enabled.
        > >
        > > Uzytkownik "Brian" <brianc8876@aol .com> napisal w wiadomosci
        > > news:d080c1cd.0 402170619.17175 6e1@posting.goo gle.com...[color=darkred]
        > > > Someone posted a function to implement imagecreatefrom bmp()but it
        > > > fails in imagecreatedfro mgd() in my test. Can the author check to see
        > > > if theres a typo? Its very useful if it will work!
        > > >
        > > > Brian
        > > >
        > > >
        > > > "Erwin Bon" <ernie@verkerk. nl> wrote in message[/color]
        > > news:<3fc9d260$ 0$4671$1b62eedf @news.euronet.n l>...[color=darkred]
        > > > > Hello,
        > > > >
        > > > > I need to be able to read windows bitmap files (*.bmp) into the[/color]
        > > GD-functions[color=darkred]
        > > > > in order to create a jpg-file from it. Is such a thing possible. I[/color][/color][/color]
        seem[color=blue][color=green]
        > > te[color=darkred]
        > > > > be unable to find the required functions.
        > > > >
        > > > > Hope you can help me.
        > > > >
        > > > > Regards, Erwin
        > > >
        > > > The GD library doesn't support .BMP files for some reason. I've
        > > > written a function that converts .BMP files to .GD, a very simple
        > > > format that GD uses.
        > > >
        > > > The following code implements imagecreatefrom bmp(). It first convert
        > > > the BMP to GD, saving it to a temporary location, then open the image
        > > > using imagecreatefrom gd().
        > > >
        > > >
        > > > <?
        > > >
        > > > function ConvertBMP2GD($ src, $dest = false) {
        > > > if(!($src_f = fopen($src, "rb"))) {
        > > > return false;
        > > > }
        > > > if(!($dest_f = fopen($dest, "wb"))) {
        > > > return false;
        > > > }
        > > > $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f,
        > > > 14));
        > > > $info =[/color]
        > >[/color][/color]
        unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyr[color=blue][color=green]
        > > es/Vncolor/Vimportant",[color=darkred]
        > > > fread($src_f, 40));
        > > >
        > > > extract($info);
        > > > extract($header );
        > > >
        > > > if($type != 0x4D42) { // signature "BM"
        > > > return false;
        > > > }
        > > >
        > > > $palette_size = $offset - 54;
        > > > $ncolor = $palette_size / 4;
        > > > $gd_header = "";
        > > > // true-color vs. palette
        > > > $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
        > > > $gd_header .= pack("n2", $width, $height);
        > > > $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
        > > > if($palette_siz e) {
        > > > $gd_header .= pack("n", $ncolor);
        > > > }
        > > > // no transparency
        > > > $gd_header .= "\xFF\xFF\xFF\x FF";
        > > >
        > > > fwrite($dest_f, $gd_header);
        > > >
        > > > if($palette_siz e) {
        > > > $palette = fread($src_f, $palette_size);
        > > > $gd_palette = "";
        > > > $j = 0;
        > > > while($j < $palette_size) {
        > > > $b = $palette{$j++};
        > > > $g = $palette{$j++};
        > > > $r = $palette{$j++};
        > > > $a = $palette{$j++};
        > > > $gd_palette .= "$r$g$b$a";
        > > > }
        > > > $gd_palette .= str_repeat("\x0 0\x00\x00\x00", 256 - $ncolor);
        > > > fwrite($dest_f, $gd_palette);
        > > > }
        > > >
        > > > $scan_line_size = (($bits * $width) + 7) >> 3;
        > > > $scan_line_alig n = ($scan_line_siz e & 0x03) ? 4 - ($scan_line_siz e &
        > > > 0x03) : 0;
        > > >
        > > > for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
        > > > // BMP stores scan lines starting from bottom
        > > > fseek($src_f, $offset + (($scan_line_si ze + $scan_line_alig n) *
        > > > $l));
        > > > $scan_line = fread($src_f, $scan_line_size );
        > > > if($bits == 24) {
        > > > $gd_scan_line = "";
        > > > $j = 0;
        > > > while($j < $scan_line_size ) {
        > > > $b = $scan_line{$j++ };
        > > > $g = $scan_line{$j++ };
        > > > $r = $scan_line{$j++ };
        > > > $gd_scan_line .= "\x00$r$g$b ";
        > > > }
        > > > }
        > > > else if($bits == 8) {
        > > > $gd_scan_line = $scan_line;
        > > > }
        > > > else if($bits == 4) {
        > > > $gd_scan_line = "";
        > > > $j = 0;
        > > > while($j < $scan_line_size ) {
        > > > $byte = ord($scan_line{ $j++});
        > > > $p1 = chr($byte >> 4);
        > > > $p2 = chr($byte & 0x0F);
        > > > $gd_scan_line .= "$p1$p2";
        > > > } $gd_scan_line = substr($gd_scan _line, 0, $width);
        > > > }
        > > > else if($bits == 1) {
        > > > $gd_scan_line = "";
        > > > $j = 0;
        > > > while($j < $scan_line_size ) {
        > > > $byte = ord($scan_line{ $j++});
        > > > $p1 = chr((int) (($byte & 0x80) != 0));
        > > > $p2 = chr((int) (($byte & 0x40) != 0));
        > > > $p3 = chr((int) (($byte & 0x20) != 0));
        > > > $p4 = chr((int) (($byte & 0x10) != 0));
        > > > $p5 = chr((int) (($byte & 0x08) != 0));
        > > > $p6 = chr((int) (($byte & 0x04) != 0));
        > > > $p7 = chr((int) (($byte & 0x02) != 0));
        > > > $p8 = chr((int) (($byte & 0x01) != 0));
        > > > $gd_scan_line .= "$p1$p2$p3$p4$p 5$p6$p7$p8";
        > > > } $gd_scan_line = substr($gd_scan _line, 0, $width);
        > > > }
        > > >
        > > > fwrite($dest_f, $gd_scan_line);
        > > > }
        > > > fclose($src_f);
        > > > fclose($dest_f) ;
        > > > return true;
        > > > }
        > > >
        > > > function imagecreatefrom bmp($filename) {
        > > > $tmp_name = tempnam("/tmp", "GD");
        > > > if(ConvertBMP2G D($filename, $tmp_name)) {
        > > > $img = imagecreatefrom gd($tmp_name);
        > > > unlink($tmp_nam e);
        > > > return $img;
        > > > } return false;
        > > > }
        > > >
        > > > $img = imagecreatefrom bmp("test24bit. bmp");
        > > > imagejpeg($img, "test.jpg") ;
        > > >
        > > > ?>
        > > > <img src="test.jpg">[/color][/color][/color]


        Comment

        • Brian

          #5
          Re: imagecreatefrom bmp()

          Just to follow up, after upgrading to 4.3.4 it started working. So the
          code apparently doesnt work on 4.3.0.

          thanks,

          Brian

          "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<j6WdnUorZ Jg0oqjdRVn-uw@comcast.com> ...[color=blue]
          > Email me the bmp file and I'll take a look. It worked fine with my test
          > image (256 colors).
          >
          > Uzytkownik "Brian" <brianc8876@aol .com> napisal w wiadomosci
          > news:d080c1cd.0 402181618.32719 a51@posting.goo gle.com...[color=green]
          > > I am running 4.3.0 with the extensions enabled. It fails on
          > > imagecreatefrom gd(), so its not creating a valid gd. I cut and pasted
          > > it directly and ran it as is.
          > >
          > > Brian
          > >
          > >
          > > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message[/color]
          > news:<AvqdnX8dI OfINa_dRVn-gg@comcast.com> ...[color=green][color=darkred]
          > > > Make sure you have GD2 extension enabled.
          > > >
          > > > Uzytkownik "Brian" <brianc8876@aol .com> napisal w wiadomosci
          > > > news:d080c1cd.0 402170619.17175 6e1@posting.goo gle.com...
          > > > > Someone posted a function to implement imagecreatefrom bmp()but it
          > > > > fails in imagecreatedfro mgd() in my test. Can the author check to see
          > > > > if theres a typo? Its very useful if it will work!
          > > > >
          > > > > Brian
          > > > >
          > > > >
          > > > > "Erwin Bon" <ernie@verkerk. nl> wrote in message[/color][/color]
          > news:<3fc9d260$ 0$4671$1b62eedf @news.euronet.n l>...[color=green][color=darkred]
          > > > > > Hello,
          > > > > >
          > > > > > I need to be able to read windows bitmap files (*.bmp) into the[/color][/color]
          > GD-functions[color=green][color=darkred]
          > > > > > in order to create a jpg-file from it. Is such a thing possible. I[/color][/color]
          > seem
          > te[color=green][color=darkred]
          > > > > > be unable to find the required functions.
          > > > > >
          > > > > > Hope you can help me.
          > > > > >
          > > > > > Regards, Erwin
          > > > >
          > > > > The GD library doesn't support .BMP files for some reason. I've
          > > > > written a function that converts .BMP files to .GD, a very simple
          > > > > format that GD uses.
          > > > >
          > > > > The following code implements imagecreatefrom bmp(). It first convert
          > > > > the BMP to GD, saving it to a temporary location, then open the image
          > > > > using imagecreatefrom gd().
          > > > >
          > > > >
          > > > > <?
          > > > >
          > > > > function ConvertBMP2GD($ src, $dest = false) {
          > > > > if(!($src_f = fopen($src, "rb"))) {
          > > > > return false;
          > > > > }
          > > > > if(!($dest_f = fopen($dest, "wb"))) {
          > > > > return false;
          > > > > }
          > > > > $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f,
          > > > > 14));
          > > > > $info =
          > > >[/color][/color]
          > unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyr[color=green][color=darkred]
          > > > es/Vncolor/Vimportant",
          > > > > fread($src_f, 40));
          > > > >
          > > > > extract($info);
          > > > > extract($header );
          > > > >
          > > > > if($type != 0x4D42) { // signature "BM"
          > > > > return false;
          > > > > }
          > > > >
          > > > > $palette_size = $offset - 54;
          > > > > $ncolor = $palette_size / 4;
          > > > > $gd_header = "";
          > > > > // true-color vs. palette
          > > > > $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
          > > > > $gd_header .= pack("n2", $width, $height);
          > > > > $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
          > > > > if($palette_siz e) {
          > > > > $gd_header .= pack("n", $ncolor);
          > > > > }
          > > > > // no transparency
          > > > > $gd_header .= "\xFF\xFF\xFF\x FF";
          > > > >
          > > > > fwrite($dest_f, $gd_header);
          > > > >
          > > > > if($palette_siz e) {
          > > > > $palette = fread($src_f, $palette_size);
          > > > > $gd_palette = "";
          > > > > $j = 0;
          > > > > while($j < $palette_size) {
          > > > > $b = $palette{$j++};
          > > > > $g = $palette{$j++};
          > > > > $r = $palette{$j++};
          > > > > $a = $palette{$j++};
          > > > > $gd_palette .= "$r$g$b$a";
          > > > > }
          > > > > $gd_palette .= str_repeat("\x0 0\x00\x00\x00", 256 - $ncolor);
          > > > > fwrite($dest_f, $gd_palette);
          > > > > }
          > > > >
          > > > > $scan_line_size = (($bits * $width) + 7) >> 3;
          > > > > $scan_line_alig n = ($scan_line_siz e & 0x03) ? 4 - ($scan_line_siz e &
          > > > > 0x03) : 0;
          > > > >
          > > > > for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
          > > > > // BMP stores scan lines starting from bottom
          > > > > fseek($src_f, $offset + (($scan_line_si ze + $scan_line_alig n) *
          > > > > $l));
          > > > > $scan_line = fread($src_f, $scan_line_size );
          > > > > if($bits == 24) {
          > > > > $gd_scan_line = "";
          > > > > $j = 0;
          > > > > while($j < $scan_line_size ) {
          > > > > $b = $scan_line{$j++ };
          > > > > $g = $scan_line{$j++ };
          > > > > $r = $scan_line{$j++ };
          > > > > $gd_scan_line .= "\x00$r$g$b ";
          > > > > }
          > > > > }
          > > > > else if($bits == 8) {
          > > > > $gd_scan_line = $scan_line;
          > > > > }
          > > > > else if($bits == 4) {
          > > > > $gd_scan_line = "";
          > > > > $j = 0;
          > > > > while($j < $scan_line_size ) {
          > > > > $byte = ord($scan_line{ $j++});
          > > > > $p1 = chr($byte >> 4);
          > > > > $p2 = chr($byte & 0x0F);
          > > > > $gd_scan_line .= "$p1$p2";
          > > > > } $gd_scan_line = substr($gd_scan _line, 0, $width);
          > > > > }
          > > > > else if($bits == 1) {
          > > > > $gd_scan_line = "";
          > > > > $j = 0;
          > > > > while($j < $scan_line_size ) {
          > > > > $byte = ord($scan_line{ $j++});
          > > > > $p1 = chr((int) (($byte & 0x80) != 0));
          > > > > $p2 = chr((int) (($byte & 0x40) != 0));
          > > > > $p3 = chr((int) (($byte & 0x20) != 0));
          > > > > $p4 = chr((int) (($byte & 0x10) != 0));
          > > > > $p5 = chr((int) (($byte & 0x08) != 0));
          > > > > $p6 = chr((int) (($byte & 0x04) != 0));
          > > > > $p7 = chr((int) (($byte & 0x02) != 0));
          > > > > $p8 = chr((int) (($byte & 0x01) != 0));
          > > > > $gd_scan_line .= "$p1$p2$p3$p4$p 5$p6$p7$p8";
          > > > > } $gd_scan_line = substr($gd_scan _line, 0, $width);
          > > > > }
          > > > >
          > > > > fwrite($dest_f, $gd_scan_line);
          > > > > }
          > > > > fclose($src_f);
          > > > > fclose($dest_f) ;
          > > > > return true;
          > > > > }
          > > > >
          > > > > function imagecreatefrom bmp($filename) {
          > > > > $tmp_name = tempnam("/tmp", "GD");
          > > > > if(ConvertBMP2G D($filename, $tmp_name)) {
          > > > > $img = imagecreatefrom gd($tmp_name);
          > > > > unlink($tmp_nam e);
          > > > > return $img;
          > > > > } return false;
          > > > > }
          > > > >
          > > > > $img = imagecreatefrom bmp("test24bit. bmp");
          > > > > imagejpeg($img, "test.jpg") ;
          > > > >
          > > > > ?>
          > > > > <img src="test.jpg">[/color][/color][/color]

          Comment

          Working...