|
Size: | 5715 |
Storage flags: | strip |
<?php
class Image_XMP
{
public $_sFilename = null;
public $_aXMP = array();
public $_bXMPParse = false;
public function isValid()
{
return $this->_bXMPParse;
}
public function getAllTags()
{
return $this->_aXMP;
}
public function _get_jpeg_header_data($filename)
{
ignore_user_abort(true);
if (is_readable($filename) && is_file($filename) && ($filehnd = fopen($filename, 'rb'))) {
} else {
return false;
}
$data = fread($filehnd, 2);
if ($data != "\xFF\xD8")
{
echo '<p>This probably is not a JPEG file</p>'."\n";
fclose($filehnd);
return false;
}
$data = fread($filehnd, 2);
if ($data{0} != "\xFF")
{
fclose($filehnd);
return false;
}
$hit_compressed_image_data = false;
while (($data{1} != "\xD9") && (!$hit_compressed_image_data) && (!feof($filehnd)))
{
if ((ord($data{1}) < 0xD0) || (ord($data{1}) > 0xD7))
{
$sizestr = fread($filehnd, 2);
$decodedsize = unpack('nsize', $sizestr);
$segdatastart = ftell($filehnd);
$segdata = fread($filehnd, $decodedsize['size'] - 2);
$headerdata[] = array(
'SegType' => ord($data{1}),
'SegName' => $GLOBALS['JPEG_Segment_Names'][ord($data{1})],
'SegDataStart' => $segdatastart,
'SegData' => $segdata,
);
}
if ($data{1} == "\xDA")
{
$hit_compressed_image_data = true;
}
else
{
$data = fread($filehnd, 2);
if ($data{0} != "\xFF")
{
fclose($filehnd);
return false;
}
}
}
fclose($filehnd);
ignore_user_abort(false);
return $headerdata;
}
public function _get_XMP_text($filename)
{
$jpeg_header_data = $this->_get_jpeg_header_data($filename);
for ($i = 0; $i < count($jpeg_header_data); $i++)
{
if (strcmp($jpeg_header_data[$i]['SegName'], 'APP1') == 0)
{
if (strncmp($jpeg_header_data[$i]['SegData'], 'http://ns.adobe.com/xap/1.0/'."\x00", 29) == 0)
{
$xmp_data = substr($jpeg_header_data[$i]['SegData'], 29);
return trim($xmp_data);
}
}
}
return false;
}
public function read_XMP_array_from_text($xmltext)
{
if (trim($xmltext) == '')
{
return false;
}
$xml_parser = xml_parser_create('UTF-8');
if (xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 0) == false)
{
xml_parser_free($xml_parser);
return false;
}
if (xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0) == false)
{
xml_parser_free($xml_parser);
return false;
}
if (xml_parse_into_struct($xml_parser, $xmltext, $values, $tags) == 0)
{
xml_parser_free($xml_parser);
return false;
}
xml_parser_free($xml_parser);
$xmp_array = array();
$current_property = '';
$container_index = -1;
foreach ($values as $xml_elem)
{
switch ($xml_elem['tag'])
{
case 'x:xmpmeta':
break;
case 'rdf:RDF':
break;
case 'rdf:Description':
switch ($xml_elem['type'])
{
case 'open':
case 'complete':
if (array_key_exists('attributes', $xml_elem))
{
foreach (array_keys($xml_elem['attributes']) as $key)
{
if (true)
{
$xmp_array[$key] = $xml_elem['attributes'][$key];
}
}
}
case 'cdata':
case 'close':
break;
}
case 'rdf:ID':
case 'rdf:nodeID':
break;
case 'rdf:li':
if ($xml_elem['type'] == 'complete')
{
if (array_key_exists('attributes', $xml_elem))
{
if (isset($xml_elem['attributes']['xml:lang']) && ($xml_elem['attributes']['xml:lang'] != 'x-default'))
{
break;
}
}
if ($current_property != '')
{
$xmp_array[$current_property][$container_index] = (isset($xml_elem['value']) ? $xml_elem['value'] : '');
$container_index += 1;
}
}
break;
case 'rdf:Seq':
case 'rdf:Bag':
case 'rdf:Alt':
switch ($xml_elem['type'])
{
case 'open':
$container_index = 0;
break;
case 'close':
$container_index = -1;
break;
case 'cdata':
break;
}
break;
default:
if (true)
{
switch ($xml_elem['type'])
{
case 'open':
$current_property = $xml_elem['tag'];
break;
case 'close':
$current_property = '';
break;
case 'complete':
$xmp_array[$xml_elem['tag']] = (isset($xml_elem['attributes']) ? $xml_elem['attributes'] : (isset($xml_elem['value']) ? $xml_elem['value'] : ''));
break;
case 'cdata':
break;
}
}
break;
}
}
return $xmp_array;
}
public function Image_XMP($sFilename)
{
$this->_sFilename = $sFilename;
if (is_file($this->_sFilename))
{
$xmp_data = $this->_get_XMP_text($sFilename);
if ($xmp_data)
{
$this->_aXMP = $this->read_XMP_array_from_text($xmp_data);
$this->_bXMPParse = true;
}
}
}
}
$GLOBALS['JPEG_Segment_Names'] = array(
0x01 => 'TEM',
0x02 => 'RES',
0xC0 => 'SOF0',
0xC1 => 'SOF1',
0xC2 => 'SOF2',
0xC3 => 'SOF4',
0xC4 => 'DHT',
0xC5 => 'SOF5',
0xC6 => 'SOF6',
0xC7 => 'SOF7',
0xC8 => 'JPG',
0xC9 => 'SOF9',
0xCA => 'SOF10',
0xCB => 'SOF11',
0xCC => 'DAC',
0xCD => 'SOF13',
0xCE => 'SOF14',
0xCF => 'SOF15',
0xD0 => 'RST0',
0xD1 => 'RST1',
0xD2 => 'RST2',
0xD3 => 'RST3',
0xD4 => 'RST4',
0xD5 => 'RST5',
0xD6 => 'RST6',
0xD7 => 'RST7',
0xD8 => 'SOI',
0xD9 => 'EOI',
0xDA => 'SOS',
0xDB => 'DQT',
0xDC => 'DNL',
0xDD => 'DRI',
0xDE => 'DHP',
0xDF => 'EXP',
0xE0 => 'APP0',
0xE1 => 'APP1',
0xE2 => 'APP2',
0xE3 => 'APP3',
0xE4 => 'APP4',
0xE5 => 'APP5',
0xE6 => 'APP6',
0xE7 => 'APP7',
0xE8 => 'APP8',
0xE9 => 'APP9',
0xEA => 'APP10',
0xEB => 'APP11',
0xEC => 'APP12',
0xED => 'APP13',
0xEE => 'APP14',
0xEF => 'APP15',
0xF0 => 'JPG0',
0xF1 => 'JPG1',
0xF2 => 'JPG2',
0xF3 => 'JPG3',
0xF4 => 'JPG4',
0xF5 => 'JPG5',
0xF6 => 'JPG6',
0xF7 => 'JPG7',
0xF8 => 'JPG8',
0xF9 => 'JPG9',
0xFA => 'JPG10',
0xFB => 'JPG11',
0xFC => 'JPG12',
0xFD => 'JPG13',
0xFE => 'COM',
);