473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

imagecreatefrombmp()

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

Brian
"Erwin Bon" <er***@verkerk.nl> wrote in message news:<3f**********************@news.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">
Jul 17 '05 #1
4 23612
Make sure you have GD2 extension enabled.

Uzytkownik "Brian" <br********@aol.com> napisal w wiadomosci
news:d0**************************@posting.google.c om...
Someone posted a function to implement imagecreatefrombmp()but it
fails in imagecreatedfromgd() in my test. Can the author check to see
if theres a typo? Its very useful if it will work!

Brian
"Erwin Bon" <er***@verkerk.nl> wrote in message news:<3f**********************@news.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/Vyr
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_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">

Jul 17 '05 #2
I am running 4.3.0 with the extensions enabled. It fails on
imagecreatefromgd(), so its not creating a valid gd. I cut and pasted
it directly and ran it as is.

Brian
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<Av********************@comcast.com>...
Make sure you have GD2 extension enabled.

Uzytkownik "Brian" <br********@aol.com> napisal w wiadomosci
news:d0**************************@posting.google.c om...
Someone posted a function to implement imagecreatefrombmp()but it
fails in imagecreatedfromgd() in my test. Can the author check to see
if theres a typo? Its very useful if it will work!

Brian
"Erwin Bon" <er***@verkerk.nl> wrote in message

news:<3f**********************@news.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/Vyr
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_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">

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

Uzytkownik "Brian" <br********@aol.com> napisal w wiadomosci
news:d0**************************@posting.google.c om...
I am running 4.3.0 with the extensions enabled. It fails on
imagecreatefromgd(), so its not creating a valid gd. I cut and pasted
it directly and ran it as is.

Brian
"Chung Leong" <ch***********@hotmail.com> wrote in message

news:<Av********************@comcast.com>...
Make sure you have GD2 extension enabled.

Uzytkownik "Brian" <br********@aol.com> napisal w wiadomosci
news:d0**************************@posting.google.c om...
Someone posted a function to implement imagecreatefrombmp()but it
fails in imagecreatedfromgd() in my test. Can the author check to see
if theres a typo? Its very useful if it will work!

Brian
"Erwin Bon" <er***@verkerk.nl> wrote in message

news:<3f**********************@news.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/Vyr 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_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">

Jul 17 '05 #4
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" <ch***********@hotmail.com> wrote in message news:<j6********************@comcast.com>...
Email me the bmp file and I'll take a look. It worked fine with my test
image (256 colors).

Uzytkownik "Brian" <br********@aol.com> napisal w wiadomosci
news:d0**************************@posting.google.c om...
I am running 4.3.0 with the extensions enabled. It fails on
imagecreatefromgd(), so its not creating a valid gd. I cut and pasted
it directly and ran it as is.

Brian
"Chung Leong" <ch***********@hotmail.com> wrote in message

news:<Av********************@comcast.com>...
Make sure you have GD2 extension enabled.

Uzytkownik "Brian" <br********@aol.com> napisal w wiadomosci
news:d0**************************@posting.google.c om...
> Someone posted a function to implement imagecreatefrombmp()but it
> fails in imagecreatedfromgd() in my test. Can the author check to see
> if theres a typo? Its very useful if it will work!
>
> Brian
>
>
> "Erwin Bon" <er***@verkerk.nl> wrote in message news:<3f**********************@news.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/Vyr 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_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">

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Erwin Bon | last post by:
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...
1
by: Perttu Pulkkinen | last post by:
I'd like to support all common iamge formats in my admin system, but i don't see imagecreatefrombmp anywhere in php manual. How can that be? Bitmap is quite common still. And xbm - what is that?...
3
by: Ken | last post by:
I need to convert 32 bit Windows bitmaps to jpgs with PHP. I used the function from http://groups.google.com/groups? hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=a164f4b5.0311302128.40fb37f4%...
1
by: Chung Leong | last post by:
Unrolled the scanline conversion loop into a lamda function. Should be slightly quicker. <? function ConvertBMP2GD($src, $dest = false) { if(!($src_f = fopen($src, "rb"))) {...
1
by: Mitul | last post by:
Hi to all, I need help for convert bmp image to jpg file. So anyone can help me to convert bmp image to jpg image by php's GD library or any ways by php. Please help me as soon as possible. Or...
8
by: brainflakes.org | last post by:
Hi guys, I need to manipulate binary data (8 bit) stored in a 2 dimensional array. I've tried various methods (arrays, using a string filled with chr(0), using gd lib) and so far the fastest...
4
by: mingkin | last post by:
<?php function ConvertBMP2GD($src, $dest = false) { if(!($src_f = fopen($src, "rb"))) { return false; } if(!($dest_f = fopen($dest, "wb"))) { return false; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.