473,426 Members | 1,448 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,426 software developers and data experts.

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
Jul 17 '05 #1
2 23415
All I can see in the php manual is support for .wbmp files.

Hamilton

"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

Jul 17 '05 #2
"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 #3

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

Similar topics

4
by: Aditya Ivaturi | last post by:
We have a CMS which is written is based on php & mysql. Recently we received a request to support multiple languages so that sites in that particular laguage can be created. I did some search on...
10
by: Bill Davidson | last post by:
Hi there, Please forgive me for posting this article on multiple groups. Being new in the newsgroups, I was not sure which group would have been appropriate for my question. Sorry. My...
5
by: shaun.mostashari | last post by:
Hello all, I am working on a trade study and one of the criterias is how easy it is to get DB2 technical support from IBM. I appreciate if you guys share your experience with me. Does DB2 have a...
5
by: NormaJean Sebastian via DBMonster.com | last post by:
Hi, I almost read all of the "DB2 vs Oracle" thread... The summary I got from that was all databases essentially do the same things, the decision of which one you use primarily depends on...
4
by: Juan T. Llibre | last post by:
ASP.NET 1.0 Support Home http://support.microsoft.com/ph/6350 ASP.NET 1.1 Support Home http://support.microsoft.com/ph/6351 ..Net Framework 1.1 Support Site...
7
by: Terry Olsen | last post by:
I paste the following code into each project to get XP theme support. 'Add this at the beginning of any program to enable Windows XP Visual Styles <System.STAThread()> Public Shared Sub Main() If...
1
by: =?Utf-8?B?QW5kZXJ3IE1pbGxlcg==?= | last post by:
Apologies for posting in this forum, I don't see a more appropriate group... I'm trying to understand the timeline for Window 2003 ending mainstream support. I have consulted the MS lifecycle...
1
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I have a web server 32 bit and SQL server 2005 64bit . Does msdtc support SQL server 2005 64bit with web server 32 bit?
8
by: Bill McCormick | last post by:
<!-- When deploying the service library project, the content of the config file must be added to the host's app.config file. System.Configuration does not support config files for libraries. --> ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.