diff --git a/src/Intervention/Image/File.php b/src/Intervention/Image/File.php index 2d122af3..c354e180 100644 --- a/src/Intervention/Image/File.php +++ b/src/Intervention/Image/File.php @@ -18,6 +18,10 @@ class File $this->extension = array_key_exists('extension', $info) ? $info['extension'] : null; $this->filename = array_key_exists('filename', $info) ? $info['filename'] : null; + if (file_exists($path)) { + $this->mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path); + } + return $this; } } diff --git a/src/Intervention/Image/Imagick/Commands/ResizeCanvasCommand.php b/src/Intervention/Image/Imagick/Commands/ResizeCanvasCommand.php index 214ed0f3..6316fe83 100644 --- a/src/Intervention/Image/Imagick/Commands/ResizeCanvasCommand.php +++ b/src/Intervention/Image/Imagick/Commands/ResizeCanvasCommand.php @@ -58,6 +58,16 @@ class ResizeCanvasCommand extends \Intervention\Image\Commands\AbstractCommand $src_h = $original_height; } + // make image area transparent to keep transparency + // even if background-color is set + $rect = new \ImagickDraw; + $fill = $canvas->pickColor(0, 0, 'hex'); + $fill = $fill == '#ff0000' ? '#00ff00' : '#ff0000'; + $rect->setFillColor($fill); + $rect->rectangle($dst_x, $dst_y, $dst_x + $src_w - 1, $dst_y + $src_h - 1); + $canvas->getCore()->drawImage($rect); + $canvas->getCore()->paintTransparentImage($fill, 0, 0); + // copy image into new canvas $image->getCore()->cropImage($src_w, $src_h, $src_x, $src_y); $canvas->getCore()->compositeImage($image->getCore(), \Imagick::COMPOSITE_DEFAULT, $dst_x, $dst_y); diff --git a/src/Intervention/Image/Imagick/Source.php b/src/Intervention/Image/Imagick/Source.php index ccd5fee9..fc54f4d1 100644 --- a/src/Intervention/Image/Imagick/Source.php +++ b/src/Intervention/Image/Imagick/Source.php @@ -24,7 +24,6 @@ class Source extends \Intervention\Image\AbstractSource // build image $image = $this->initFromImagick($core); - $image->mime = $this->getMime($core); $image->setFileInfoFromPath($path); return $image; diff --git a/tests/FileTest.php b/tests/FileTest.php index 90a2beb6..d9fc15d9 100644 --- a/tests/FileTest.php +++ b/tests/FileTest.php @@ -7,10 +7,11 @@ class FileTest extends PHPUnit_Framework_TestCase public function testSetFileInfoFromPath() { $file = new File; - $file->setFileInfoFromPath('test/foo/bar.baz'); - $this->assertEquals('test/foo', $file->dirname); - $this->assertEquals('bar.baz', $file->basename); - $this->assertEquals('baz', $file->extension); - $this->assertEquals('bar', $file->filename); + $file->setFileInfoFromPath('tests/images/test.jpg'); + $this->assertEquals('tests/images', $file->dirname); + $this->assertEquals('test.jpg', $file->basename); + $this->assertEquals('jpg', $file->extension); + $this->assertEquals('test', $file->filename); + $this->assertEquals('image/jpeg', $file->mime); } } diff --git a/tests/ResizeCanvasCommandTest.php b/tests/ResizeCanvasCommandTest.php index 152fde1f..e8f8866a 100644 --- a/tests/ResizeCanvasCommandTest.php +++ b/tests/ResizeCanvasCommandTest.php @@ -54,9 +54,12 @@ class ResizeCanvasCommandTest extends PHPUnit_Framework_TestCase $imagick->shouldReceive('cropimage')->with(800, 600, 0, 0)->once(); $imagick->shouldReceive('compositeimage')->with($imagick, 40, 0, 0)->once(); $imagick->shouldReceive('setimagepage')->with(0, 0, 0, 0)->once(); + $imagick->shouldReceive('drawimage')->once(); + $imagick->shouldReceive('painttransparentimage')->once(); - $canvas->shouldReceive('getCore')->times(3)->andReturn($imagick); + $canvas->shouldReceive('getCore')->times(5)->andReturn($imagick); $canvas->shouldReceive('getSize')->andReturn($canvas_size); + $canvas->shouldReceive('pickColor')->with(0, 0, 'hex')->once()->andReturn('#000000'); $driver = Mockery::mock('\Intervention\Image\Gd\Driver'); $driver->shouldReceive('newImage')->with(820, 640, '#b53717')->once()->andReturn($canvas); $image = Mockery::mock('Intervention\Image\Image');