Unable to create GD image resource from BMP with MIME type 'image/x-ms-bmp' in PHP

筅森魡賤 提交于 2019-12-10 10:09:33

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/2100875/unable-to-create-gd-image-resource-from-bmp-with-mime-type-image-x-ms-bmp-in-p

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!