src/Image.php
public function imagebmp(&$img, $filename = false)
	{
		$wid = imagesx($img);
		$hei = imagesy($img);
		$wid_pad = str_pad('', $wid % 4, "\0");
		$size = 54 + ($wid + $wid_pad) * $hei;
		// prepare & save header
		$header = array(
			'identifier'		=> 'BM',
			'file_size'			=> $this->dword($size),
			'reserved'			=> $this->dword(0),
			'bitmap_data'		=> $this->dword(54),
			'header_size'		=> $this->dword(40),
			'width'				=> $this->dword($wid),
			'height'			=> $this->dword($hei),
			'planes'			=> $this->word(1),
			'bits_per_pixel'	=> $this->word(24),
			'compression'		=> $this->dword(0),
			'data_size'			=> $this->dword(0),
			'h_resolution'		=> $this->dword(0),
			'v_resolution'		=> $this->dword(0),
			'colors'			=> $this->dword(0),
			'important_colors'	=> $this->dword(0),
		);
	    if ($filename) {
			$fh = fopen($filename, "wb");
			foreach ($header as $h) {
				fwrite($fh, $h);
			}
			// save pixels
			for ($y=$hei-1; $y>=0; $y--) {
				for ($x=0; $x<$wid; $x++) {
					$rgb = imagecolorat($img, $x, $y);
					fwrite($fh, $this->byte3($rgb));
				}
				fwrite($fh, $wid_pad);
			}
			return fclose($fh);
		} else {
			foreach ($header as $h) {
			    echo $h;
			}
			// save pixels
			for ($y = $hei - 1; $y >= 0; $y--) {
				for ($x=0; $x<$wid; $x++) {
					$rgb = imagecolorat($img, $x, $y);
					echo $this->byte3($rgb);
				}
				echo $wid_pad;
			}
			return true;
		}
	}
public function imagebmp(&$img, $filename = false)
	{
		$wid = imagesx($img);
		$hei = imagesy($img);
		$wid_pad = str_pad('', $wid % 4, "\0");
		$size = 54 + ($wid + $wid_pad) * $hei;
		// prepare & save header
		$header = array(
			'identifier'		=> 'BM',
			'file_size'			=> $this->dword($size),
			'reserved'			=> $this->dword(0),
			'bitmap_data'		=> $this->dword(54),
			'header_size'		=> $this->dword(40),
			'width'				=> $this->dword($wid),
			'height'			=> $this->dword($hei),
			'planes'			=> $this->word(1),
			'bits_per_pixel'	=> $this->word(24),
			'compression'		=> $this->dword(0),
			'data_size'			=> $this->dword(0),
			'h_resolution'		=> $this->dword(0),
			'v_resolution'		=> $this->dword(0),
			'colors'			=> $this->dword(0),
			'important_colors'	=> $this->dword(0),
		);
	    if ($filename) {
			$fh = fopen($filename, "wb");
			foreach ($header as $h) {
				fwrite($fh, $h);
			}
			// save pixels
			for ($y=$hei-1; $y>=0; $y--) {
				for ($x=0; $x<$wid; $x++) {
					$rgb = imagecolorat($img, $x, $y);
					fwrite($fh, $this->byte3($rgb));
				}
				fwrite($fh, $wid_pad);
			}
			return fclose($fh);
		} else {
			foreach ($header as $h) {
			    echo $h;
			}
			// save pixels
			for ($y = $hei - 1; $y >= 0; $y--) {
				for ($x=0; $x<$wid; $x++) {
					$rgb = imagecolorat($img, $x, $y);
					echo $this->byte3($rgb);
				}
				echo $wid_pad;
			}
			return true;
		}
	}
public function imagebmp ( & $ img , $ filename = false ) { $ wid = imagesx ( $ img ) ; $ hei = imagesy ( $ img ) ; $ wid_pad = str_pad ( '' , $ wid % 4 , "\0" ) ; $ size = 54 + ( $ wid + $ wid_pad ) * $ hei ; // prepare & save header $ header = array ( 'identifier' => 'BM' , 'file_size' => $ this -> dword ( $ size ) , 'reserved' => $ this -> dword ( 0 ) , 'bitmap_data' => $ this -> dword ( 54 ) , 'header_size' => $ this -> dword ( 40 ) , 'width' => $ this -> dword ( $ wid ) , 'height' => $ this -> dword ( $ hei ) , 'planes' => $ this -> word ( 1 ) , 'bits_per_pixel' => $ this -> word ( 24 ) , 'compression' => $ this -> dword ( 0 ) , 'data_size' => $ this -> dword ( 0 ) , 'h_resolution' => $ this -> dword ( 0 ) , 'v_resolution' => $ this -> dword ( 0 ) , 'colors' => $ this -> dword ( 0 ) , 'important_colors' => $ this -> dword ( 0 ) , ) ; if ( $ filename ) { $ fh = fopen ( $ filename , "wb" ) ; foreach ( $ header as $ h ) { fwrite ( $ fh , $ h ) ; } // save pixels for ( $ y = $ hei - 1 ; $ y >= 0 ; $ y -- ) { for ( $ x = 0 ; $ x < $ wid ; $ x ++ ) { $ rgb = imagecolorat ( $ img , $ x , $ y ) ; fwrite ( $ fh , $ this -> byte3 ( $ rgb ) ) ; } fwrite ( $ fh , $ wid_pad ) ; } return fclose ( $ fh ) ; } else { foreach ( $ header as $ h ) { echo $ h ; } // save pixels for ( $ y = $ hei - 1 ; $ y >= 0 ; $ y -- ) { for ( $ x = 0 ; $ x < $ wid ; $ x ++ ) { $ rgb = imagecolorat ( $ img , $ x , $ y ) ; echo $ this -> byte3 ( $ rgb ) ; } echo $ wid_pad ; } return true ; } }
Save 24bit BMP file

@param resource $img      image resource
@param boolean  $filename (optional) filepath to save to.. otherwise image is echoed

@return boolean
Save 24bit BMP file