Cannot create GD image resource from BMP with MIME type "image / x-ms-bmp" in PHP

I'm trying to create a GD image resource from a BMP image, but I'm out of luck.

The BMP image in question was created and saved in Photoshop. I tried a couple of BMPs that I found on the Internet and they gave the same results.

getimagesize () tells me that the BMP image is IMAGETYPE_BMP (6) image type and the MIME type is 'image / x-ms-bmp'.

I tried to run the image through imagecreatefromwbmp () and imagecreatefromxbm (), but did not recognize it. I also tried to run it through imagecreatefromstring (), but it mistakenly says: "The data is not in a recognized format."

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

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

+3
source share
6 answers

As far as I know, it does not support BMP images. The method is imagecreatefromwbmp()designed to work with raster image files (WBMP), and not with the regular BMP that you have there. imagecreatefromxbm()Designed to work with the XBM format (again, different from BMP).

, Photoshop PNG JPG. , PHP / , .

+3

Github , BMP ( ) PHP. .

PHP Magician.

+2

, , : http://tr.php.net/imagecreate

, " ImageCreateFromBMP". BMP.

imagejpeg(), jpeg.

+1

, , GD BMP.

, .

WBMP, .

Delicious.com , , , 2005 .

0

:

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

0

PHP 7.2 BMP GD: imagebmp, imagecreatefrombmp.

0

Source: https://habr.com/ru/post/1729216/


All Articles