diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index d3d143f7..5fa2384c 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -79,7 +79,14 @@ class Image extends File */ public function save($path = null, $quality = null) { - $path = is_null($path) ? ($this->dirname .'/'. $this->basename) : $path; + $path = is_null($path) ? $this->basePath() : $path; + + if (is_null($path)) { + throw new Exception\NotWritableException( + "Can't write to undefined path." + ); + } + $data = $this->encode(pathinfo($path, PATHINFO_EXTENSION), $quality); $saved = @file_put_contents($path, $data); @@ -253,6 +260,20 @@ class Image extends File return $this->mime; } + /** + * Get fully qualified path to image + * + * @return string + */ + public function basePath() + { + if ($this->dirname && $this->basename) { + return ($this->dirname .'/'. $this->basename); + } + + return null; + } + /** * Returns encoded image data in string conversion * diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 2c0d1f04..ace7546d 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -57,6 +57,12 @@ class ImageTest extends PHPUnit_Framework_TestCase $this->assertEquals('image/png', $image->mime()); } + public function testBasePath() + { + $image = $this->getTestImage(); + $this->assertEquals('./tmp/foo.png', $image->basePath()); + } + private function getTestImage() { $size = Mockery::mock('\Intervention\Image\Size', array(800, 600)); @@ -67,6 +73,8 @@ class ImageTest extends PHPUnit_Framework_TestCase $driver->shouldReceive('executeCommand')->andReturn($command); $image = new Image($driver, 'mock'); $image->mime = 'image/png'; + $image->dirname = './tmp'; + $image->basename = 'foo.png'; return $image; }