Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix reading of small SVG images to determine dimensions #1807

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
* @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;
}
}
}
12 changes: 7 additions & 5 deletions third_party/fasterimage/Stream/StreamableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@ public function write($string);
/**
* 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.
*/
public function read($characters);
public function read($characters, $check_length=false);

/**
* Get characters from the string but don't move the pointer
*
* @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 mixed
*/
public function peek($characters);
public function peek($characters, $check_length=false);

/**
* Resets the pointer to the 0 position
* @return mixed
*/
public function resetPointer();

}
}