## Get the gd format data as a string my $gd = $image->gd; ## Unpack the header (we're only interested in the width & height) and strip that header from the image data my( undef, $width, $height, undef, undef ) = unpack 'nnnCV', substr( $gd, 0, 11, '' ); ## GD image data is 4 bytes (rgba); BMP image data is 3 bytes; so calulate the length the bmp image data with occupy my $len = length( $gd ) / 4 * 3; ## Pack the required information to create a .bmp header (in two parts). (see link above) my $bmp = pack 'a2 V V V l< l< l< v v V V l< l< V V', ## The .bmp file header has 5 fields (but 2 U16s do nothing so pack 1xU32 value 0 for them) ## sig file-length (data+header length), 2 unused shorts as U32, and the offset to the rgb data. 'BM', $len + 54, 0, 54, ## BITMAPINFOHEADER 40 bytes, image width & height (negative to turn it the right way up!), ## color planes (1), bits/pel(24), compression type(none:0), length of rgb data, 3 unused fields. 40, $width, -$height, 1, 24, 0, $len, 0, 0, 0; ## Convert the rgba gd data to rgb bmp data (x skips the alpha bytes) ## And tack it onto the end of the header. $bmp .= join'', unpack '(xaaa)*', $gd;