Professional Web Applications Themes

is there BMP support in GD? - PHP Development

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

  1. #1

    Default is there BMP support in GD?

    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


    Erwin Bon Guest

  2. #2

    Default Re: is there BMP support in GD?

    All I can see in the php manual is support for .wbmp files.

    Hamilton

    "Erwin Bon" <ernieverkerk.nl> wrote in message
    news:3fc9d260$0$4671$1b62eedfnews.euronet.nl...
    > 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
    >
    >

    Spidah Guest

  3. #3

    Default Re: is there BMP support in GD?

    "Erwin Bon" <ernieverkerk.nl> wrote in message news:<3fc9d260$0$4671$1b62eedfnews.euronet.nl>...
    > 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
    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 imagecreatefrombmp(). It first convert
    the BMP to GD, saving it to a temporary location, then open the image
    using imagecreatefromgd().


    <?

    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_size) {
    $gd_header .= pack("n", $ncolor);
    }
    // no transparency
    $gd_header .= "\xFF\xFF\xFF\xFF";

    fwrite($dest_f, $gd_header);

    if($palette_size) {
    $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("\x00\x00\x00\x00", 256 - $ncolor);
    fwrite($dest_f, $gd_palette);
    }

    $scan_line_size = (($bits * $width) + 7) >> 3;
    $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size &
    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_size + $scan_line_align) *
    $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$p5$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 imagecreatefrombmp($filename) {
    $tmp_name = tempnam("/tmp", "GD");
    if(ConvertBMP2GD($filename, $tmp_name)) {
    $img = imagecreatefromgd($tmp_name);
    unlink($tmp_name);
    return $img;
    }
    return false;
    }

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

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

Similar Threads

  1. Does FMS 2 support UDP?
    By Zen Seven in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: December 9th, 05:21 PM
  2. UTF support in SWF
    By lordali in forum Macromedia Flex General Discussion
    Replies: 2
    Last Post: June 24th, 04:06 AM
  3. CSS support in DW
    By rg0001 in forum Macromedia Dynamic HTML
    Replies: 1
    Last Post: February 15th, 11:04 PM
  4. UTF-8 Support
    By David Simon in forum Macromedia Director Basics
    Replies: 0
    Last Post: December 11th, 04:38 PM
  5. XML support in PHP 5
    By Juliano in forum PHP Development
    Replies: 0
    Last Post: July 25th, 12:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139