2016-10-28 2 views
0

16bit bmp로 변환하는 코드가 있습니다. 나는 8 비트 bmp 파일을 얻기 위해 무엇을 바꾸거나 추가해야하는지 알아 내려고했지만 여전히 아무것도하지 못했습니다. 난 단지 그 변경 $의 bfOffBitsJPEG를 8bit로 변환합니다. BMP

<?php 
//convert jpeg to 16 bit bmp 

$jpgImageFile = 'TEST.jpg'; 
$newFileName = 'NEW_BMP'; 


$imageSource = imagecreatefromjpeg($jpgImageFile); 
imagebmp($imageSource,$newFileName.".bmp"); 


function imagebmp(&$im, $filename = "") 
{ 
    if (!$im) return false; 
    $w = imagesx($im); 
    $h = imagesy($im); 
    $result = ''; 

    if (!imageistruecolor($im)) { 
     $tmp = imagecreatetruecolor($w, $h); 
     imagecopy($tmp, $im, 0, 0, 0, 0, $w, $h); 
     imagedestroy($im); 
     $im = & $tmp; 
    } 

    $biBPLine = $w * 2; 
    $biStride = ($biBPLine + 3) & ~3; 
    $biSizeImage = $biStride * $h; 
    $bfOffBits = 66; 
    $bfSize = $bfOffBits + $biSizeImage; 
    $result .= substr('BM', 0, 2); 
    $result .= pack ('VvvV', $bfSize, 0, 0, $bfOffBits); 
    $result .= pack ('VVVvvVVVVVV', 40, $w, '-'.$h, 1, 16, 3, $biSizeImage, 0, 0, 0, 0); 
    $numpad = $biStride - $biBPLine; 


     $result .= pack('VVV',63488,2016,31); 
     for ($y = 0; $y < $h; ++$y) { 
     for ($x = 0; $x < $w; ++$x) { 

      $rgb = imagecolorat($im, $x, $y); 
      $r24 = ($rgb >> 16) & 0xFF; 
      $g24 = ($rgb >> 8) & 0xFF; 
      $b24 = $rgb & 0xFF; 
      $col = ((($r24 >> 3) << 11) | (($g24 >> 2) << 5) | ($b24 >> 3)); 
      $result .= pack('v',$col); 
     } 
     for ($i = 0; $i < $numpad; ++$i) 
      $result .= pack ('C', 0); 
    } 
    if($filename==""){ 
    } 
    else 
    { 
     $file = fopen($filename, "wb"); 
     fwrite($file, $result); 
     fclose($file); 
    } 
    return true; 
} 
?> 

질문해야 상상할 수 :이 8 비트에 대한처럼 보이는 방법?

+0

http://php.net/manual/en/function.imagetruecolortopalette.php –

+0

감사 @MarcB하지만이 기능을 사용 GD 라이브러리 등 PNG 및 GIF입니다 :

내 코드 imagick 호출 이것은 잘못된 방식입니다. – MrFreeman555

+0

예, 그리고? gd 라이브러리를 사용하고 있습니다. 'imagecreatefromjpeg'는 어디에서 온 것 같습니까? 이미지를 GD에로드하면 더 이상 gif/jpg/png가 아닙니다. 그것은 GD 이미지입니다. –

답변

0

답변을 찾았 으면 좋겠습니다. GD 라이브러리가 bmp 형식으로 응답하지 않기 때문에 imagick 라이브러리를 사용했습니다. 대신 diff30을 만드는 wbmp을 사용합니다.

<?php 
//convert jpeg to 8 bit bmp with imagick library 

//Read the image 
$im = new imagick('input.jpg'); 

// Set number of colors 
$numberColors = 256; 

// Set colorspace 
$colorSpace = Imagick::COLORSPACE_SRGB; 

// Set tree depth 
$treeDepth = 0; 

// Set dither 
$dither = false; 

// Set quantize 
$im->quantizeImage($numberColors, $colorSpace, $treeDepth, $dither, false); 

// write to disk 
$im->writeImage('output.bmp'); 
?>