note 83119 deleted from function.imagecreatefromwbmp by cmb

From: Date: Sun, 19 May 2019 16:01:07 +0000
Subject: note 83119 deleted from function.imagecreatefromwbmp by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211063@lists.php.net to get a copy of this message
Note Submitter: AeroX @ aerox-studios ---- .bmp is just hex encoded RGB values. All you need to do is open in binary mode and seperate the header from the body. Decode the width and height from the header. Then create the image pixel by pixel from the RGB values in the body. function imagecreatefrombmp( $filename ) { $file = fopen( $filename, "rb" ); $read = fread( $file, 10 ); while( !feof( $file ) && $read != "" ) { $read .= fread( $file, 1024 ); } $temp = unpack( "H*", $read ); $hex = $temp[1]; $header = substr( $hex, 0, 104 ); $body = str_split( substr( $hex, 108 ), 6 ); if( substr( $header, 0, 4 ) == "424d" ) { $header = substr( $header, 4 ); // Remove some stuff? $header = substr( $header, 32 ); // Get the width $width = hexdec( substr( $header, 0, 2 ) ); // Remove some stuff? $header = substr( $header, 8 ); // Get the height $height = hexdec( substr( $header, 0, 2 ) ); unset( $header ); } $x = 0; $y = 1; $image = imagecreatetruecolor( $width, $height ); foreach( $body as $rgb ) { $r = hexdec( substr( $rgb, 4, 2 ) ); $g = hexdec( substr( $rgb, 2, 2 ) ); $b = hexdec( substr( $rgb, 0, 2 ) ); $color = imagecolorallocate( $image, $r, $g, $b ); imagesetpixel( $image, $x, $height-$y, $color ); $x++; if( $x >= $width ) { $x = 0; $y++; } } return $image; }

« previous php.notes (#211063) next »