2011-04-16 01:23:15 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-05-03 20:13:10 +00:00
|
|
|
* Extraction of metadata from different bitmap image types.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Media
|
|
|
|
|
*/
|
2011-04-16 01:23:15 +00:00
|
|
|
|
2015-05-23 21:34:25 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2022-04-26 15:48:03 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2022-01-06 18:44:56 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2018-06-01 02:14:06 +00:00
|
|
|
use Wikimedia\XMPReader\Reader as XMPReader;
|
2015-05-23 21:34:25 +00:00
|
|
|
|
2012-05-03 20:13:10 +00:00
|
|
|
/**
|
|
|
|
|
* Class to deal with reconciling and extracting metadata from bitmap images.
|
|
|
|
|
* This is meant to comply with http://www.metadataworkinggroup.org/pdf/mwg_guidance.pdf
|
|
|
|
|
*
|
|
|
|
|
* This sort of acts as an intermediary between MediaHandler::getMetadata
|
|
|
|
|
* and the various metadata extractors.
|
|
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @todo Other image formats.
|
2020-06-30 10:13:50 +00:00
|
|
|
* @newable
|
|
|
|
|
* @note marked as newable in 1.35 for lack of a better alternative,
|
|
|
|
|
* but should become a stateless service, or a handler managed
|
|
|
|
|
* registry for metadata handlers for different file types.
|
2012-05-03 20:13:10 +00:00
|
|
|
* @ingroup Media
|
|
|
|
|
*/
|
2011-04-16 01:23:15 +00:00
|
|
|
class BitmapMetadataHandler {
|
2013-12-05 19:27:27 +00:00
|
|
|
/** @var array */
|
2016-02-17 09:09:32 +00:00
|
|
|
private $metadata = [];
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-12-05 19:27:27 +00:00
|
|
|
/** @var array Metadata priority */
|
2016-02-17 09:09:32 +00:00
|
|
|
private $metaPriority = [
|
|
|
|
|
20 => [ 'other' ],
|
|
|
|
|
40 => [ 'native' ],
|
|
|
|
|
60 => [ 'iptc-good-hash', 'iptc-no-hash' ],
|
|
|
|
|
70 => [ 'xmp-deprecated' ],
|
|
|
|
|
80 => [ 'xmp-general' ],
|
|
|
|
|
90 => [ 'xmp-exif' ],
|
|
|
|
|
100 => [ 'iptc-bad-hash' ],
|
|
|
|
|
120 => [ 'exif' ],
|
|
|
|
|
];
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-12-05 19:27:27 +00:00
|
|
|
/** @var string */
|
2011-04-16 01:23:15 +00:00
|
|
|
private $iptcType = 'iptc-no-hash';
|
|
|
|
|
|
|
|
|
|
/**
|
2013-03-07 16:27:38 +00:00
|
|
|
* This does the photoshop image resource app13 block
|
|
|
|
|
* of interest, IPTC-IIM metadata is stored here.
|
|
|
|
|
*
|
|
|
|
|
* Mostly just calls doPSIR and doIPTC
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $app13 String containing app13 block from jpeg file
|
2013-03-07 16:27:38 +00:00
|
|
|
*/
|
2013-03-18 19:44:43 +00:00
|
|
|
private function doApp13( $app13 ) {
|
2012-01-05 23:25:39 +00:00
|
|
|
try {
|
|
|
|
|
$this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
|
2023-06-09 16:14:32 +00:00
|
|
|
} catch ( InvalidPSIRException $e ) {
|
2012-01-05 23:25:39 +00:00
|
|
|
// Error reading the iptc hash information.
|
|
|
|
|
// This probably means the App13 segment is something other than what we expect.
|
|
|
|
|
// However, still try to read it, and treat it as if the hash didn't exist.
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( "Error parsing iptc data of file: " . $e->getMessage() );
|
2012-01-05 23:25:39 +00:00
|
|
|
$this->iptcType = 'iptc-no-hash';
|
|
|
|
|
}
|
2011-04-16 01:23:15 +00:00
|
|
|
|
|
|
|
|
$iptc = IPTC::parse( $app13 );
|
|
|
|
|
$this->addMetadata( $iptc, $this->iptcType );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 18:59:42 +00:00
|
|
|
/**
|
2011-08-21 17:16:57 +00:00
|
|
|
* Get exif info using exif class.
|
2011-05-28 18:59:42 +00:00
|
|
|
* Basically what used to be in BitmapHandler::getMetadata().
|
|
|
|
|
* Just calls stuff in the Exif class.
|
|
|
|
|
*
|
2012-02-01 20:53:38 +00:00
|
|
|
* Parameters are passed to the Exif class.
|
|
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param string $filename
|
|
|
|
|
* @param string $byteOrder
|
2011-05-28 18:59:42 +00:00
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public function getExif( $filename, $byteOrder ) {
|
2022-04-26 15:48:03 +00:00
|
|
|
$showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
|
2022-01-06 18:44:56 +00:00
|
|
|
if ( file_exists( $filename ) && $showEXIF ) {
|
2011-08-21 17:16:57 +00:00
|
|
|
$exif = new Exif( $filename, $byteOrder );
|
2011-04-16 01:23:15 +00:00
|
|
|
$data = $exif->getFilteredData();
|
|
|
|
|
if ( $data ) {
|
|
|
|
|
$this->addMetadata( $data, 'exif' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
/** Add misc metadata. Warning: atm if the metadata category
|
2013-03-07 16:27:38 +00:00
|
|
|
* doesn't have a priority, it will be silently discarded.
|
|
|
|
|
*
|
2014-08-13 19:41:39 +00:00
|
|
|
* @param array $metaArray Array of metadata values
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param string $type Type. defaults to other. if two things have the same type they're merged
|
2013-03-07 16:27:38 +00:00
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public function addMetadata( $metaArray, $type = 'other' ) {
|
2011-04-16 01:23:15 +00:00
|
|
|
if ( isset( $this->metadata[$type] ) ) {
|
|
|
|
|
/* merge with old data */
|
2020-07-31 19:41:28 +00:00
|
|
|
$metaArray += $this->metadata[$type];
|
2011-04-16 01:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->metadata[$type] = $metaArray;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-03-07 16:27:38 +00:00
|
|
|
* Merge together the various types of metadata
|
2021-12-26 11:13:47 +00:00
|
|
|
* the different types have different priorities,
|
2013-03-07 16:27:38 +00:00
|
|
|
* and are merged in order.
|
|
|
|
|
*
|
|
|
|
|
* This function is generally called by the media handlers' getMetadata()
|
|
|
|
|
*
|
2021-06-17 14:32:05 +00:00
|
|
|
* @return array
|
2013-03-07 16:27:38 +00:00
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public function getMetadataArray() {
|
2011-04-16 01:23:15 +00:00
|
|
|
// this seems a bit ugly... This is all so its merged in right order
|
2018-08-13 17:07:14 +00:00
|
|
|
// based on the MWG recommendation.
|
2016-02-17 09:09:32 +00:00
|
|
|
$temp = [];
|
2011-04-16 01:23:15 +00:00
|
|
|
krsort( $this->metaPriority );
|
|
|
|
|
foreach ( $this->metaPriority as $pri ) {
|
|
|
|
|
foreach ( $pri as $type ) {
|
|
|
|
|
if ( isset( $this->metadata[$type] ) ) {
|
|
|
|
|
// Do some special casing for multilingual values.
|
|
|
|
|
// Don't discard translations if also as a simple value.
|
|
|
|
|
foreach ( $this->metadata[$type] as $itemName => $item ) {
|
2019-03-29 20:12:24 +00:00
|
|
|
if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' &&
|
|
|
|
|
isset( $temp[$itemName] ) && !is_array( $temp[$itemName] )
|
|
|
|
|
) {
|
|
|
|
|
$default = $temp[$itemName];
|
|
|
|
|
$temp[$itemName] = $item;
|
|
|
|
|
$temp[$itemName]['x-default'] = $default;
|
|
|
|
|
unset( $this->metadata[$type][$itemName] );
|
2011-04-16 01:23:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 19:41:28 +00:00
|
|
|
$temp += $this->metadata[$type];
|
2011-04-16 01:23:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
return $temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Main entry point for jpeg's.
|
2011-05-29 14:24:27 +00:00
|
|
|
*
|
2014-07-24 17:43:25 +00:00
|
|
|
* @param string $filename Filename (with full path)
|
2013-12-05 19:27:27 +00:00
|
|
|
* @return array Metadata result array.
|
2023-06-09 16:14:32 +00:00
|
|
|
* @throws InvalidJpegException
|
2011-05-29 14:24:27 +00:00
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public static function Jpeg( $filename ) {
|
2015-03-12 22:49:22 +00:00
|
|
|
$showXMP = XMPReader::isSupported();
|
2011-04-16 01:23:15 +00:00
|
|
|
$meta = new self();
|
2011-04-16 11:17:14 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
$seg = JpegMetadataExtractor::segmentSplitter( $filename );
|
2017-05-16 12:56:26 +00:00
|
|
|
|
Use the unserialized form of image metadata internally
Image metadata is usually a serialized string representing an array.
Passing the string around internally and having everything unserialize
it is an awkward convention.
Also, many image handlers were reading the file twice: once for
getMetadata() and again for getImageSize(). Often getMetadata()
would actually read the width and height and then throw it away.
So, in filerepo:
* Add File::getMetadataItem(), which promises to allow partial
loading of metadata per my proposal on T275268 in a future commit.
* Add File::getMetadataArray(), which returns the unserialized array.
Some file handlers were returning non-serializable strings from
getMetadata(), so I gave them a legacy array form ['_error' => ...]
* Changed MWFileProps to return the array form of metadata.
* Deprecate the weird File::getImageSize(). It was apparently not
called by anything, but was overridden by UnregisteredLocalFile.
* Wrap serialize/unserialize with File::getMetadataForDb() and
File::loadMetadataFromDb() in preparation for T275268.
In MediaHandler:
* Merged MediaHandler::getImageSize() and MediaHandler::getMetadata()
into getSizeAndMetadata(). Deprecated the old methods.
* Instead of isMetadataValid() we now have isFileMetadataValid(), which
only gets a File object, so it can decide what data it needs to load.
* Simplified getPageDimensions() by having it return false for non-paged
media. It was not called in that case, but was implemented anyway.
In specific handlers:
* Rename DjVuHandler::getUnserializedMetadata() and
extractTreesFromMetadata() for clarity. "Metadata" in these function
names meant an XML string.
* Updated DjVuImage::getImageSize() to provide image sizes in the new
style.
* In ExifBitmapHandler, getRotationForExif() now takes just the
Orientation tag, rather than a serialized string. Also renamed for
clarity.
* In GIFMetadataExtractor, return the width, height and bits per channel
instead of throwing them away. There was some conflation in
decodeBPP() which I picked apart. Refer to GIF89a section 18.
* In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract
bits per channel, width, height and components (channel count). This
is essentially a port of PHP's getimagesize(), so should be bugwards
compatible.
* In PNGMetadataExtractor, return the width and height, which were
previously assigned to unused local variables. I verified the
implementation by referring to the specification.
* In SvgHandler, retain the version validation from unpackMetadata(),
but rename the function since it now takes an array as input.
In tests:
* In ExifBitmapTest, refactored some tests by using a provider.
* In GIFHandlerTest and PNGHandlerTest, I removed the tests in which
getMetadata() returns null, since it doesn't make sense when ported to
getMetadataArray(). I added tests for empty arrays instead.
* In tests, I retained serialization of input data since I figure it's
useful to confirm that existing database rows will continue to be read
correctly. I removed serialization of expected values, replacing them
with plain data.
* In tests, I replaced access to private class constants like
BROKEN_FILE with string literals, since stability is essential. If
the class constant changes, the test should fail.
Elsewhere:
* In maintenance/refreshImageMetadata.php, I removed the check for
shrinking image metadata, since it's not easy to implement and is
not future compatible. Image metadata is expected to shrink in
future.
Bug: T275268
Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
|
|
|
if ( isset( $seg['SOF'] ) ) {
|
|
|
|
|
$meta->addMetadata( [ 'SOF' => $seg['SOF'] ] );
|
|
|
|
|
}
|
2011-04-16 01:23:15 +00:00
|
|
|
if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$meta->addMetadata( [ 'JPEGFileComment' => $seg['COM'] ], 'native' );
|
2011-04-16 01:23:15 +00:00
|
|
|
}
|
2012-01-05 23:25:39 +00:00
|
|
|
if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
|
2013-04-20 21:11:46 +00:00
|
|
|
foreach ( $seg['PSIR'] as $curPSIRValue ) {
|
2012-01-05 23:25:39 +00:00
|
|
|
$meta->doApp13( $curPSIRValue );
|
|
|
|
|
}
|
2011-04-16 01:23:15 +00:00
|
|
|
}
|
|
|
|
|
if ( isset( $seg['XMP'] ) && $showXMP ) {
|
2017-12-03 02:07:03 +00:00
|
|
|
$xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
|
2011-04-16 01:23:15 +00:00
|
|
|
$xmp->parse( $seg['XMP'] );
|
|
|
|
|
foreach ( $seg['XMP_ext'] as $xmpExt ) {
|
|
|
|
|
/* Support for extended xmp in jpeg files
|
|
|
|
|
* is not well tested and a bit fragile.
|
|
|
|
|
*/
|
|
|
|
|
$xmp->parseExtended( $xmpExt );
|
|
|
|
|
}
|
|
|
|
|
$res = $xmp->getResults();
|
|
|
|
|
foreach ( $res as $type => $array ) {
|
|
|
|
|
$meta->addMetadata( $array, $type );
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-16 12:56:26 +00:00
|
|
|
|
2017-10-06 22:17:58 +00:00
|
|
|
$meta->getExif( $filename, $seg['byteOrder'] ?? 'BE' );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
return $meta->getMetadataArray();
|
|
|
|
|
}
|
2011-05-29 14:24:27 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
/** Entry point for png
|
2011-05-29 14:24:27 +00:00
|
|
|
* At some point in the future this might
|
|
|
|
|
* merge the png various tEXt chunks to that
|
|
|
|
|
* are interesting, but for now it only does XMP
|
|
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param string $filename Full path to file
|
|
|
|
|
* @return array Array for storage in img_metadata.
|
2011-05-29 14:24:27 +00:00
|
|
|
*/
|
2013-03-18 19:44:43 +00:00
|
|
|
public static function PNG( $filename ) {
|
2015-03-12 22:49:22 +00:00
|
|
|
$showXMP = XMPReader::isSupported();
|
2011-04-16 01:23:15 +00:00
|
|
|
|
|
|
|
|
$meta = new self();
|
|
|
|
|
$array = PNGMetadataExtractor::getMetadata( $filename );
|
2013-12-05 11:33:18 +00:00
|
|
|
if ( isset( $array['text']['xmp']['x-default'] )
|
|
|
|
|
&& $array['text']['xmp']['x-default'] !== '' && $showXMP
|
|
|
|
|
) {
|
2017-12-03 02:07:03 +00:00
|
|
|
$xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
|
2011-04-16 01:23:15 +00:00
|
|
|
$xmp->parse( $array['text']['xmp']['x-default'] );
|
|
|
|
|
$xmpRes = $xmp->getResults();
|
|
|
|
|
foreach ( $xmpRes as $type => $xmpSection ) {
|
|
|
|
|
$meta->addMetadata( $xmpSection, $type );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unset( $array['text']['xmp'] );
|
2022-03-28 20:10:05 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset xmp is not alone in text, makes text always set
|
2011-04-16 01:23:15 +00:00
|
|
|
$meta->addMetadata( $array['text'], 'native' );
|
|
|
|
|
unset( $array['text'] );
|
|
|
|
|
$array['metadata'] = $meta->getMetadataArray();
|
|
|
|
|
$array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** function for gif images.
|
|
|
|
|
*
|
|
|
|
|
* They don't really have native metadata, so just merges together
|
|
|
|
|
* XMP and image comment.
|
|
|
|
|
*
|
2014-07-24 17:43:25 +00:00
|
|
|
* @param string $filename Full path to file
|
2013-12-05 19:27:27 +00:00
|
|
|
* @return array Metadata array
|
2011-04-16 01:23:15 +00:00
|
|
|
*/
|
2013-03-18 19:44:43 +00:00
|
|
|
public static function GIF( $filename ) {
|
2011-04-16 01:23:15 +00:00
|
|
|
$meta = new self();
|
|
|
|
|
$baseArray = GIFMetadataExtractor::getMetadata( $filename );
|
|
|
|
|
|
|
|
|
|
if ( count( $baseArray['comment'] ) > 0 ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$meta->addMetadata( [ 'GIFFileComment' => $baseArray['comment'] ], 'native' );
|
2011-04-16 01:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-12 22:49:22 +00:00
|
|
|
if ( $baseArray['xmp'] !== '' && XMPReader::isSupported() ) {
|
2017-12-03 02:07:03 +00:00
|
|
|
$xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
|
2011-04-16 01:23:15 +00:00
|
|
|
$xmp->parse( $baseArray['xmp'] );
|
|
|
|
|
$xmpRes = $xmp->getResults();
|
|
|
|
|
foreach ( $xmpRes as $type => $xmpSection ) {
|
|
|
|
|
$meta->addMetadata( $xmpSection, $type );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unset( $baseArray['comment'] );
|
|
|
|
|
unset( $baseArray['xmp'] );
|
2012-10-19 20:10:42 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
$baseArray['metadata'] = $meta->getMetadataArray();
|
|
|
|
|
$baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
return $baseArray;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-21 17:16:57 +00:00
|
|
|
/**
|
|
|
|
|
* This doesn't do much yet, but eventually I plan to add
|
|
|
|
|
* XMP support for Tiff. (PHP's exif support already extracts
|
|
|
|
|
* but needs some further processing because PHP's exif support
|
|
|
|
|
* is stupid...)
|
|
|
|
|
*
|
2013-12-05 19:27:27 +00:00
|
|
|
* @todo Add XMP support, so this function actually makes sense to put here.
|
2011-08-21 17:16:57 +00:00
|
|
|
*
|
|
|
|
|
* The various exceptions this throws are caught later.
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param string $filename
|
2023-06-09 16:14:32 +00:00
|
|
|
* @throws InvalidTiffException
|
2013-12-05 19:27:27 +00:00
|
|
|
* @return array The metadata.
|
2011-08-21 17:16:57 +00:00
|
|
|
*/
|
2013-03-18 19:44:43 +00:00
|
|
|
public static function Tiff( $filename ) {
|
2011-08-21 17:16:57 +00:00
|
|
|
if ( file_exists( $filename ) ) {
|
|
|
|
|
$byteOrder = self::getTiffByteOrder( $filename );
|
|
|
|
|
if ( !$byteOrder ) {
|
2023-06-09 16:14:32 +00:00
|
|
|
throw new InvalidTiffException( "Error determining byte order of $filename" );
|
2011-08-21 17:16:57 +00:00
|
|
|
}
|
|
|
|
|
$exif = new Exif( $filename, $byteOrder );
|
|
|
|
|
$data = $exif->getFilteredData();
|
|
|
|
|
if ( $data ) {
|
|
|
|
|
$data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-08-21 17:16:57 +00:00
|
|
|
return $data;
|
|
|
|
|
} else {
|
2023-06-09 16:14:32 +00:00
|
|
|
throw new InvalidTiffException( "Could not extract data from tiff file $filename" );
|
2011-08-21 17:16:57 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-06-09 16:14:32 +00:00
|
|
|
throw new InvalidTiffException( "File doesn't exist - $filename" );
|
2011-08-21 17:16:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-08-21 17:16:57 +00:00
|
|
|
/**
|
|
|
|
|
* Read the first 2 bytes of a tiff file to figure out
|
|
|
|
|
* Little Endian or Big Endian. Needed for exif stuff.
|
|
|
|
|
*
|
2017-12-28 15:06:10 +00:00
|
|
|
* @param string $filename
|
2021-09-20 22:18:39 +00:00
|
|
|
* @return string|false 'BE' or 'LE' or false
|
2011-08-21 17:16:57 +00:00
|
|
|
*/
|
2020-05-17 23:57:36 +00:00
|
|
|
public static function getTiffByteOrder( $filename ) {
|
2011-08-21 17:16:57 +00:00
|
|
|
$fh = fopen( $filename, 'rb' );
|
2013-04-20 21:11:46 +00:00
|
|
|
if ( !$fh ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-08-21 17:16:57 +00:00
|
|
|
$head = fread( $fh, 2 );
|
|
|
|
|
fclose( $fh );
|
|
|
|
|
|
2013-04-26 14:42:31 +00:00
|
|
|
switch ( $head ) {
|
2011-08-21 17:16:57 +00:00
|
|
|
case 'II':
|
|
|
|
|
return 'LE'; // II for intel.
|
|
|
|
|
case 'MM':
|
|
|
|
|
return 'BE'; // MM for motorla.
|
|
|
|
|
default:
|
|
|
|
|
return false; // Something went wrong.
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-16 01:23:15 +00:00
|
|
|
}
|