Skip to content

Commit

Permalink
Merge pull request #13 from marc1706/ticket/12
Browse files Browse the repository at this point in the history
Correctly skip over exif in images
  • Loading branch information
marc1706 committed Jun 8, 2016
2 parents 27467cf + 216661e commit 50ae9b1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/Type/TypeJpeg.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class TypeJpeg extends TypeBase
"\xCF"
);

/** @var array JPEG APP markers */
protected $appMarkers = array(
"\xE0",
"\xE1",
"\xE2",
"\xE3",
"\xEC",
"\xED",
"\xEE",
);

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -75,6 +86,18 @@ protected function isSofMarker($firstByte, $secondByte)
return $firstByte === self::SOF_START_MARKER && in_array($secondByte, $this->sofMarkers);
}

/**
* Return if current data point is an APP marker
*
* @param string $firstByte First byte to check
* @param string $secondByte Second byte to check
* @return bool True if current data point is APP marker, false if not
*/
protected function isAppMarker($firstByte, $secondByte)
{
return $firstByte === self::SOF_START_MARKER && in_array($secondByte, $this->appMarkers);
}

/**
* Get size info from image data
*
Expand All @@ -89,8 +112,19 @@ protected function getSizeInfo($data)
$dataLength = strlen($data);

// Look through file for SOF marker
for ($i = 2 * self::SHORT_SIZE; $i < $dataLength; $i++)
for ($i = 0; $i < $dataLength; $i++)
{
if ($this->isAppMarker($data[$i], $data[$i + 1]))
{
// Extract length from APP marker
list(, $unpacked) = unpack("H*", substr($data, $i + self::SHORT_SIZE, 2));

$length = hexdec(substr($unpacked, 0, 4));

// Skip over length of APP header
$i += (int) $length;
}

if ($this->isSofMarker($data[$i], $data[$i + 1]))
{
// Extract size info from SOF marker
Expand Down
1 change: 1 addition & 0 deletions tests/FastImageSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function dataGetImageSize()
array('png', 'image/icon', false),
array('meh', '', false),
array('meh', 'image/meh', false),
array('exif.jpg', 'image/jpeg', array('width' => 100, 'height' => 100, 'type' => IMAGETYPE_JPEG)),
);
}

Expand Down
Binary file added tests/fixture/exif.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 50ae9b1

Please sign in to comment.