📄 phpthumb.bmp.php
字号:
foreach ($paletteindexes as $paletteindex) {
$col = $pixelcounter % $thisfile_bmp_header_raw['width'];
$row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
$thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindex];
$pixelcounter++;
}
break;
}
} else {
// encoded mode - the first byte of the pair contains the number of pixels to be
// drawn using the color indexes in the second byte. The second byte contains two
// color indexes, one in its high-order 4 bits and one in its low-order 4 bits.
// The first of the pixels is drawn using the color specified by the high-order
// 4 bits, the second is drawn using the color in the low-order 4 bits, the third
// is drawn using the color in the high-order 4 bits, and so on, until all the
// pixels specified by the first byte have been drawn.
$paletteindexes[0] = ($secondbyte & 0xF0) >> 4;
$paletteindexes[1] = ($secondbyte & 0x0F);
for ($i = 0; $i < $firstbyte; $i++) {
$col = $pixelcounter % $thisfile_bmp_header_raw['width'];
$row = $thisfile_bmp_header_raw['height'] - 1 - (($pixelcounter - $col) / $thisfile_bmp_header_raw['width']);
$thisfile_bmp['data'][$row][$col] = $thisfile_bmp['palette'][$paletteindexes[($i % 2)]];
$pixelcounter++;
}
}
}
break;
default:
$ThisFileInfo['error'][] = 'Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data';
break;
}
break;
case 3: // BI_BITFIELDS
switch ($thisfile_bmp_header_raw['bits_per_pixel']) {
case 16:
case 32:
$redshift = 0;
$greenshift = 0;
$blueshift = 0;
if (!$thisfile_bmp_header_raw['red_mask'] || !$thisfile_bmp_header_raw['green_mask'] || !$thisfile_bmp_header_raw['blue_mask']) {
$ThisFileInfo['error'][] = 'missing $thisfile_bmp_header_raw[(red|green|blue)_mask]';
return false;
}
while ((($thisfile_bmp_header_raw['red_mask'] >> $redshift) & 0x01) == 0) {
$redshift++;
}
while ((($thisfile_bmp_header_raw['green_mask'] >> $greenshift) & 0x01) == 0) {
$greenshift++;
}
while ((($thisfile_bmp_header_raw['blue_mask'] >> $blueshift) & 0x01) == 0) {
$blueshift++;
}
for ($row = ($thisfile_bmp_header_raw['height'] - 1); $row >= 0; $row--) {
for ($col = 0; $col < $thisfile_bmp_header_raw['width']; $col++) {
$pixelvalue = $this->LittleEndian2Int(substr($BMPpixelData, $pixeldataoffset, $thisfile_bmp_header_raw['bits_per_pixel'] / 8));
$pixeldataoffset += $thisfile_bmp_header_raw['bits_per_pixel'] / 8;
$red = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['red_mask']) >> $redshift) / ($thisfile_bmp_header_raw['red_mask'] >> $redshift)) * 255));
$green = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['green_mask']) >> $greenshift) / ($thisfile_bmp_header_raw['green_mask'] >> $greenshift)) * 255));
$blue = intval(round(((($pixelvalue & $thisfile_bmp_header_raw['blue_mask']) >> $blueshift) / ($thisfile_bmp_header_raw['blue_mask'] >> $blueshift)) * 255));
$thisfile_bmp['data'][$row][$col] = (($red << 16) | ($green << 8) | ($blue));
}
while (($pixeldataoffset % 4) != 0) {
// lines are padded to nearest DWORD
$pixeldataoffset++;
}
}
break;
default:
$ThisFileInfo['error'][] = 'Unknown bits-per-pixel value ('.$thisfile_bmp_header_raw['bits_per_pixel'].') - cannot read pixel data';
break;
}
break;
default: // unhandled compression type
$ThisFileInfo['error'][] = 'Unknown/unhandled compression type value ('.$thisfile_bmp_header_raw['compression'].') - cannot decompress pixel data';
break;
}
}
return true;
}
function IntColor2RGB($color) {
$red = ($color & 0x00FF0000) >> 16;
$green = ($color & 0x0000FF00) >> 8;
$blue = ($color & 0x000000FF);
return array($red, $green, $blue);
}
function PlotPixelsGD(&$BMPdata, $truecolor=true) {
$imagewidth = $BMPdata['header']['raw']['width'];
$imageheight = $BMPdata['header']['raw']['height'];
if ($truecolor) {
$gd = @ImageCreateTrueColor($imagewidth, $imageheight);
} else {
$gd = @ImageCreate($imagewidth, $imageheight);
if (!empty($BMPdata['palette'])) {
// create GD palette from BMP palette
foreach ($BMPdata['palette'] as $color) {
list($r, $g, $b) = $this->IntColor2RGB($color);
ImageColorAllocate($gd, $r, $g, $b);
}
} else {
// create 216-color websafe palette
for ($r = 0x00; $r <= 0xFF; $r += 0x33) {
for ($g = 0x00; $g <= 0xFF; $g += 0x33) {
for ($b = 0x00; $b <= 0xFF; $b += 0x33) {
ImageColorAllocate($gd, $r, $g, $b);
}
}
}
}
}
if (!is_resource($gd)) {
return false;
}
foreach ($BMPdata['data'] as $row => $colarray) {
@set_time_limit(30);
foreach ($colarray as $col => $color) {
list($red, $green, $blue) = $this->IntColor2RGB($color);
if ($truecolor) {
$pixelcolor = ImageColorAllocate($gd, $red, $green, $blue);
} else {
$pixelcolor = ImageColorClosest($gd, $red, $green, $blue);
}
ImageSetPixel($gd, $col, $row, $pixelcolor);
}
}
return $gd;
}
function PlotBMP(&$BMPinfo) {
$starttime = time();
if (!isset($BMPinfo['bmp']['data']) || !is_array($BMPinfo['bmp']['data'])) {
echo 'ERROR: no pixel data<br />';
return false;
}
set_time_limit(intval(round($BMPinfo['resolution_x'] * $BMPinfo['resolution_y'] / 10000)));
$im = $this->PlotPixelsGD($BMPinfo['bmp']);
if (headers_sent()) {
echo 'plotted '.($BMPinfo['resolution_x'] * $BMPinfo['resolution_y']).' pixels in '.(time() - $starttime).' seconds<br />';
ImageDestroy($im);
exit;
} else {
header('Content-Type: image/png');
ImagePNG($im);
ImageDestroy($im);
return true;
}
return false;
}
function BMPcompressionWindowsLookup($compressionid) {
static $BMPcompressionWindowsLookup = array(
0 => 'BI_RGB',
1 => 'BI_RLE8',
2 => 'BI_RLE4',
3 => 'BI_BITFIELDS',
4 => 'BI_JPEG',
5 => 'BI_PNG'
);
return (isset($BMPcompressionWindowsLookup[$compressionid]) ? $BMPcompressionWindowsLookup[$compressionid] : 'invalid');
}
function BMPcompressionOS2Lookup($compressionid) {
static $BMPcompressionOS2Lookup = array(
0 => 'BI_RGB',
1 => 'BI_RLE8',
2 => 'BI_RLE4',
3 => 'Huffman 1D',
4 => 'BI_RLE24',
);
return (isset($BMPcompressionOS2Lookup[$compressionid]) ? $BMPcompressionOS2Lookup[$compressionid] : 'invalid');
}
// from getid3.lib.php
function trunc($floatnumber) {
// truncates a floating-point number at the decimal point
// returns int (if possible, otherwise float)
if ($floatnumber >= 1) {
$truncatednumber = floor($floatnumber);
} elseif ($floatnumber <= -1) {
$truncatednumber = ceil($floatnumber);
} else {
$truncatednumber = 0;
}
if ($truncatednumber <= 1073741824) { // 2^30
$truncatednumber = (int) $truncatednumber;
}
return $truncatednumber;
}
function LittleEndian2Int($byteword) {
$intvalue = 0;
$byteword = strrev($byteword);
$bytewordlen = strlen($byteword);
for ($i = 0; $i < $bytewordlen; $i++) {
$intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
}
return $intvalue;
}
function BigEndian2Int($byteword) {
return $this->LittleEndian2Int(strrev($byteword));
}
function BigEndian2Bin($byteword) {
$binvalue = '';
$bytewordlen = strlen($byteword);
for ($i = 0; $i < $bytewordlen; $i++) {
$binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
}
return $binvalue;
}
function FixedPoint2_30($rawdata) {
$binarystring = $this->BigEndian2Bin($rawdata);
return $this->Bin2Dec(substr($binarystring, 0, 2)) + (float) ($this->Bin2Dec(substr($binarystring, 2, 30)) / 1073741824);
}
function Bin2Dec($binstring, $signed=false) {
$signmult = 1;
if ($signed) {
if ($binstring{0} == '1') {
$signmult = -1;
}
$binstring = substr($binstring, 1);
}
$decvalue = 0;
for ($i = 0; $i < strlen($binstring); $i++) {
$decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
}
return $this->CastAsInt($decvalue * $signmult);
}
function CastAsInt($floatnum) {
// convert to float if not already
$floatnum = (float) $floatnum;
// convert a float to type int, only if possible
if ($this->trunc($floatnum) == $floatnum) {
// it's not floating point
if ($floatnum <= 1073741824) { // 2^30
// it's within int range
$floatnum = (int) $floatnum;
}
}
return $floatnum;
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -