imagecreatefrombmp不适用于php
我正试图从php中复制一半大小的bmp图像。PHO gd没有imagecreatefrombmp,所以我必须包含分离函数。但是我的代码似乎不起作用。它与jpeg很好地结合。这是我的代码,它应该显示bmp image test.bmp的一半图像imagecreatefrombmp不适用于php,php,image,bmp,Php,Image,Bmp,我正试图从php中复制一半大小的bmp图像。PHO gd没有imagecreatefrombmp,所以我必须包含分离函数。但是我的代码似乎不起作用。它与jpeg很好地结合。这是我的代码,它应该显示bmp image test.bmp的一半图像 <?php if (!function_exists("imagecreatefrombmp")) { function imagecreatefrombmp( $filename ) { $file = fopen( $
<?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);
?>
试试imagecreatefromwbmp()
。这是将图像转换为bmp文件的正确函数。请尝试imagecreatefromwbmp()
。这是将图像转换为bmp文件的正确函数。显然,PHP7中包含imagecreatefrombmp()。显然,PHP7中包含imagecreatefrombmp()。在我的例子中,它也抛出了与下面相同的错误
调用未定义的函数imagecreatefrombmp()
甚至在我使用php版本7.1.28的时候
我把这个函数定义为
function imagecreatefrombmp($p_sFile)
{
$file = fopen($p_sFile,"rb");
$read = fread($file,10);
while(!feof($file)&&($read<>""))
$read .= fread($file,1024);
$temp = unpack("H*",$read);
$hex = $temp[1];
$header = substr($hex,0,108);
if (substr($header,0,4)=="424d")
{
$header_parts = str_split($header,2);
$width = hexdec($header_parts[19].$header_parts[18]);
$height = hexdec($header_parts[23].$header_parts[22]);
unset($header_parts);
}
$x = 0;
$y = 1;
$image = imagecreatetruecolor($width,$height);
$body = substr($hex,108);
$body_size = (strlen($body)/2);
$header_size = ($width*$height);
$usePadding = ($body_size>($header_size*3)+4);
for ($i=0;$i<$body_size;$i+=3)
{
if ($x>=$width)
{
if ($usePadding)
$i += $width%4;
$x = 0;
$y++;
if ($y>$height)
break;
}
$i_pos = $i*2;
$r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
$g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
$b = hexdec($body[$i_pos].$body[$i_pos+1]);
$color = imagecolorallocate($image,$r,$g,$b);
imagesetpixel($image,$x,$height-$y,$color);
$x++;
}
unset($body);
return $image;
}
函数imagecreatefrombmp($p\u sFile)
{
$file=fopen($p_sFile,“rb”);
$read=fread($file,10);
而(!feof($file)&($read“”)
$read.=fread($file,1024);
$temp=解包(“H*”,$read);
$hex=$temp[1];
$header=substr($hex,0108);
if(substr($header,0,4)=“424d”)
{
$header\u parts=str\u split($header,2);
$width=hexdec($header_parts[19]。$header_parts[18]);
$height=hexdec($header_parts[23]。$header_parts[22]);
未设置($header\u parts);
}
$x=0;
$y=1;
$image=imageCreateTureColor($width,$height);
$body=substr($hex,108);
$body_size=(strlen($body)/2);
$header_size=($width*$height);
$usePadding=($body\u size>($header\u size*3)+4);
对于($i=0;$i=$width)
{
如果($usePadding)
$i+=$width%4;
$x=0;
$y++;
如果($y>$height)
打破
}
$i_pos=$i*2;
$r=hexdec($body[$i_pos+4]。$body[$i_pos+5]);
$g=hexdec($body[$i_pos+2]。$body[$i_pos+3]);
$b=hexdec($body[$i_pos]。$body[$i_pos+1]);
$color=imagecolorallocate($image,$r,$g,$b);
imagesetpixel($image,$x,$height-$y,$color);
$x++;
}
unset($body);
返回$image;
}
作为参数,我传递了文件的相对路径。i、 e.
(upload/images/example.bmp)
在我的例子中,它也抛出了与下面相同的错误
调用未定义的函数imagecreatefrombmp()
甚至在我使用php版本7.1.28的时候
我把这个函数定义为
function imagecreatefrombmp($p_sFile)
{
$file = fopen($p_sFile,"rb");
$read = fread($file,10);
while(!feof($file)&&($read<>""))
$read .= fread($file,1024);
$temp = unpack("H*",$read);
$hex = $temp[1];
$header = substr($hex,0,108);
if (substr($header,0,4)=="424d")
{
$header_parts = str_split($header,2);
$width = hexdec($header_parts[19].$header_parts[18]);
$height = hexdec($header_parts[23].$header_parts[22]);
unset($header_parts);
}
$x = 0;
$y = 1;
$image = imagecreatetruecolor($width,$height);
$body = substr($hex,108);
$body_size = (strlen($body)/2);
$header_size = ($width*$height);
$usePadding = ($body_size>($header_size*3)+4);
for ($i=0;$i<$body_size;$i+=3)
{
if ($x>=$width)
{
if ($usePadding)
$i += $width%4;
$x = 0;
$y++;
if ($y>$height)
break;
}
$i_pos = $i*2;
$r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
$g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
$b = hexdec($body[$i_pos].$body[$i_pos+1]);
$color = imagecolorallocate($image,$r,$g,$b);
imagesetpixel($image,$x,$height-$y,$color);
$x++;
}
unset($body);
return $image;
}
函数imagecreatefrombmp($p\u sFile)
{
$file=fopen($p_sFile,“rb”);
$read=fread($file,10);
而(!feof($file)&($read“”)
$read.=fread($file,1024);
$temp=解包(“H*”,$read);
$hex=$temp[1];
$header=substr($hex,0108);
if(substr($header,0,4)=“424d”)
{
$header\u parts=str\u split($header,2);
$width=hexdec($header_parts[19]。$header_parts[18]);
$height=hexdec($header_parts[23]。$header_parts[22]);
未设置($header\u parts);
}
$x=0;
$y=1;
$image=imageCreateTureColor($width,$height);
$body=substr($hex,108);
$body_size=(strlen($body)/2);
$header_size=($width*$height);
$usePadding=($body\u size>($header\u size*3)+4);
对于($i=0;$i=$width)
{
如果($usePadding)
$i+=$width%4;
$x=0;
$y++;
如果($y>$height)
打破
}
$i_pos=$i*2;
$r=hexdec($body[$i_pos+4]。$body[$i_pos+5]);
$g=hexdec($body[$i_pos+2]。$body[$i_pos+3]);
$b=hexdec($body[$i_pos]。$body[$i_pos+1]);
$color=imagecolorallocate($image,$r,$g,$b);
imagesetpixel($image,$x,$height-$y,$color);
$x++;
}
unset($body);
返回$image;
}
作为参数,我传递了文件的相对路径。i、 e.
(upload/images/example.bmp)
,那么它实际上是如何工作的呢?什么有效,什么无效?它只是一个银行页面,一半大小的图片没有显示出来,那么它实际上是如何不起作用的呢?什么有效,什么无效?它只是一个银行页面,半幅图片无法显示文档表明这是一个不同于windows位图图像格式的无线位图规范。文档表明这是一个不同于windows位图图像格式的无线位图规范。