PNG file format परिभाषित करता है कि एक पीएनजी दस्तावेज डेटा के कई हिस्सों में विभाजित होता है। इसलिए आपको अपनी इच्छा के अनुसार अपने रास्ते पर नेविगेट करना होगा।
जो डेटा आप निकालना चाहते हैं उसे tEXt
खंड में परिभाषित किया गया प्रतीत होता है। मैंने निम्नलिखित वर्ग को लिखा है ताकि आप पीएनजी फाइलों से हिस्सों को निकालने की अनुमति दे सकें।
class PNG_Reader
{
private $_chunks;
private $_fp;
function __construct($file) {
if (!file_exists($file)) {
throw new Exception('File does not exist');
}
$this->_chunks = array();
// Open the file
$this->_fp = fopen($file, 'r');
if (!$this->_fp)
throw new Exception('Unable to open file');
// Read the magic bytes and verify
$header = fread($this->_fp, 8);
if ($header != "\x89PNG\x0d\x0a\x1a\x0a")
throw new Exception('Is not a valid PNG image');
// Loop through the chunks. Byte 0-3 is length, Byte 4-7 is type
$chunkHeader = fread($this->_fp, 8);
while ($chunkHeader) {
// Extract length and type from binary data
$chunk = @unpack('Nsize/a4type', $chunkHeader);
// Store position into internal array
if ($this->_chunks[$chunk['type']] === null)
$this->_chunks[$chunk['type']] = array();
$this->_chunks[$chunk['type']][] = array (
'offset' => ftell($this->_fp),
'size' => $chunk['size']
);
// Skip to next chunk (over body and CRC)
fseek($this->_fp, $chunk['size'] + 4, SEEK_CUR);
// Read next chunk header
$chunkHeader = fread($this->_fp, 8);
}
}
function __destruct() { fclose($this->_fp); }
// Returns all chunks of said type
public function get_chunks($type) {
if ($this->_chunks[$type] === null)
return null;
$chunks = array();
foreach ($this->_chunks[$type] as $chunk) {
if ($chunk['size'] > 0) {
fseek($this->_fp, $chunk['offset'], SEEK_SET);
$chunks[] = fread($this->_fp, $chunk['size']);
} else {
$chunks[] = '';
}
}
return $chunks;
}
}
आप इस तरह के रूप में अपने वांछित tEXt
हिस्सा निकालने के लिए इस तरह के रूप में इसका इस्तेमाल हो सकता है:
$file = '18201010338AM16390621000846.png';
$png = new PNG_Reader($file);
$rawTextData = $png->get_chunks('tEXt');
$metadata = array();
foreach($rawTextData as $data) {
$sections = explode("\0", $data);
if($sections > 1) {
$key = array_shift($sections);
$metadata[$key] = implode("\0", $sections);
} else {
$metadata[] = $data;
}
}
स्रोत
2010-02-03 07:56:53
क्षमा करें, न लक्ष्य है और न ही प्रश्न को समझने ... – deceze
पीएनजी मात्रा में बांटा गया है (http://www.libpng.org/pub/png/spec/ 1.2/पीएनजी-Chunks.html)। और आप शायद 'टेक्स्ट' खंड की तलाश में हैं जिसमें एक टिप्पणी है ('टिप्पणी' कीवर्ड के साथ निर्दिष्ट)। – Gumbo