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

Added type guessing from data / see #99 #100

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion Adapter/GD.php
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions Image.php
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions Source/Data.php
Original file line number Diff line number Diff line change
@@ -23,4 +23,22 @@ public function getInfos()
{
return sha1($this->data);
}

public function guessType()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a PHP doc block.

{
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';
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 19 additions & 2 deletions tests/ImageTests.php
Original file line number Diff line number Diff line change
@@ -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'));
}