无法从 MIME 类型为“image/x-ms-bmp”的 BMP 创建 GD 图像资源;在 PHP 中

发布于 2024-08-18 07:44:11 字数 509 浏览 5 评论 0 原文

我正在尝试从 BMP 图像创建 GD 图像资源,但是我没有运气。

所讨论的 BMP 图像是使用 Photoshop 创建并保存的。我也尝试了在网上找到的几个 BMP,它们给出了相同的结果。

getimagesize() 告诉我 BMP 图像的图像类型为 IMAGETYPE_BMP (6),MIME 类型为“image/x-ms-bmp”。

我尝试通过 imagecreatefromwbmp() 和 imagecreatefromxbm() 运行图像,但都无法识别它。我还尝试通过 imagecreatefromstring() 运行它,但错误提示“数据不是可识别的格式”。

我在 Windows 机器上运行 XAMPP,运行 PHP 5.3.1 和 GD 2.0.34,并启用 WBMP 和 XBM 支持。我还在运行 PHP 5.2.6 和 GD 2.0.34 且启用了 WBMP 和 XBM 支持的 Linux 服务器上尝试过此操作,结果相同。

有什么想法可以如何从此 BMP 创建 GD 图像资源吗?实际上可能吗?

I'm trying to create a GD image resource from a BMP image, however I'm having no luck.

The BMP image in question was created and saved with Photoshop. I've tried a couple of BMPs I found on the web too, and they gave the same results.

getimagesize() tells me the BMP image has an image type of IMAGETYPE_BMP (6) and a MIME type of 'image/x-ms-bmp'.

I've tried running the image through imagecreatefromwbmp() and imagecreatefromxbm(), but neither recognise it. I've also tried running it through imagecreatefromstring(), but that errored saying 'Data is not in a recognized format'.

I'm running XAMPP on a Windows machine with PHP 5.3.1 and GD 2.0.34 with WBMP and XBM support enabled. I've also tried this on a Linux server running PHP 5.2.6 and GD 2.0.34 with WBMP and XBM support enabled, same result.

Any ideas how I can create a GD image resource from this BMP? Is it actually possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一身软味 2024-08-25 07:44:11

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

我会通过重新打开 Photoshop 并重新保存为 PNG 或 JPG 来解决此问题。假设您已安装/编译了具有适当支持的 PHP,您将能够很好地使用其中一种或两种图像格式。

As far as I know, it doesn't support BMP images. The imagecreatefromwbmp() method is for dealing with wireless bitmaps (WBMP) files, not the normal BMP you have there. The imagecreatefromxbm() is for dealing with the XBM format (again, different from BMP).

I would work around this by re-opening Photoshop and re-saving as PNG or JPG. Assuming you have PHP installed/compiled with the appropriate support, you'll be able to work with one or both of those image formats fine.

情痴 2024-08-25 07:44:11

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

该项目名为 PHP Image Magician

There is a new opensource project on Github that allows reading and saving of BMP files (and other file formats) in PHP. It's pretty easy to use.

The project is called PHP Image Magician.

無心 2024-08-25 07:44:11

您正在寻找的解决方案在这里:
http://tr.php.net/imagecreate

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

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

The solution you are looking for is here:
http://tr.php.net/imagecreate

Scroll below to the comments to find the function named "ImageCreateFromBMP".
It will help you create images from the bmp images.

Once you create the image you can use the imagejpeg() function to save the image in jpeg format.

彡翼 2024-08-25 07:44:11

PHP 7.2 在 GD 库中引入了对 BMP 的支持: imagebmp, imagecreatefrombmp

PHP 7.2 introduced support for BMP in GD library: imagebmp, imagecreatefrombmp.

高速公鹿 2024-08-25 07:44:11

我好像记得很久以前就知道GD不支持BMP格式。

这是我刚刚找到的链接。

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

此时间线< Delicious.com 的 /a> 表明这可能是 2005 年。

I seem to recall learning a long time ago that GD doesn't support BMP format.

Here's a link I just found.

Although there was some confusion about WBMP files, it was a long time ago.

This timeline from Delicious.com indicates it was probably 2005.

甜中书 2024-08-25 07:44:11

使用功能:

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

use function :

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

source
http://php.net/manual/ru/function.imagecreatefromwbmp.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文