Advertisement
sebbu

Java raw image writer

Oct 20th, 2011
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <?php
  2. //header('Content-Type: text/plain');
  3. $fp=fopen('C:\Users\sebbu\Downloads\test.txt', 'r') or die('failed to open file');
  4. $data=unpack('N', chr(0).fread($fp, 3));
  5. $data=$data[1];
  6. //var_dump($data);
  7. $dim=unpack('n2', fread($fp, 4));
  8. $width=$dim[1];
  9. $height=$dim[2];
  10. //var_dump($width, $height);
  11. $img=imagecreatetruecolor($width, $height);
  12. imageantialias($img, false);
  13. imagealphablending($img, false);
  14. $bg=imagecolorallocate($img, 255, 255, 255);
  15. imagefill($img, 0, 0, $bg);
  16. for($y=0;$y<$height;++$y) {
  17.     for($x=0;$x<$width;++$x) {
  18.         $pix=unpack('C3', fread($fp, 3));
  19.         $red=$pix[1];
  20.         $green=$pix[2];
  21.         $blue=$pix[3];
  22.         //var_dump($red, $green, $blue);
  23.         //die();
  24.         $col=imagecolorallocate($img, $red, $green, $blue);
  25.         if($col === false) {
  26.             echo 'imagecolorallocate failed for $red='.$red.' $green='.$green.' $blue='.$blue."\n";
  27.             die();
  28.         }
  29.         imagesetpixel($img, $x, $y, $col);
  30.     }
  31. }
  32. imagealphablending($img, true);
  33. imagepng($img, 'C:\Users\sebbu\Downloads\test.png');
  34. echo 'image created<br/>'."\n";
  35. fclose($fp);
  36. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement