File indexing completed on 2024-05-12 17:26:00

0001 <?php
0002 /////////////////////////////////////////////////////////////////
0003 /// getID3() by James Heinrich <info@getid3.org>               //
0004 //  available at http://getid3.sourceforge.net                 //
0005 //            or http://www.getid3.org                         //
0006 //          also https://github.com/JamesHeinrich/getID3       //
0007 /////////////////////////////////////////////////////////////////
0008 // See readme.txt for more details                             //
0009 /////////////////////////////////////////////////////////////////
0010 //                                                             //
0011 // module.graphic.svg.php                                      //
0012 // module for analyzing SVG Image files                        //
0013 // dependencies: NONE                                          //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_svg extends getid3_handler
0019 {
0020 
0021 
0022   public function Analyze() {
0023     $info = &$this->getid3->info;
0024 
0025     $this->fseek($info['avdataoffset']);
0026 
0027     $SVGheader = $this->fread(4096);
0028     if (preg_match('#\<\?xml([^\>]+)\?\>#i', $SVGheader, $matches)) {
0029       $info['svg']['xml']['raw'] = $matches;
0030     }
0031     if (preg_match('#\<\!DOCTYPE([^\>]+)\>#i', $SVGheader, $matches)) {
0032       $info['svg']['doctype']['raw'] = $matches;
0033     }
0034     if (preg_match('#\<svg([^\>]+)\>#i', $SVGheader, $matches)) {
0035       $info['svg']['svg']['raw'] = $matches;
0036     }
0037     if (isset($info['svg']['svg']['raw'])) {
0038 
0039       $sections_to_fix = array('xml', 'doctype', 'svg');
0040       foreach ($sections_to_fix as $section_to_fix) {
0041         if (!isset($info['svg'][$section_to_fix])) {
0042           continue;
0043         }
0044         $section_data = array();
0045         while (preg_match('/ "([^"]+)"/', $info['svg'][$section_to_fix]['raw'][1], $matches)) {
0046           $section_data[] = $matches[1];
0047           $info['svg'][$section_to_fix]['raw'][1] = str_replace($matches[0], '', $info['svg'][$section_to_fix]['raw'][1]);
0048         }
0049         while (preg_match('/([^\s]+)="([^"]+)"/', $info['svg'][$section_to_fix]['raw'][1], $matches)) {
0050           $section_data[] = $matches[0];
0051           $info['svg'][$section_to_fix]['raw'][1] = str_replace($matches[0], '', $info['svg'][$section_to_fix]['raw'][1]);
0052         }
0053         $section_data = array_merge($section_data, preg_split('/[\s,]+/', $info['svg'][$section_to_fix]['raw'][1]));
0054         foreach ($section_data as $keyvaluepair) {
0055           $keyvaluepair = trim($keyvaluepair);
0056           if ($keyvaluepair) {
0057             $keyvalueexploded = explode('=', $keyvaluepair);
0058             $key   = (isset($keyvalueexploded[0]) ? $keyvalueexploded[0] : '');
0059             $value = (isset($keyvalueexploded[1]) ? $keyvalueexploded[1] : '');
0060             $info['svg'][$section_to_fix]['sections'][$key] = trim($value, '"');
0061           }
0062         }
0063       }
0064 
0065       $info['fileformat']                  = 'svg';
0066       $info['video']['dataformat']         = 'svg';
0067       $info['video']['lossless']           = true;
0068       //$info['video']['bits_per_sample']    = 24;
0069       $info['video']['pixel_aspect_ratio'] = (float) 1;
0070 
0071       if (!empty($info['svg']['svg']['sections']['width'])) {
0072         $info['svg']['width']  = intval($info['svg']['svg']['sections']['width']);
0073       }
0074       if (!empty($info['svg']['svg']['sections']['height'])) {
0075         $info['svg']['height'] = intval($info['svg']['svg']['sections']['height']);
0076       }
0077       if (!empty($info['svg']['svg']['sections']['version'])) {
0078         $info['svg']['version'] = $info['svg']['svg']['sections']['version'];
0079       }
0080       if (!isset($info['svg']['version']) && isset($info['svg']['doctype']['sections'])) {
0081         foreach ($info['svg']['doctype']['sections'] as $key => $value) {
0082           if (preg_match('#//W3C//DTD SVG ([0-9\.]+)//#i', $key, $matches)) {
0083             $info['svg']['version'] = $matches[1];
0084             break;
0085           }
0086         }
0087       }
0088 
0089       if (!empty($info['svg']['width'])) {
0090         $info['video']['resolution_x'] = $info['svg']['width'];
0091       }
0092       if (!empty($info['svg']['height'])) {
0093         $info['video']['resolution_y'] = $info['svg']['height'];
0094       }
0095 
0096       return true;
0097     }
0098     $info['error'][] = 'Did not find expected <svg> tag';
0099     return false;
0100   }
0101 
0102 }