我用imagecreatefromjpegimagecreatefromgifimagecreatefrompng函数来创建image/jpegimage/gif,和image/png默剧的缩略图。

我想还创建的.BMP文件的缩略图。

我检查一个文件,并发现其MIME是image/x-ms-bmp

然而,我无法找到一个合适的imagecreatefrom...功能。

请建议。

有帮助吗?

解决方案

PHP没有内置在图像功能BMP。

已经有一些尝试来创建函数来做到这一点。

您可以在PHP文档中此评论找到一个强大的和有据可查的版本:的 http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214

下面是从该评论没有出色的文件,这使得更长时间,但更可读的功能:

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

其他提示

有是一个开源项目,PHP图片魔术师,它允许您读取和写入BMP文件。在这里看到: https://stackoverflow.com/a/11531747/577306

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top