BMP & GD PHP Support 16,447 views

The missing imagecreatefrombmp that php doesn't include.

  1<?php  2function convertBMP2GD($src, $dest = false) {  3  if (!($src_f = fopen($src, "rb"))) {  4    return false;  5  }  6  7  if (!($dest_f = fopen($dest, "wb"))) {  8    return false;  9  } 10 11  $header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_f, 14)); 12  $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", 13  fread($src_f, 40)); 14 15  extract($info); 16  extract($header); 17 18  if ($type != 0x4D42) { // signature "BM" 19    return false; 20  } 21 22  $palette_size = $offset - 54; 23  $ncolor = $palette_size / 4; 24  $gd_header = ""; 25  // true-color vs. palette 26  $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF"; 27  $gd_header .= pack("n2", $width, $height); 28  $gd_header .= ($palette_size == 0) ? "\x01" : "\x00"; 29 30  if ($palette_size) { 31    $gd_header .= pack("n", $ncolor); 32  } 33 34  // no transparency 35  $gd_header .= "\xFF\xFF\xFF\xFF"; 36  fwrite($dest_f, $gd_header); 37 38  if ($palette_size) { 39    $palette = fread($src_f, $palette_size); 40    $gd_palette = ""; 41    $j = 0; 42 43    while ($j < $palette_size) { 44      $b = $palette{$j++}; 45      $g = $palette{$j++}; 46      $r = $palette{$j++}; 47      $a = $palette{$j++}; 48      $gd_palette .= "$r$g$b$a"; 49    } 50 51    $gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor); 52    fwrite($dest_f, $gd_palette); 53  } 54 55  $scan_line_size = (($bits * $width) + 7) >> 3; 56  $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0; 57 58  for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) { 59    // BMP stores scan lines starting from bottom 60    fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l)); 61    $scan_line = fread($src_f, $scan_line_size); 62    if ($bits == 24) { 63      $gd_scan_line = ""; 64      $j = 0; 65      while ($j < $scan_line_size) { 66        $b = $scan_line{$j++}; 67        $g = $scan_line{$j++}; 68        $r = $scan_line{$j++}; 69        $gd_scan_line .= "\x00$r$g$b"; 70      } 71    } elseif ($bits == 8) { 72      $gd_scan_line = $scan_line; 73    } elseif ($bits == 4) { 74      $gd_scan_line = ""; 75      $j = 0; 76      while ($j < $scan_line_size) { 77        $byte = ord($scan_line{$j++}); 78        $p1 = chr($byte >> 4); 79        $p2 = chr($byte & 0x0F); 80        $gd_scan_line .= "$p1$p2"; 81      } 82 83      $gd_scan_line = substr($gd_scan_line, 0, $width); 84    } elseif ($bits == 1) { 85      $gd_scan_line = ""; 86      $j = 0; 87      while ($j < $scan_line_size) { 88        $byte = ord($scan_line{$j++}); 89        $p1 = chr((int) (($byte & 0x80) != 0)); 90        $p2 = chr((int) (($byte & 0x40) != 0)); 91        $p3 = chr((int) (($byte & 0x20) != 0)); 92        $p4 = chr((int) (($byte & 0x10) != 0)); 93        $p5 = chr((int) (($byte & 0x08) != 0)); 94        $p6 = chr((int) (($byte & 0x04) != 0)); 95        $p7 = chr((int) (($byte & 0x02) != 0)); 96        $p8 = chr((int) (($byte & 0x01) != 0)); 97        $gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8"; 98      } 99100      $gd_scan_line = substr($gd_scan_line, 0, $width);101    }102103    fwrite($dest_f, $gd_scan_line);104  }105106  fclose($src_f);107  fclose($dest_f);108109  return true;110}111112function imagecreatefrombmp($filename) {113  $tmp_name = tempnam("/tmp", "GD");114  if (convertBMP2GD($filename, $tmp_name)) {115    $img = imagecreatefromgd($tmp_name);116    unlink($tmp_name);117    return $img;118  }119120  return false;121}