module.graphic.svg.php 4.0 KB

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