From a53d5917362e132a6f22cc6e03252017fc32fd50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Sun, 31 Dec 2017 10:13:44 +0100 Subject: [PATCH] Issue #258 Add toString() method to Image --- src/Image.php | 13 +++++++++++++ tests/ImageTest.php | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Image.php b/src/Image.php index 2d8b4b6..1bf25e4 100644 --- a/src/Image.php +++ b/src/Image.php @@ -144,6 +144,19 @@ public function send($filename = null,$inline = false) return true; } + /** + * Get the raw Image contents (triggers Image creation). + * @return string|bool The Image content as a string or `false` if the + * Image wasn't created successfully. + */ + public function toString() + { + if (!$this->_isCreated && !$this->createImage()) { + return false; + } + return file_get_contents($this->_tmpImageFile->getFileName()); + } + /** * Set options * diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 34d2f24..27f2418 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -102,6 +102,21 @@ public function testCanSetPageFromUrl() unlink($outFile); } + public function testToString() + { + $outFile = $this->getOutFile('png'); + $binary = $this->getBinary(); + + $image = new Image('

Test

'); + $image->binary = $binary; + + $this->assertTrue($image->saveAs($outFile)); + $this->assertFileExists($outFile); + + $this->assertEquals(file_get_contents($outFile), $image->toString()); + unlink($outFile); + } + // Options public function testCanOptionsInConstructor() {