From cedb085cdcbbef8f85f7b1d26646134fc328e2d6 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Wed, 9 Apr 2014 16:47:44 +0200 Subject: [PATCH] rewritten binary check --- src/Intervention/Image/Image.php | 19 ++++++++----------- tests/ImageTest.php | 10 ++++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 5462a44f..07614f11 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -102,16 +102,16 @@ class Image // image properties come from gd image resource $this->initFromResource($source); - } elseif ($this->isBinary($source)) { - - // image properties come from binary image string - $this->initFromString($source); - } elseif (filter_var($source, FILTER_VALIDATE_URL)) { // image will be fetched from url before init $this->initFromString(file_get_contents($source)); + } elseif ($this->isBinary($source)) { + + // image properties come from binary image string + $this->initFromString($source); + } else { // image properties come from image file @@ -1791,18 +1791,15 @@ class Image } /** - * Checks if string contains printable characters + * Checks if string contains binary image data * * @param mixed $input * @return boolean */ private function isBinary($input) { - if (is_resource($input)) { - return false; - } - - return ( ! ctype_print($input)); + $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), (string) $input); + return substr($mime, 0, 4) != 'text'; } /** diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 19e82e04..ea7d6b05 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -64,6 +64,16 @@ class ImageTest extends PHPUnit_Framework_Testcase $img = new Image('public/foo/bar/invalid_image_path.jpg'); } + /** + * @expectedException Intervention\Image\Exception\ImageNotFoundException + */ + public function testConstructorWithNonAsciiCharacters() + { + // file does not exists but path string should NOT be considered + // as binary data. (should _NOT_ throw InvalidImageDataStringException) + $img = new Image('public/Über.jpg'); + } + /** * @expectedException Intervention\Image\Exception\InvalidImageTypeException */