首頁 >後端開發 >php教程 >bmp圖片 php 將bmp圖片轉換為jpg等其他任一格式的圖片

bmp圖片 php 將bmp圖片轉換為jpg等其他任一格式的圖片

WBOY
WBOY原創
2016-07-29 08:40:201145瀏覽

複製程式碼程式碼如下:


php
// 範例:
$path = ROOT . 'php
// 範例:
$path = ROOT . 2009/06/03/124401282315771。 ';
$pathAll = $path 。 'bmp';
$mi = '' ;
$mi = ImageCreateFromBMP( $pathAll );
imagejpeg( $mi , $path . ' jpg ' );
// 函數:
function ImageCreateFromBMP( $filename )
{
// 檔案開啟方式
if ( ! $f1 = fopen ( $filename , " rb " ) ) 回傳假;
// 1 : FICHIER的費用
$FILE = unpack ( " vfile_type/Vfile_size/Vreserved/Vbitmap_offset " , fread ( $f1 , 14 ));
if ( $FILE[ ' file_type ' ] != 19778 ) return F5; >// 2 : 壓縮BMP
$BMP = unpack ( ' Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel ' .
' /Vcompression/Vsize_bitmap/Vhoriz_resolution . /Vcolors_important ', fread ( $f1 , 40 ));
$BMP['顏色']=pow(2,$BMP['bits_per_pixel']);
if ( $BMP [ ' size_bitmap ' ] = = 0 ) $BMP [ ' size_bitmap ' ] = $FILE [ ' file_size ' ] - $FILE [ ' bitmap_offset ' ];
$BMP ['bytes_per_pixel'] = $BMP['bits_per_pixel']/8; $BMP['bytes_per_pixel2']=ceil($BMP['bytes_per_pixel2']);
$BMP [ ' 貼花' ] = ( $BMP [ ' 寬度' ] * $BMP [ ' bytes_per_pixel ' ] / 4 ) ;
$BMP [ ' 貼花' ] -= 地板( $BMP [ ' 寬度' ] * $BMP [ ' bytes_per_pixel ' ] / 4 );
$BMP [ ' 貼花' ] = 4 - ( 4 * $BMP [ ' 貼花' ]);
if ( $BMP [ ' 貼花' ] == 4 ) $BMP [ ' 貼花' ] = 0 ;
// 3 : 調色盤顏色的費用
$PALETTE = array ();
if ( $BMP [ '顏色' ] {
$PALETTE = unpack ( ' V ' . $BMP [ ' 顏色' ] , fread ( $ f1 , $BMP [ ' 顏色' '] * 4));
}
// 4 : 建立影像
$IMG = fread ( $f1 , $BMP [ ' size_bitmap ' ]);
$VIDE = chr ( 0 );
$res = imagecreatetruecolor( $BMP [ ' 寬度' ] , $BMP [ ' 高度' ]);
$P = 0 ;
$Y = $BMP ['高度'] - 1;
while ( $Y >= 0 )
{
$X = 0 ;
while ( $X {
if ( $BMP [ ' bits_per_pixel ' ] == 24 )
$COLOR = unpack ( " V " , substr ($IMG,$P,3);
elseif ( $BMP [ 'bits_per_pixel ' ] == 16 )
{
$COLOR = unpack ( " n " , substr ( $IMG , $P , 2 ));
$COLOR [ 1 ] = $PALETTE [ $COLOR [ 1 ] + 1 ];
}
elseif ( $BMP [ 'bits_per_pixel ' ] == 8 )
{
$COLOR = unpack ( " n " , $VIDE . substr ( $IMG , $ P , 1 )) ;
$COLOR [ 1 ] = $PALETTE [ $COLOR [ 1 ] + 1 ];
}
elseif ( $BMP [ 'bits_per_pixel ' ] == 4 )
{
$COLOR = unpack ( " n " , $VIDE . substr ( $IMG , Floor ( $P ) , 1));
if (( $P * 2 ) % 2 == 0 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] >> 4 ) ;否則$COLOR[1]=($COLOR[1]&0x0F);
$COLOR [ 1 ] = $PALETTE [ $COLOR [ 1 ] + 1 ];
}
elseif ( $BMP [ 'bits_per_pixel ' ] == 1 )
{
$COLOR = unpack ( " n " , $VIDE . substr ( $IMG , Floor ( $P ) , 1));
if (( $P * 8 ) % 8 == 0 ) $COLOR [ 1 ] = $COLOR [ 1 ] >>> 7;
elseif (( $P * 8 ) % 8 == 1 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x40 ) >>> 6 ;
elseif (( $P * 8 ) % 8 == 2 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x20 ) >>> 5 ;
elseif (( $P * 8 ) % 8 == 3 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x10 ) >>> 4 ;
elseif (( $P * 8 ) % 8 == 4 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x8 ) >>> 3 ;
elseif (( $P * 8 ) % 8 == 5 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x4 ) >>> 2 ;
elseif (( $P * 8 ) % 8 == 6 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x2 ) >>> 1 ;
elseif (( $P * 8 ) % 8 == 7 ) $COLOR [ 1 ] = ( $COLOR [ 1 ] & 0x1 );
$COLOR [ 1 ] = $PALETTE [ $COLOR [ 1 ] + 1 ];
}
否則
回傳FALSE ;
imagesetpixel( $res , $X , $Y , $COLOR [ 1 ]);
$X++;
$P += $BMP [ ' bytes_per_pixel ' ];
}
$Y -- ;
$P += $BMP [ ' 貼花' ];
}
// 檔案過濾
fclose ( $f1 );
回傳$res;
}
? >

以上就介紹了bmp圖片 php將bmp圖片轉為jpg等其他各種格式的圖片,包括bmp圖片方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn