dongshao6591
2011-03-23 12:32
浏览 68
已采纳

PHP将原始RGBA保存为PNG / GIF

I have an image file, with a 128 byte header, followed by raw RGBA data and the width and height are stored at offset 72 and 74 as short ints (big endian). I have successfully been able to read the header, however, I need a way to save the raw RGBA as PNG format and/or GIF. How can I do that in PHP?

cheers

图片转代码服务由CSDN问答提供 功能建议

我有一个图像文件,带有128字节标题,后跟原始RGBA数据,存储宽度和高度 偏移72和74为短整数(大端)。 我已成功读取标题,但是,我需要一种方法将原始RGBA保存为PNG格式和/或GIF。 我怎么能用PHP做到这一点?

欢呼

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dow56114 2011-03-23 12:46
    已采纳

    If you have the gd lib installed (check with phpinfo(); ) it's something like

    //create image
    $img = imagecreatetruecolor($width, $height);
    
    //fill by iterating through your raw pixel data
    imagesetpixel($img, $x, $y, $color);
    
    //output
    header("Content-Type: image/png");
    imagepng($img);
    
    //cleanup
    imagedestroy($img);
    

    EDIT

    You said it was RGBA data. Assuming standard 4 bytes per channel and that your raw data is an integer array, it should be:

    $pos = ($y * $width + $x) * 4 + ($headerLengthInBytes / 4);
    $red = $rawImageData[$pos];
    $green = $rawImageData[$pos + 1];
    $blue = $rawImageData[$pos + 2];
    $alpha = $rawImageData[$pos + 3];
    
    已采纳该答案
    打赏 评论
  • drny20290570 2011-03-23 12:41

    I would look at gd or imagemagick one or both of these should do the trick

    打赏 评论
  • dongmeiba6151 2011-03-23 12:44

    While you could generate an uncompressed PNG from your source stream yourself, that takes a lot of effort. (Need to handicraft the png32 header, and write out uncompressed deflate chunks because PNG itself supports no uncompressed payloads).

    It's easier with imagemagick. Simply write out your raw RGBA stream to a temporary file and use:

    file_put_contents("tmp.rgba", susbstr($data, 128));
    
    exec("convert -size 640x300 -depth 8   tmp.rgba output.png");
    
    打赏 评论

相关推荐 更多相似问题