How to create a thumbnail of .BMP file?
Asked Answered
F

3

6

I use imagecreatefromjpeg, imagecreatefromgif, and imagecreatefrompng functions to create thumbnails of image/jpeg, image/gif, and image/png mimes.

I would like also to create thumbnails of .BMP files.

I checked one file and found out that its mime is image/x-ms-bmp.

However, I cannot find an appropriate imagecreatefrom... function.

Please suggest.

Fard answered 29/10, 2010 at 3:53 Comment(1)
Yes, take a look at this PHP manpage - there's a lot of solutions offered: us.php.net/manual/en/function.imagecreatefromwbmp.phpLitmus
C
11

PHP does not have built in image functions for BMP.

There have been a few attempts to create functions to do this.

You can find a robust and well documented version in this comment in the PHP documentation: http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214

Here is the function from that comment without the excellent documentation which makes much longer but much more readable:

public 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;
}
Confidant answered 29/10, 2010 at 4:9 Comment(1)
Doesn't work with x-ms-bmp, Notice: Uninitialized string offset making distorted imagesSpiritualist
S
2

how about something like this guy describes:

http://www.php.net/manual/en/function.imagecreate.php#53879

Schurman answered 29/10, 2010 at 3:56 Comment(0)
T
1

There is an opensource project, PHP Image Magician, that allows you to read and write BMP files. See here: https://mcmap.net/q/1770135/-convert-bmp-to-png-with-php

Totalizator answered 19/7, 2012 at 4:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.