imagecreatefrombmp not working on php

php imagecreate
convert bitmap to image in php
imagejpeg php not working
imagecreatefromstring not working

i am trying to half the size of bmp image from php. PHO gd doesnt have a imagecreatefrombmp so i have to include seperate function. But my code doesnt seems to be working. It works well with jpeg. Here is my code it should display half the image of bmp image test.bmp

<?php


if (!function_exists("imagecreatefrombmp")) {
    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;
    }
}
// File and new size
$filename = 'wheat.bmp';
$percent = 0.5;

// Content type
header('Content-Type: image/bmp');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefrombmp($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

Try imagecreatefromwbmp(). This is the right function to convert images as bmp file.

image - imagecreatefrombmp not working on php, Try imagecreatefromwbmp() . This is the right function to convert images as bmp file. Tip. A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

Apparently imagecreatefrombmp() is now included in PHP 7.

imagecreatefrombmp - Manual, imagecreatefrombmp. (PHP 7 >= 7.2.0). imagecreatefrombmp — Create a new image from file or URL. Description  imagecreatefromwbmp() returns an image identifier representing the image obtained from the given filename. Note: WBMP images are Wireless Bitmaps, not Windows Bitmaps. The latter can be loaded with imagecreatefrombmp().

In my case, it was also throwing the same error as below.

Call to undefined function imagecreatefrombmp()

Even When I was on php version PHP Version 7.1.28

So, I defined this function as

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

As an argument, I passed relative path of the file. i.e. (upload/images/example.bmp)

Php imagecreatefrombmp · GitHub, Php imagecreatefrombmp. imagecreatefrombmp.php. <?php. /**. * @param string $fileName function imagecreatefrombmp($fileName) {. Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Add support for bitmap (BMP) images · Issue #171 · claviska , Creates function imagecreatefrombmp, since PHP doesn't have one imagecreatefromwbmp is not intended to work with Bitmaps (despite the  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

imagecreatefrombmp() won't work on this bitmap, I've tried imagecreatefrombmp() and imagecreatefromwbmp() but it won't process this How can I get my php to convert this image to a jpg? Just to follow up, after upgrading to 4.3.4 it started working. So the code apparently doesnt work on 4.3.0. PHP Development; imagecreatefrombmp()

Problem with imagecreatefrombmp, Hi there, I'm having problem with bmp extension. There is no imagecreatefrombmp() function in the built-in PHP functions or extensions. imagecreatefrombmp(). PHP Forums on Bytes. I am running 4.3.0 with the extensions enabled. It fails on imagecreatefromgd(), so its not creating a valid gd.

Comments
  • so how actually it doesn't seem to be working? what works and what doesn't?
  • it just a bank page the half size picture does not get displayed
  • The documentation indicated this is a wireless bitmap specification that is different from the windows bitmap image format.