我正在尝试将 php 中的 bmp 图像大小减半。PHO gd 没有 imagecreatefrombmp 所以我必须包含单独的功能。但是我的代码似乎不起作用。它适用于 jpeg。这是我的代码,它应该显示 bmp 图像 test.bmp 的一半图像
<?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);
?>