Skip to content

Commit

Permalink
[mms] Horde_Image_Exif_Bundled now supports reading data from a PHP s…
Browse files Browse the repository at this point in the history
…tream, rather than a file.
  • Loading branch information
slusarz committed Jan 7, 2015
1 parent 0b3cf7d commit 8e28721
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
55 changes: 44 additions & 11 deletions framework/Image/lib/Horde/Image/Exif/Bundled.php
Expand Up @@ -29,6 +29,9 @@
*/
class Horde_Image_Exif_Bundled extends Horde_Image_Exif_Base
{
/**
* @param mixed $image Filename -or- an open PHP stream (@since 2.2.0).
*/
public function getData($image)
{
$raw = $this->_readData($image);
Expand All @@ -44,22 +47,38 @@ public function getData($image)
}
// Not really an EXIF property, but an attribute nonetheless...
// PHP's exif functions return it, so add it here to be consistent.
$exif['FileSize'] = @filesize($image);
if (is_resource($image)) {
fseek($image, SEEK_END);
$exif['FileSize'] = ftell($image);
} else {
$exif['FileSize'] = @filesize($image);
}

return $this->_processData($exif);
}

/**
*
* @param $path
* @param mixed $path Filename -or- an open PHP stream.
*
* @return array
*/
protected function _readData($path)
{
// There may be an elegant way to do this with one file handle.
$in = @fopen($path, 'rb');
$seek = @fopen($path, 'rb');
if (is_resource($path)) {
$in = $path;
rewind($in);
$seek = fopen('php://temp/', 'r+');
while (!feof($in)) {
fwrite($seek, fread($in, 65536));
}
rewind($in);
} else {
$in = @fopen($path, 'rb');
$seek = @fopen($path, 'rb');
}

$globalOffset = 0;
$result = array('Errors' => 0);

Expand All @@ -76,7 +95,9 @@ protected function _readData($path)
$result['ValidJpeg'] = 1;
} else {
$result['ValidJpeg'] = 0;
fclose($in);
if (!is_resource($path)) {
fclose($in);
}
fclose($seek);
return $result;
}
Expand Down Expand Up @@ -150,7 +171,9 @@ protected function _readData($path)
}

if ($data != 'ffe1') {
fclose($in);
if (!is_resource($path)) {
fclose($in);
}
fclose($seek);
return $result;
}
Expand Down Expand Up @@ -194,7 +217,9 @@ protected function _readData($path)
// Check for extremely large values here
if (hexdec($offset) > 100000) {
$result['ValidEXIFData'] = 0;
fclose($in);
if (!is_resource($path)) {
fclose($in);
}
fclose($seek);
return $result;
}
Expand Down Expand Up @@ -235,7 +260,9 @@ protected function _readData($path)
// Check for SubIFD
if (!isset($result['IFD0']['ExifOffset']) ||
$result['IFD0']['ExifOffset'] == 0) {
fclose($in);
if (!is_resource($path)) {
fclose($in);
}
fclose($seek);
return $result;
}
Expand Down Expand Up @@ -273,7 +300,9 @@ protected function _readData($path)

// Check for IFD1
if (!isset($result['IFD1Offset']) || $result['IFD1Offset'] == 0) {
fclose($in);
if (!is_resource($path)) {
fclose($in);
}
fclose($seek);
return $result;
}
Expand Down Expand Up @@ -319,7 +348,9 @@ protected function _readData($path)
// Check for Interoperability IFD
if (!isset($result['SubIFD']['ExifInteroperabilityOffset']) ||
$result['SubIFD']['ExifInteroperabilityOffset'] == 0) {
fclose($in);
if (!is_resource($path)) {
fclose($in);
}
fclose($seek);
return $result;
}
Expand Down Expand Up @@ -350,7 +381,9 @@ protected function _readData($path)
$result['Error'][$result['Errors']] = Horde_Image_Translation::t("Illegal size for InteroperabilityIFD");
}

fclose($in);
if (!is_resource($path)) {
fclose($in);
}
fclose($seek);
return $result;
}
Expand Down
12 changes: 6 additions & 6 deletions framework/Image/package.xml
Expand Up @@ -25,16 +25,16 @@
</lead>
<date>2014-10-21</date>
<version>
<release>2.1.1</release>
<api>2.0.0</api>
<release>2.2.0</release>
<api>2.2.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Horde_Image_Exif_Bundled now supports reading data from a PHP stream, rather than a file.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand Down Expand Up @@ -1046,15 +1046,15 @@ Initial release as a PEAR package
</release>
<release>
<version>
<release>2.1.1</release>
<api>2.0.0</api></version>
<release>2.2.0</release>
<api>2.2.0</api></version>
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2014-10-21</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Horde_Image_Exif_Bundled now supports reading data from a PHP stream, rather than a file.
</notes>
</release>
</changelog>
Expand Down

0 comments on commit 8e28721

Please sign in to comment.