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/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/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 f023d35..2bbae76 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. @@ -112,6 +113,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 +461,10 @@ protected function output($file) public function setUp() { $dir = $this->output(''); - `rm -rf $dir`; + $filesystem = new Filesystem(); + + $filesystem->remove($dir); + mkdir($dir); mkdir($this->output('cache')); }