Skip to content

Commit

Permalink
Fix reading of small SVG images to determine dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jan 11, 2019
1 parent d04af55 commit 7528117
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions third_party/fasterimage/ImageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function parseType()
return $this->type = 'tiff';
default:
$this->stream->resetPointer();
$markup = $this->stream->read( 1024 );
$markup = $this->stream->read( 1024, false );
if ( false !== strpos( $markup, '<svg' ) ) {
$this->type = 'svg';
} else {
Expand Down Expand Up @@ -361,7 +361,7 @@ protected function parseSizeForWebp()
protected function parseSizeForSvg()
{
$this->stream->resetPointer();
$markup = $this->stream->read( 1024 );
$markup = $this->stream->read( 1024, false );
if ( ! preg_match( '#<svg.*?>#s', $markup, $matches ) ) {
return null;
}
Expand Down
23 changes: 15 additions & 8 deletions third_party/fasterimage/Stream/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ class Stream implements StreamableInterface
/**
* Get characters from the string but don't move the pointer
*
* @param $characters
* @param int $characters Number of characters to read. If -1 then it will read until the end.
* @param bool $check_length Throw exception if there are not enough bytes left in the stream.
*
* @return string
* @throws StreamBufferTooSmallException
*/
public function peek($characters)
public function peek($characters, $check_length=true)
{
if ( strlen($this->stream_string) < $this->strpos + $characters ) {
if ( $check_length && strlen($this->stream_string) < $this->strpos + $characters ) {
throw new StreamBufferTooSmallException('Not enough of the stream available.');
}

Expand All @@ -42,16 +43,22 @@ public function peek($characters)
/**
* Get Characters from the string
*
* @param $characters
* @param int $characters Number of characters to read.
* @param bool $check_length Throw exception if there are not enough bytes left in the stream.
*
* @return string
* @throws StreamBufferTooSmallException
*/
public function read($characters)
public function read($characters, $check_length=true)
{
$result = $this->peek($characters);
$size = strlen($this->stream_string);
$result = $this->peek($characters, $check_length);

$this->strpos += $characters;
if ( strlen( $result ) + $characters > $size ) {
$this->strpos = $size;
} else {
$this->strpos += $characters;
}

return $result;
}
Expand All @@ -75,4 +82,4 @@ public function write($string)
{
$this->stream_string .= $string;
}
}
}

0 comments on commit 7528117

Please sign in to comment.