如何创建 BMP 文件的缩略图?

6
我使用imagecreatefromjpegimagecreatefromgifimagecreatefrompng函数来创建image/jpegimage/gifimage/png格式的缩略图。
我还想创建.BMP文件的缩略图。
我检查了一个文件并发现它的格式是image/x-ms-bmp
然而,我找不到适当的imagecreatefrom...函数。
请给予建议。

是的,请看一下这个 PHP 手册 - 提供了很多解决方案:http://us.php.net/manual/en/function.imagecreatefromwbmp.php - leepowers
3个回答

11

PHP没有内置BMP图像处理函数。

已经有一些尝试创建此类函数的方法。

在PHP文档的这个评论中,您可以找到一个强大且文档完备的版本:http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214

以下是该评论中的函数,但省略了使其更长但更易读的优秀文档:

public 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;
}

3
无法使用x-ms-bmp格式,Notice: Uninitialized string offset导致图像变形。 - Sem

2

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接