From 97e365e7699414f9454ba32df31f15e2e38a92fc Mon Sep 17 00:00:00 2001 From: nebulousGirl Date: Sun, 8 Nov 2015 18:26:18 -0500 Subject: [PATCH 1/3] Added type guessing from data / see #99 --- Source/Data.php | 18 ++++++++++++++++++ tests/ImageTests.php | 20 +++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Source/Data.php b/Source/Data.php index 5fd9f67..846b80d 100644 --- a/Source/Data.php +++ b/Source/Data.php @@ -23,4 +23,22 @@ public function getInfos() { return sha1($this->data); } + + public function guessType() + { + if (class_exists('finfo')) { + $finfo = new \finfo(FILEINFO_MIME_TYPE); + $mime = $finfo->buffer($this->data); + switch ($mime) { + case 'image/gif': + return 'gif'; + case 'image/png': + return 'png'; + default: + return 'jpeg'; + } + } + + return 'jpeg'; + } } diff --git a/tests/ImageTests.php b/tests/ImageTests.php index f023d35..af986b6 100755 --- a/tests/ImageTests.php +++ b/tests/ImageTests.php @@ -112,6 +112,19 @@ public function testGuess() $this->assertSame('gif', $image->guessType()); } + /** + * Testing type guess from image data + */ + public function testGuessFromData() + { + $image = Image::fromData(file_get_contents(__DIR__.'/files/monalisa.gif')); + $this->assertSame('gif', $image->guessType()); + $image = Image::fromData(file_get_contents(__DIR__.'/files/monalisa.png')); + $this->assertSame('png', $image->guessType()); + $image = Image::fromData(file_get_contents(__DIR__.'/files/monalisa.jpg')); + $this->assertSame('jpeg', $image->guessType()); + } + public function testDefaultCacheSystem() { $image = $this->open('monalisa.jpg'); @@ -447,7 +460,12 @@ protected function output($file) public function setUp() { $dir = $this->output(''); - `rm -rf $dir`; + + if (PHP_OS === 'WINNT') { + `rd /s /q "$dir"`; + } else { + `rm -rf $dir`; + } mkdir($dir); mkdir($this->output('cache')); } From 4d6840ec0f3ddd784d7ba333167b701e962e8935 Mon Sep 17 00:00:00 2001 From: Sonia Marquette Date: Sat, 12 Mar 2016 10:54:35 -0500 Subject: [PATCH 2/3] Corrected StyleCI issues and added Filesystem to delete test files --- composer.json | 3 ++- tests/ImageTests.php | 15 +++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 7ea5292..3f0c558 100755 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ }, "require-dev": { "sllh/php-cs-fixer-styleci-bridge": "~1.0", - "symfony/phpunit-bridge": "^2.7.4" + "symfony/phpunit-bridge": "^2.7.4", + "symfony/filesystem": "^2.7.4" }, "suggest": { "behat/transliterator": "Transliterator provides ability to set non-latin1 pretty names" diff --git a/tests/ImageTests.php b/tests/ImageTests.php index af986b6..aa0dfb7 100755 --- a/tests/ImageTests.php +++ b/tests/ImageTests.php @@ -2,11 +2,12 @@ use Gregwar\Image\Image; use Gregwar\Image\ImageColor; +use Symfony\Component\Filesystem\Filesystem; /** * Unit testing for Image. */ -class ImageTests extends \PHPUnit_Framework_TestCase +class ImageTest extends \PHPUnit_Framework_TestCase { /** * Testing the basic width & height. @@ -113,7 +114,7 @@ public function testGuess() } /** - * Testing type guess from image data + * Testing type guess from image data. */ public function testGuessFromData() { @@ -460,12 +461,10 @@ protected function output($file) public function setUp() { $dir = $this->output(''); - - if (PHP_OS === 'WINNT') { - `rd /s /q "$dir"`; - } else { - `rm -rf $dir`; - } + $filesystem = new Filesystem; + + $filesystem->remove($dir); + mkdir($dir); mkdir($this->output('cache')); } From 179b035dbc6f30bbb1405c48d9592470010e48ca Mon Sep 17 00:00:00 2001 From: nebulousGirl Date: Sat, 12 Mar 2016 10:58:54 -0500 Subject: [PATCH 3/3] Corrected some more StyleCI issues --- Adapter/AdapterInterface.php | 4 ++-- Adapter/GD.php | 2 +- Image.php | 4 ++-- tests/ImageTests.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Adapter/AdapterInterface.php b/Adapter/AdapterInterface.php index d9866fa..fa4312c 100644 --- a/Adapter/AdapterInterface.php +++ b/Adapter/AdapterInterface.php @@ -377,10 +377,10 @@ public function polygon(array $points, $color, $filled = false); /** * Flips the image. - * + * * @param int $flipVertical * @param int $flipHorizontal - * + * * @return $this */ public function flip($flipVertical, $flipHorizontal); diff --git a/Adapter/GD.php b/Adapter/GD.php index 4bd156c..b0fcac7 100644 --- a/Adapter/GD.php +++ b/Adapter/GD.php @@ -561,7 +561,7 @@ protected function openPng($file) */ protected function supports($type) { - return (imagetypes() & self::$gdTypes[$type]); + return imagetypes() & self::$gdTypes[$type]; } protected function getColor($x, $y) diff --git a/Image.php b/Image.php index 4b60df2..7b99117 100644 --- a/Image.php +++ b/Image.php @@ -671,10 +671,10 @@ public function save($file, $type = 'guess', $quality = 80) return false; } - return (null === $file ? ob_get_clean() : $file); + return null === $file ? ob_get_clean() : $file; } catch (\Exception $e) { if ($this->useFallbackImage) { - return (null === $file ? file_get_contents($this->fallback) : $this->getCacheFallback()); + return null === $file ? file_get_contents($this->fallback) : $this->getCacheFallback(); } else { throw $e; } diff --git a/tests/ImageTests.php b/tests/ImageTests.php index aa0dfb7..2bbae76 100755 --- a/tests/ImageTests.php +++ b/tests/ImageTests.php @@ -461,7 +461,7 @@ protected function output($file) public function setUp() { $dir = $this->output(''); - $filesystem = new Filesystem; + $filesystem = new Filesystem(); $filesystem->remove($dir);