代码之家  ›  专栏  ›  技术社区  ›  Stephen Melrose

无法在PHP中使用mime类型“image/x-ms-bmp”从BMP创建gd图像资源

  •  3
  • Stephen Melrose  · 技术社区  · 15 年前

    我试图从BMP图像创建一个GD图像资源,但是我运气不好。

    有关的BMP图像是用Photoshop创建和保存的。我也尝试了一些在网上找到的BMP,它们给出了相同的结果。

    getImageSize()告诉我BMP图像的图像类型为imageType_BMP(6)和mime类型为“image/x-ms-bmp”。

    我试过通过ImageCreateFromWbmp()和ImageCreateFromXbm()运行图像,但都不能识别它。我还尝试通过imageCreateFromString()运行它,但错误地说“数据不是可识别的格式”。

    我在启用了WBMP和XBM支持的Windows机器上运行XAMPP,该机器支持php 5.3.1和gd 2.0.34。我也在运行php 5.2.6和gd 2.0.34的Linux服务器上尝试过,启用了wbmp和xbm支持,结果是一样的。

    我能从这个BMP创建一个GD图像资源吗?真的有可能吗?

    6 回复  |  直到 7 年前
        1
  •  3
  •   Mark Embling    15 年前

    据我所知,它不支持BMP图像。这个 imagecreatefromwbmp() 方法是处理无线位图(wbmp)文件,而不是您在那里的普通BMP。这个 imagecreatefromxbm() 用于处理XBM格式(同样,与BMP不同)。

    我会通过重新打开photoshop并重新保存为png或jpg来解决这个问题。假设您安装/编译了具有适当支持的PHP,那么就可以很好地使用其中一种或两种图像格式。

        2
  •  2
  •   Jarrod    12 年前

    GitHub上有一个新的开源项目,它允许在PHP中读取和保存BMP文件(以及其他文件格式)。它很容易使用。

    这个项目叫做 PHP Image Magician .

        3
  •  1
  •   Haluk    15 年前

    您要寻找的解决方案如下: http://tr.php.net/imagecreate

    滚动到下面的注释以查找名为“”的函数。 从BMP创建图像 “。 它将帮助您从BMP图像创建图像。

    创建图像后,可以使用 图像jpeg() 函数以jpeg格式保存图像。

        4
  •  0
  •   pavium    15 年前

    我似乎记得很久以前学过GD不支持BMP格式。

    here's a link i just found.

    虽然对wbmp文件有些困惑,但那是很久以前的事了。

    此时间线 from del icio.com indicates it was probable 2005.

    我刚找到一个链接。

    虽然WBMP文件有些混乱,但那是很久以前的事了。

    此时间线 从delicious.com上看,可能是2005年。

        5
  •  0
  •   igor    10 年前

    使用功能:

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

    来源 http://php.net/manual/ru/function.imagecreatefromwbmp.php

        6
  •  0
  •   Robo Robok    7 年前

    php 7.2在gd库中引入了对BMP的支持: imagebmp , imagecreatefrombmp .