example.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. ini_set('display_errors', 'On');
  3. error_reporting(E_ALL | E_STRICT);
  4. require 'nanodicom.php';
  5. $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'samples'.DIRECTORY_SEPARATOR;
  6. $files = array();
  7. if ($handle = opendir($dir)) {
  8. while (false !== ($file = readdir($handle)))
  9. {
  10. if ($file != "." && $file != ".." && is_file($dir.$file))
  11. {
  12. $files[] = $file;
  13. }
  14. }
  15. closedir($handle);
  16. }
  17. foreach ($files as $file)
  18. {
  19. $filename = $dir.$file;
  20. // 1) Most basic example. Fast!
  21. try
  22. {
  23. echo "1) Most basic example. Fast!\n";
  24. $dicom = Nanodicom::factory($filename);
  25. // Only a small subset of the dictionary entries were loaded
  26. echo $dicom->parse()->profiler_diff('parse')."\n";
  27. unset($dicom);
  28. }
  29. catch (Nanodicom_Exception $e)
  30. {
  31. echo 'File failed. '.$e->getMessage()."\n";
  32. }
  33. // 2) Load only given tags. It will stop once all given tags are found. Fastest!
  34. try
  35. {
  36. echo "2) Load only given tags. It will stop once all given tags are found. Fastest!\n";
  37. $dicom = Nanodicom::factory($filename, 'simple');
  38. $dicom->parse(array(array(0x0010, 0x0010)));
  39. // Only a small subset of the dictionary entries were loaded
  40. echo $dicom->profiler_diff('parse')."\n";
  41. echo 'Patient name if exists: '.$dicom->value(0x0010, 0x0010)."\n"; // Patient Name if exists
  42. // This will return nothing because dictionaries were not loaded
  43. echo 'Patient name should be empty here: '.$dicom->PatientName."\n";
  44. unset($dicom);
  45. }
  46. catch (Nanodicom_Exception $e)
  47. {
  48. echo 'File failed. '.$e->getMessage()."\n";
  49. }
  50. // 3) Load only given tags by name. Stops once all tags are found. Not so fast.
  51. try
  52. {
  53. echo "3) Load only given tags by name. Stops once all tags are found. Not so fast.\n";
  54. $dicom = Nanodicom::factory($filename, 'simple');
  55. $dicom->parse(array('PatientName'));
  56. echo $dicom->profiler_diff('parse')."\n";
  57. echo 'Patient name if exists: '.$dicom->value(0x0010, 0x0010)."\n"; // Patient Name if exists
  58. // Or
  59. echo 'Patient name if exists: '.$dicom->PatientName."\n"; // Patient Name if exists
  60. unset($dicom);
  61. }
  62. catch (Nanodicom_Exception $e)
  63. {
  64. echo 'File failed. '.$e->getMessage()."\n";
  65. }
  66. // 4) Load only given tags. Dump it and print certain tags. Load 'dumper' directly.
  67. try
  68. {
  69. echo "4) Load only given tags. Dump it and print certain tags. Load 'dumper' directly.\n";
  70. $dicom = Nanodicom::factory($filename, 'dumper');
  71. $dicom->parse(array(array(0x0010, 0x0010)));
  72. echo $dicom->dump();
  73. echo $dicom->profiler_diff('parse')."\n";
  74. // Patient Name if exists
  75. echo 'Something should show if element exists.'.$dicom->value(0x0010, 0x0010)."\n";
  76. // This will return the value because 'dumper' was used and loaded the dictionaries
  77. echo 'This should be empty, no dictionaries loaded.'.$dicom->PatientName."\n";
  78. unset($dicom);
  79. }
  80. catch (Nanodicom_Exception $e)
  81. {
  82. echo 'File failed. '.$e->getMessage()."\n";
  83. }
  84. // 5) Load simple and print certain value
  85. try
  86. {
  87. echo "5) Load simple and print certain value\n";
  88. $dicom = Nanodicom::factory($filename);
  89. $dicom->parse();
  90. echo $dicom->profiler_diff('parse')."\n";
  91. echo 'Patient Name: '.$dicom->value(0x0010, 0x0010)."\n"; // Patient Name if exists
  92. unset($dicom);
  93. }
  94. catch (Nanodicom_Exception $e)
  95. {
  96. echo 'File failed. '.$e->getMessage()."\n";
  97. }
  98. // 6) Load simple and extend it to 'dumper'
  99. try
  100. {
  101. echo "6) Load simple and extend it to 'dumper'\n";
  102. $dicom = Nanodicom::factory($filename);
  103. echo $dicom->parse()->extend('dumper')->dump();
  104. echo $dicom->profiler_diff('parse').' '.$dicom->profiler_diff('dump')."\n";
  105. unset($dicom);
  106. }
  107. catch (Nanodicom_Exception $e)
  108. {
  109. echo 'File failed. '.$e->getMessage()."\n";
  110. }
  111. // 7) Load simple and extend it to 'dumper'. No need to parse, dump() does it. Parsing is done only once.
  112. try
  113. {
  114. echo "7) Load simple and extend it to 'dumper'. No need to parse, dump() does it. Parsing is done only once.\n";
  115. $dicom = Nanodicom::factory($filename);
  116. echo $dicom->extend('dumper')->dump();
  117. echo $dicom->profiler_diff('parse').' '.$dicom->profiler_diff('dump')."\n";
  118. unset($dicom);
  119. }
  120. catch (Nanodicom_Exception $e)
  121. {
  122. echo 'File failed. '.$e->getMessage()."\n";
  123. }
  124. // 8) Load 'dumper' directly. Dump output is in html format.
  125. try
  126. {
  127. echo "8) Load 'dumper' directly. Dump output is in html format.\n";
  128. $dicom = Nanodicom::factory($filename, 'dumper');
  129. echo $dicom->parse()->dump('html');
  130. echo $dicom->profiler_diff('parse').' '.$dicom->profiler_diff('dump')."\n";
  131. unset($dicom);
  132. }
  133. catch (Nanodicom_Exception $e)
  134. {
  135. echo 'File failed. '.$e->getMessage()."\n";
  136. }
  137. // 9) Load 'anonymizer' directly.
  138. try
  139. {
  140. echo "9) Load 'anonymizer' directly.\n";
  141. $dicom = Nanodicom::factory($filename, 'anonymizer');
  142. file_put_contents($filename.'.ex9', $dicom->anonymize());
  143. unset($dicom);
  144. }
  145. catch (Nanodicom_Exception $e)
  146. {
  147. echo 'File failed. '.$e->getMessage()."\n";
  148. }
  149. // 10) Extend 'anonymizer'. No need to call parse(), anonymize() will do it.
  150. try
  151. {
  152. echo "10) Extend 'anonymizer'. No need to call parse(), anonymize() will do it.\n";
  153. $dicom = Nanodicom::factory($filename);
  154. file_put_contents($filename.'.ex10', $dicom->extend('anonymizer')->anonymize());
  155. unset($dicom);
  156. }
  157. catch (Nanodicom_Exception $e)
  158. {
  159. echo 'File failed. '.$e->getMessage()."\n";
  160. }
  161. // 11) Double extension (and probably you can go on and on)
  162. try
  163. {
  164. echo "11) Double extension (and probably you can go on and on)\n";
  165. $dicom = Nanodicom::factory($filename);
  166. echo $dicom->extend('dumper')->dump();
  167. file_put_contents($filename.'.ex11', $dicom->extend('anonymizer')->anonymize());
  168. unset($dicom);
  169. }
  170. catch (Nanodicom_Exception $e)
  171. {
  172. echo 'File failed. '.$e->getMessage()."\n";
  173. }
  174. // 12) Save file as Explicit VR Little Endian
  175. try
  176. {
  177. echo "12) Save file as Explicit VR Little Endian\n";
  178. $dicom = Nanodicom::factory($filename);
  179. echo $dicom->parse()->profiler_diff('parse')."\n";
  180. // Setting values takes care of even length
  181. // If set to '1.2.840.10008.1.2.1.99' it will use deflate
  182. $dicom->value(0x0002, 0x0010, Nanodicom::EXPLICIT_VR_LITTLE_ENDIAN);
  183. echo $dicom->write_file($filename.'.ex12')->profiler_diff('write')."\n";
  184. unset($dicom);
  185. }
  186. catch (Nanodicom_Exception $e)
  187. {
  188. echo 'File failed. '.$e->getMessage()."\n";
  189. }
  190. // 13) Pass file contents instead of filename
  191. try
  192. {
  193. echo "13) Pass file contents instead of filename\n";
  194. $contents = file_get_contents($filename);
  195. $dicom = Nanodicom::factory($contents, 'dumper', 'blob');
  196. echo $dicom->dump();
  197. unset($dicom);
  198. }
  199. catch (Nanodicom_Exception $e)
  200. {
  201. echo 'File failed. '.$e->getMessage()."\n";
  202. }
  203. // 14) Check if file has preamble and DICM
  204. try
  205. {
  206. echo "14) Check if file has preamble and DICM\n";
  207. $dicom = Nanodicom::factory($filename);
  208. echo 'Is DICOM? '.$dicom->is_dicom()."\n";
  209. unset($dicom);
  210. }
  211. catch (Nanodicom_Exception $e)
  212. {
  213. echo 'File failed. '.$e->getMessage()."\n";
  214. }
  215. // 15) Anonymize and save file as Explicit VR Little Endian
  216. try
  217. {
  218. echo "15) Anonymize and save file as Explicit VR Little Endian\n";
  219. $dicom = Nanodicom::factory($filename);
  220. echo $dicom->parse()->profiler_diff('parse')."\n";
  221. // Setting values takes care of even length
  222. // If set to '1.2.840.10008.1.2.1.99' it will use deflate
  223. $dicom->value(0x0002, 0x0010, Nanodicom::EXPLICIT_VR_LITTLE_ENDIAN);
  224. file_put_contents($filename.'.ex15', $dicom->extend('anonymizer')->anonymize());
  225. unset($dicom);
  226. }
  227. catch (Nanodicom_Exception $e)
  228. {
  229. echo 'File failed. '.$e->getMessage()."\n";
  230. }
  231. // 16) Provide your own dumper formatting
  232. try
  233. {
  234. echo "16) Provide your own dumper formatting\n";
  235. $formatting = array(
  236. 'dataset_begin' => '',
  237. 'dataset_end' => "\n",
  238. 'item_begin' => 'BEGIN',
  239. 'item_end' => 'END',
  240. 'spacer' => ' ',
  241. 'text_begin' => 'TEXTBEGIN',
  242. 'text_end' => 'TEXTEND',
  243. 'columns' => array(
  244. 'off' => array('%04X', ' '),
  245. 'g' => array('%04X', ':'),
  246. 'e' => array('%04X', ' '),
  247. 'name' => array('%-10.10s', ' '),
  248. 'vr' => array('%2s', ' '),
  249. 'len' => array('%-3d', ' '),
  250. 'val' => array('[%s]', ''),
  251. ),
  252. );
  253. $dicom = Nanodicom::factory($filename, 'dumper');
  254. echo $dicom->dump($formatting);
  255. unset($dicom);
  256. }
  257. catch (Nanodicom_Exception $e)
  258. {
  259. echo 'File failed. '.$e->getMessage()."\n";
  260. }
  261. // 17) Anonymize on the fly and dump the contents, don't save to a file
  262. try
  263. {
  264. echo "17) Anonymize on the fly and dump the contents, don't save to a file\n";
  265. $dicom = Nanodicom::factory($filename, 'anonymizer');
  266. $dicom1 = Nanodicom::factory($dicom->anonymize(), 'dumper', 'blob');
  267. echo $dicom1->dump();
  268. unset($dicom);
  269. unset($dicom1);
  270. }
  271. catch (Nanodicom_Exception $e)
  272. {
  273. echo 'File failed. '.$e->getMessage()."\n";
  274. }
  275. // 18) Pass your own list of elements to anonymizer
  276. try
  277. {
  278. echo "18) Pass your own list of elements to anonymizer\n";
  279. // Own tag elements for anonymizing
  280. $tags = array(
  281. array(0x0008, 0x0020, '{date|Ymd}'), // Study Date
  282. array(0x0008, 0x0021, '{date|Ymd}'), // Series Date
  283. array(0x0008, 0x0090, 'physician{random}'), // Referring Physician
  284. array(0x0010, 0x0010, 'patient{consecutive}'), // Patient Name
  285. array(0x0010, 0x0020, 'id{consecutive}'), // Patient ID
  286. array(0x0010, 0x0030, '{date|Ymd}'), // Patient Date of Birth
  287. );
  288. $dicom = Nanodicom::factory($filename, 'anonymizer');
  289. $dicom1 = Nanodicom::factory($dicom->anonymize($tags), 'dumper', 'blob');
  290. echo $dicom1->dump();
  291. unset($dicom);
  292. unset($dicom1);
  293. }
  294. catch (Nanodicom_Exception $e)
  295. {
  296. echo 'File failed. '.$e->getMessage()."\n";
  297. }
  298. // 19) Pass your own list of mappings to anonymizer. Patient Name should be replace to
  299. // 'Mapped' if 'Anonymized' is found. Case sensitive
  300. try
  301. {
  302. echo "19) Pass your own list of mappings to anonymizer\n";
  303. // Own tag elements for anonymizing
  304. $tags = array(
  305. array(0x0008, 0x0020, '{date|Ymd}'), // Study Date
  306. array(0x0008, 0x0021, '{date|Ymd}'), // Series Date
  307. array(0x0008, 0x0090, 'physician{random}'), // Referring Physician
  308. array(0x0010, 0x0010, 'patient{consecutive}'), // Patient Name
  309. array(0x0010, 0x0020, 'id{consecutive}'), // Patient ID
  310. array(0x0010, 0x0030, '{date|Ymd}'), // Patient Date of Birth
  311. );
  312. $replacements = array(
  313. array(0x0010, 0x0010, 'anonymized', 'Mapped'),
  314. );
  315. $dicom = Nanodicom::factory($filename, 'anonymizer');
  316. $dicom1 = Nanodicom::factory($dicom->anonymize($tags, $replacements), 'dumper', 'blob');
  317. echo $dicom1->dump();
  318. file_put_contents($filename.'.ex19', $dicom1->write());
  319. unset($dicom);
  320. unset($dicom1);
  321. }
  322. catch (Nanodicom_Exception $e)
  323. {
  324. echo 'File failed. '.$e->getMessage()."\n";
  325. }
  326. // 20) Gets the images from the dicom object if they exist. This example is for gd
  327. try
  328. {
  329. echo "20) Gets the images from the dicom object if they exist. This example is for gd\n";
  330. $dicom = Nanodicom::factory($filename, 'pixeler');
  331. if ( ! file_exists($filename.'.0.jpg'))
  332. {
  333. $images = $dicom->get_images();
  334. // If using another library, for example, imagemagick, the following should be done:
  335. // $images = $dicom->set_driver('imagick')->get_images();
  336. if ($images !== FALSE)
  337. {
  338. foreach ($images as $index => $image)
  339. {
  340. // Defaults to jpg
  341. $dicom->write_image($image, $dir.$file.'.'.$index);
  342. // To write another format, pass the format in second parameter.
  343. // This will write a png image instead
  344. // $dicom->write_image($image, $dir.$file.'.'.$index, 'png');
  345. }
  346. }
  347. else
  348. {
  349. echo "There are no DICOM images or transfer syntax not supported yet.\n";
  350. }
  351. $images = NULL;
  352. }
  353. else
  354. {
  355. echo "Image already exists\n";
  356. }
  357. unset($dicom);
  358. }
  359. catch (Nanodicom_Exception $e)
  360. {
  361. echo 'File failed. '.$e->getMessage()."\n";
  362. }
  363. // 21) Prints summary report
  364. try
  365. {
  366. echo "21) Prints summary report\n";
  367. $dicom = Nanodicom::factory($filename);
  368. echo $dicom->summary();
  369. unset($dicom);
  370. }
  371. catch (Nanodicom_Exception $e)
  372. {
  373. echo 'File failed. '.$e->getMessage()."\n";
  374. }
  375. }