Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] img .tga jak odtworzyć plik .tga na stronie?
Forum PHP.pl > Forum > PHP
mathevs
Witam wszystkich smile.gif
tak jak w temacie poszukuje funkcji która otwiera .tga na stronie (img)
tak zeby czytało np jak .png
z góry dzięki ;D
cudny
http://superuser.com/questions/269475/how-...ga-files-to-png

Trzymaj sobie i tga i png
Czyli wrzucasz tga na serwer i generujesz poprzez shell_exec(bash_file) png.
mathevs
plików tga mam no ponad 12 tyś
także potrzebuje czegoś takiego

  1. <?php
  2.  
  3.  
  4. function rle_decode($data, $datalen)
  5. {
  6. $len = strlen($data);
  7.  
  8. $out = '';
  9.  
  10. $i = 0;
  11. $k = 0;
  12. while ($i<$len)
  13. {
  14. dec_bits(ord($data[$i]), $type, $value);
  15. if ($k >= $datalen)
  16. {
  17. break;
  18. }
  19.  
  20. $i++;
  21.  
  22. if ($type == 0) //raw
  23. {
  24. for ($j=0; $j<3*$value; $j++)
  25. {
  26. $out .= $data[$j+$i];
  27. $k++;
  28. }
  29. $i += $value*3;
  30. }
  31. else //rle
  32. {
  33. for ($j=0; $j<$value; $j++)
  34. {
  35. $out .= $data[$i] . $data[$i+1] . $data[$i+2];
  36. $k++;
  37. }
  38. $i += 3;
  39. }
  40. }
  41. return $out;
  42. }
  43.  
  44. function dec_bits($byte, &$type, &$value)
  45. {
  46. $type = ($byte & 0x80) >> 7;
  47. $value = 1 + ($byte & 0x7F);
  48. }
  49.  
  50. function getimagesizetga($filename)
  51. {
  52. $f = fopen($filename, 'rb');
  53. $header = fread($f, 18);
  54. $header = @unpack( "cimage_id_len/ccolor_map_type/cimage_type/vcolor_map_origin/vcolor_map_len/" .
  55. "ccolor_map_entry_size/vx_origin/vy_origin/vwidth/vheight/" .
  56. "cpixel_size/cdescriptor", $header);
  57. fclose($f);
  58.  
  59. $types = array(0,1,2,3,9,10,11,32,33);
  60. if (!in_array($header['image_type'], $types))
  61. {
  62. return array(0, 0, 0, 0, 0);
  63. }
  64.  
  65. if ($header['pixel_size'] > 32)
  66. {
  67. return array(0, 0, 0, 0, 0);
  68. }
  69.  
  70. return array($header['width'], $header['height'], 'tga', $header['pixel_size'], $header['image_type']);
  71. }
  72.  
  73. function imagecreatefromtga($filename)
  74. {
  75. $f = fopen($filename, 'rb');
  76. if (!$f)
  77. {
  78. return false;
  79. }
  80. $header = fread($f, 18);
  81. $header = unpack( "cimage_id_len/ccolor_map_type/cimage_type/vcolor_map_origin/vcolor_map_len/" .
  82. "ccolor_map_entry_size/vx_origin/vy_origin/vwidth/vheight/" .
  83. "cpixel_size/cdescriptor", $header);
  84.  
  85. switch ($header['image_type'])
  86. {
  87. case 2: //no palette, uncompressed
  88. case 10: //no palette, rle
  89. break;
  90. default: die('Unsupported TGA format');
  91. }
  92.  
  93. if ($header['pixel_size'] != 32)
  94. {
  95. die('Unsupported TGA color depth');
  96. }
  97.  
  98. $bytes = $header['pixel_size'] / 8;
  99.  
  100. if ($header['image_id_len'] > 0)
  101. {
  102. $header['image_id'] = fread($f, $header['image_id_len']);
  103. }
  104. else
  105. {
  106. $header['image_id'] = '';
  107. }
  108.  
  109. $im = imagecreatetruecolor($header['width'], $header['height']);
  110.  
  111. $size = $header['width'] * $header['height'] * 3;
  112.  
  113. //-- check whether this is NEW TGA or not
  114. $pos = ftell($f);
  115. fseek($f, -26, SEEK_END);
  116. $newtga = fread($f, 26);
  117. if (substr($newtga, 8, 16) != 'TRUEVISION-XFILE')
  118. {
  119. $newtga = false;
  120. }
  121.  
  122. fseek($f, 0, SEEK_END);
  123. $datasize = ftell($f) - $pos;
  124. if ($newtga)
  125. {
  126. $datasize -= 26;
  127. }
  128.  
  129. fseek($f, $pos, SEEK_SET);
  130.  
  131. //-- end of check
  132. $data = fread($f, $datasize);
  133. if ($header['image_type'] == 10)
  134. {
  135. $data = rle_decode($data, $size);
  136. }
  137. if (bit5($header['descriptor']) == 1)
  138. {
  139. $reverse = true;
  140. }
  141. else
  142. {
  143. $reverse = false;
  144. }
  145.  
  146. $pixels = str_split($data, 4);
  147. $i = 0;
  148.  
  149. //read pixels
  150. if ($reverse)
  151. {
  152. for ($y=0; $y<$header['height']; $y++)
  153. {
  154. for ($x=0; $x<$header['width']; $x++)
  155. {
  156. imagesetpixel($im, $x, $y, dwordize($pixels[$i]));
  157. $i++;
  158. }
  159. }
  160. }
  161. else
  162. {
  163. for ($y=$header['height']-1; $y>=0; $y--)
  164. {
  165. for ($x=0; $x<$header['width']; $x++)
  166. {
  167. imagesetpixel($im, $x, $y, dwordize($pixels[$i]));
  168. $i++;
  169. }
  170. }
  171. }
  172. fclose($f);
  173.  
  174. return $im;
  175. }
  176.  
  177. function dwordize($str)
  178. {
  179. $a = ord($str[0]);
  180. $b = ord($str[1]);
  181. $c = ord($str[2]);
  182. return $c*256*256 + $b*256 + $a;
  183. }
  184.  
  185. function bit5($x)
  186. {
  187. return ($x & 32) >> 5;
  188. }
  189. ?>


tylko ze ten skrypt nie działa albo nie moge go wywałać
cudny
http://de77.com/php/open-tga-with-php-imagecreatefromtga

Ale ja jednak bym był za tym, abyś zrobił to raz na zawsze i nie generował tego w locie
thek
Tga w locie to ogólnie zły pomysł. Czemu? W skrócie... Tga to wariacja na temat bmp z kanałem alfa. Czyli mamy ogromny plik z jeszcze jednym kanałem, a więc więcej niż ogromną ilość danych do przesłania. O niebo lepiej się wychodzi na konwersji tego do png, które choć nadal duże, to jest znacznie mniejsze i też ma przezroczystość. O wsparciu przeglądarek dla tga nie wspomnę.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.