module.graphic.svg.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. // also https://github.com/JamesHeinrich/getID3 //
  7. /////////////////////////////////////////////////////////////////
  8. // See readme.txt for more details //
  9. /////////////////////////////////////////////////////////////////
  10. // //
  11. // module.graphic.svg.php //
  12. // module for analyzing SVG Image files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_svg extends getid3_handler
  17. {
  18. public function Analyze() {
  19. $info = &$this->getid3->info;
  20. $this->fseek($info['avdataoffset']);
  21. $SVGheader = $this->fread(4096);
  22. if (preg_match('#\<\?xml([^\>]+)\?\>#i', $SVGheader, $matches)) {
  23. $info['svg']['xml']['raw'] = $matches;
  24. }
  25. if (preg_match('#\<\!DOCTYPE([^\>]+)\>#i', $SVGheader, $matches)) {
  26. $info['svg']['doctype']['raw'] = $matches;
  27. }
  28. if (preg_match('#\<svg([^\>]+)\>#i', $SVGheader, $matches)) {
  29. $info['svg']['svg']['raw'] = $matches;
  30. }
  31. if (isset($info['svg']['svg']['raw'])) {
  32. $sections_to_fix = array('xml', 'doctype', 'svg');
  33. foreach ($sections_to_fix as $section_to_fix) {
  34. if (!isset($info['svg'][$section_to_fix])) {
  35. continue;
  36. }
  37. $section_data = array();
  38. while (preg_match('/ "([^"]+)"/', $info['svg'][$section_to_fix]['raw'][1], $matches)) {
  39. $section_data[] = $matches[1];
  40. $info['svg'][$section_to_fix]['raw'][1] = str_replace($matches[0], '', $info['svg'][$section_to_fix]['raw'][1]);
  41. }
  42. while (preg_match('/([^\s]+)="([^"]+)"/', $info['svg'][$section_to_fix]['raw'][1], $matches)) {
  43. $section_data[] = $matches[0];
  44. $info['svg'][$section_to_fix]['raw'][1] = str_replace($matches[0], '', $info['svg'][$section_to_fix]['raw'][1]);
  45. }
  46. $section_data = array_merge($section_data, preg_split('/[\s,]+/', $info['svg'][$section_to_fix]['raw'][1]));
  47. foreach ($section_data as $keyvaluepair) {
  48. $keyvaluepair = trim($keyvaluepair);
  49. if ($keyvaluepair) {
  50. $keyvalueexploded = explode('=', $keyvaluepair);
  51. $key = (isset($keyvalueexploded[0]) ? $keyvalueexploded[0] : '');
  52. $value = (isset($keyvalueexploded[1]) ? $keyvalueexploded[1] : '');
  53. $info['svg'][$section_to_fix]['sections'][$key] = trim($value, '"');
  54. }
  55. }
  56. }
  57. $info['fileformat'] = 'svg';
  58. $info['video']['dataformat'] = 'svg';
  59. $info['video']['lossless'] = true;
  60. //$info['video']['bits_per_sample'] = 24;
  61. $info['video']['pixel_aspect_ratio'] = (float) 1;
  62. if (!empty($info['svg']['svg']['sections']['width'])) {
  63. $info['svg']['width'] = intval($info['svg']['svg']['sections']['width']);
  64. }
  65. if (!empty($info['svg']['svg']['sections']['height'])) {
  66. $info['svg']['height'] = intval($info['svg']['svg']['sections']['height']);
  67. }
  68. if (!empty($info['svg']['svg']['sections']['version'])) {
  69. $info['svg']['version'] = $info['svg']['svg']['sections']['version'];
  70. }
  71. if (!isset($info['svg']['version']) && isset($info['svg']['doctype']['sections'])) {
  72. foreach ($info['svg']['doctype']['sections'] as $key => $value) {
  73. if (preg_match('#//W3C//DTD SVG ([0-9\.]+)//#i', $key, $matches)) {
  74. $info['svg']['version'] = $matches[1];
  75. break;
  76. }
  77. }
  78. }
  79. if (!empty($info['svg']['width'])) {
  80. $info['video']['resolution_x'] = $info['svg']['width'];
  81. }
  82. if (!empty($info['svg']['height'])) {
  83. $info['video']['resolution_y'] = $info['svg']['height'];
  84. }
  85. return true;
  86. }
  87. $info['error'][] = 'Did not find expected <svg> tag';
  88. return false;
  89. }
  90. }