2016-10-28 2 views
0

私は16bit bmpに変換するコードを持っています。私は何が変わるべきか、または8ビットのbmpファイルを作成するために追加することを試みようとしていましたが、まだ何もありませんでした。それが8ビットのためにどのように見えるか:$ 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; 
} 
?> 

質問を変更する必要があります私はそれを想像することができますか?

+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形式に応答しないので、代わりにwbmpを使用するので、imagickライブラリを使用しました。

<?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'); 
?> 
関連する問題