Posted By


gms8994 on 12/04/09

Tagged


Statistics


Viewed 906 times
Favorited by 0 user(s)

imagecreatefrombmp


/ Published in: PHP
Save to your folder(s)

Since PHP doesn't have a builtin imagecreatefrombmp, this allows users to upload bmp files and you can handle them and create a different image type from it.


Copy this code and paste it in your HTML
  1. /**
  2.  * Creates function imagecreatefrombmp, since PHP doesn't have one
  3.  * @return resource An image identifier, similar to imagecreatefrompng
  4.  * @param string $filename Path to the BMP image
  5.  * @see imagecreatefrompng
  6.  * @author Glen Solsberry <[email protected]>
  7.  */
  8. if (!function_exists("imagecreatefrombmp")) {
  9. function imagecreatefrombmp( $filename ) {
  10. $file = fopen( $filename, "rb" );
  11. $read = fread( $file, 10 );
  12. while( !feof( $file ) && $read != "" )
  13. {
  14. $read .= fread( $file, 1024 );
  15. }
  16. $temp = unpack( "H*", $read );
  17. $hex = $temp[1];
  18. $header = substr( $hex, 0, 104 );
  19. $body = str_split( substr( $hex, 108 ), 6 );
  20. if( substr( $header, 0, 4 ) == "424d" )
  21. {
  22. $header = substr( $header, 4 );
  23. // Remove some stuff?
  24. $header = substr( $header, 32 );
  25. // Get the width
  26. $width = hexdec( substr( $header, 0, 2 ) );
  27. // Remove some stuff?
  28. $header = substr( $header, 8 );
  29. // Get the height
  30. $height = hexdec( substr( $header, 0, 2 ) );
  31. unset( $header );
  32. }
  33. $x = 0;
  34. $y = 1;
  35. $image = imagecreatetruecolor( $width, $height );
  36. foreach( $body as $rgb )
  37. {
  38. $r = hexdec( substr( $rgb, 4, 2 ) );
  39. $g = hexdec( substr( $rgb, 2, 2 ) );
  40. $b = hexdec( substr( $rgb, 0, 2 ) );
  41. $color = imagecolorallocate( $image, $r, $g, $b );
  42. imagesetpixel( $image, $x, $height-$y, $color );
  43. $x++;
  44. if( $x >= $width )
  45. {
  46. $x = 0;
  47. $y++;
  48. }
  49. }
  50. return $image;
  51. }
  52. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.