2013-05-15 30 views
0

我想从php的一半大小的BMP图像。 PHO gd没有imagecreatefrombmp,所以我必须包含单独的功能。但我的代码似乎没有工作。它适用于jpeg。这里是我的代码它应该显示一半的图像的bmp图像test.bmpimagecreatefrombmp不能在php上工作

<?php 


if (!function_exists("imagecreatefrombmp")) { 
    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; 
    } 
} 
// File and new size 
$filename = 'wheat.bmp'; 
$percent = 0.5; 

// Content type 
header('Content-Type: image/bmp'); 

// Get new sizes 
list($width, $height) = getimagesize($filename); 
$newwidth = $width * $percent; 
$newheight = $height * $percent; 

// Load 
$thumb = imagecreatetruecolor($newwidth, $newheight); 
$source = imagecreatefrombmp($filename); 

// Resize 
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

// Output 
imagejpeg($thumb); 
?> 
+1

所以实际上它怎么没有工作?什么工作,什么不工作? – eis

+0

它只是一个银行页面,不显示半角大小的图片 – user2232459

回答

0

尝试imagecreatefromwbmp()。这是将图像转换为bmp文件的正确功能。

+0

该文档指出这是一个与Windows位图图像格式不同的无线位图规范。 – Gujamin

0

显然imagecreatefrombmp()现在包含在PHP 7中。