PHP 7.2.0 Release Candidate 4 Released

imagecreatefromwbmp

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

imagecreatefromwbmpCria uma nova imagem a apratir de um arquivo ou URL

Descrição

resource imagecreatefromwbmp ( string $filename )

imagecreatefromwbmp() retorna um identificador de imagem representando a imagem obtida através do nome de arquivo dado.

imagecreatefromwbmp() retorna uma string vazia em caso de falha. Também mostra uma mensagem de erro, a qual infelizmente aparece como um link quebrado no browser. Para facilitar o debug, o exemplo asseguir irá prodizir um WBMP de erro:

Exemplo #1 Exemplo de como manipular um erro durante a criação

<?php
function LoadWBMP($imgname
{
    
$im = @imagecreatefromwbmp($imgname); /* Attempt to open */
    
if (!$im) { /* See if it failed */
        
$im  imagecreatetruecolor (2020); /* Create a blank image */
        
$bgc imagecolorallocate($im255255255);
        
$tc  imagecolorallocate($im000);
        
imagefilledrectangle($im001010$bgc);
        
/* Output an errmsg */
        
imagestring($im155"Error loading $imgname"$tc);
    }
    return 
$im;
}
?>

Dica

Uma URL pode ser utilizada como um nome de arquivo se fopen wrappers estiver ativo. Veja fopen() para mais detalhes em como especificar URLs como nome de arquivo. Veja também the Protocolos e Wrappers suportados para informações sobre que capacidades cada wrapper tem, notas de uso e informações sobre variáveis predefinidas fornecidas.

Parâmetros

filename

Caminho para imagem WBMP

Valor Retornado

Retorna um resource identificador da imagem em sucesso, FALSE em erros.

Notas

add a note add a note

User Contributed Notes 4 notes

up
12
clambert at whitecrown dot net
16 years ago
WBMP images are Wireless Bitmaps, not Windows Bitmaps. WBMP is used for bandwidth constrained, black and white, limited devices such as PDAs and Cell Phones.
up
8
AeroX @ aerox-studios
9 years ago
.bmp is just hex encoded RGB values.
All you need to do is open in binary mode and seperate the header from the body.
Decode the width and height from the header.
Then create the image pixel by pixel from the RGB values in the body.

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;
}
up
7
alexander at alexauto dot nl
9 years ago
AeroX created a nice example, but it only works at specific imagesizes because bmp uses fully 32-bit lines. To prevent failures (different colors, pixel-shifting per line), the solution is to skip the padding, inserted after color-pixels on the line. His implementation did cutting the padding into the image-data, causing wrong colors, pixel-shifting per line and also an large memory-consumption.

I improved this script to the next implementation. The readability of a image is checked by a other function, so it doesn't take part in this script.
Suggestions? Please let me know.

<?php
   
public function imagecreatefrombmp($p_sFile)
    {
       
//    Load the image into a string
       
$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);
       
       
//    Process the header
        //    Structure: http://www.fastgraph.com/help/bmp_header_format.html
       
if (substr($header,0,4)=="424d")
        {
           
//    Cut it in parts of 2 bytes
           
$header_parts    =    str_split($header,2);
           
           
//    Get the width        4 bytes
           
$width            =    hexdec($header_parts[19].$header_parts[18]);
           
           
//    Get the height        4 bytes
           
$height            =    hexdec($header_parts[23].$header_parts[22]);
           
           
//    Unset the header params
           
unset($header_parts);
        }
       
       
//    Define starting X and Y
       
$x                =    0;
       
$y                =    1;
       
       
//    Create newimage
       
$image            =    imagecreatetruecolor($width,$height);
       
       
//    Grab the body from the image
       
$body            =    substr($hex,108);

       
//    Calculate if padding aader pend-/>&nb-.(        //    SDividd by atw top keep verlvie.
nbsp;       //    S1bytes =2 bHEX-char
        &/span>$body&_izesnbsp;       =    <sutrln($pody&/span>);//span>2);
        &/span>$header_pizesnbsp;   &/span>=    <swidth,*/span>$height);
&
       
//    CUspend-/>&nb-adding ?Only Ewen c/span6
        //span>$husePdding nbsp;       =    <sbody&_izes/span>=gt;<$header_pizes/span>=*/span>$])+/span>4)=
       
       
//    Gsing rafore-loopwith Xndex.-clculateon isstalidof 2tr_split nbsp;       //    Salculate ihe next iDWORD-posieon issthe body
       
ior b$hi/span>=&/span>0; /span>$hi/span>=lt;&/span>sbody&_izes/span>= /span>$hi/span>=+&/span>0])
        {
           
//    Culculate i>&nb-nd-ng and Gadding
           
uf (s$,gt;= /span>swidth,
            <
            //    CI padding a/span6, ignor image-dadding
                        uf (s$usePdding /span>,
            $hinbsp;   u+    $tidth,%/span>4)
            nbsp;  
        //    CRsetp horizontal posieon
        $x    &/span>=    0;
                //    CRaisethe height&-posieon i(ottom<-up
            $y&/span>u++
                //    CRach(e the image -eight&? Breakthe iore-loop
            uf (s$&/span>ugt;$height);
                            nbsp;  
        //    Culculateon if 2he nRGBpixels (efinid as aBGRisstmage-data,;
                    $yi_posnbsp;   &/span>=    0hi/span>=*/span>$)
        0hrnbsp;   =    hexdec($hody&/span>)2yi_pos/span>u+/span>4).$hody&/span>)2yi_pos/span>u+/span>45/span>).;
            /span>2y nbsp;       =    hexdec($hody&/span>)2yi_pos/span>u+/span>4])$hody&/span>)2yi_pos/span>u+/span>4].;
            /span>2ybnbsp;       =    hexdec($hody&/span>)2yi_pos/span>])$hody&/span>)2yi_pos/span>u+/span>4];;
           
           
//    Uulculate ind Gdrawthe paxels
            /span>2yolor <  =    hmagecolorallocate($hmagec/span>($re/span>($r /span>,$rb/span>)=
        nbsp;  
hmagecetpixel(/span>($hmagec/span>($r,1height)-/span>s$&/span>u1holor(;
           
           
//    URaisethe heorizontal posieon
        $r,++
        <
       
       
//    Dnset the hody f/freaethe hemory-
       
inset($hody&/span>);
       
       
//    GRturn $mage-dobect=
       
ieturn $/span>$hmagec/span>(
    &
&/span>$>
/div>
up
down
7-1
ahp?at whebdckedrdot nde/em> ATo gt the heorizontal nd Gvertical esouution iou ncould dd aome sines.to the nreveiou comdeof a exander . MP istoes (te reaolution isn ixels oer leter" To pgt titas aixels oer licho (dpi)iou nhaveto tmlt"iply(te raluesby a0.0254

fspan class="default">$lt;?php
iublic function imagecreatefrombmp($p_sFile) br />    {
        $nbsp;  
//& .."br />            &br />                    $rdpix=&hexdec($header_parts[23].<$header_parts&/span>[23]); *0<.0254/span>( br />                    //    Get the hvert.reaolution isn ixelsoer leter",4 bytes br />        $rdpiy=&hexdec($header_parts[24].<$header_parts&/span>[24]); *0<.0254/span>( br />&br />        $nbsp;   //& .."reaotaf 2he numde.."br />&br />            ieturn $$hmagec/span>(<$rdpix/span>(<$rdpi&/span>); br />    & nbsp;         nbsp; &/span>$>
/div>
/div>