Skip to content

Commit a53d591

Browse files
committed
Issue #258 Add toString() method to Image
1 parent dccea43 commit a53d591

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Image.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ public function send($filename = null,$inline = false)
144144
return true;
145145
}
146146

147+
/**
148+
* Get the raw Image contents (triggers Image creation).
149+
* @return string|bool The Image content as a string or `false` if the
150+
* Image wasn't created successfully.
151+
*/
152+
public function toString()
153+
{
154+
if (!$this->_isCreated && !$this->createImage()) {
155+
return false;
156+
}
157+
return file_get_contents($this->_tmpImageFile->getFileName());
158+
}
159+
147160
/**
148161
* Set options
149162
*

tests/ImageTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ public function testCanSetPageFromUrl()
102102
unlink($outFile);
103103
}
104104

105+
public function testToString()
106+
{
107+
$outFile = $this->getOutFile('png');
108+
$binary = $this->getBinary();
109+
110+
$image = new Image('<html><h1>Test</h1></html>');
111+
$image->binary = $binary;
112+
113+
$this->assertTrue($image->saveAs($outFile));
114+
$this->assertFileExists($outFile);
115+
116+
$this->assertEquals(file_get_contents($outFile), $image->toString());
117+
unlink($outFile);
118+
}
119+
105120
// Options
106121
public function testCanOptionsInConstructor()
107122
{

0 commit comments

Comments
 (0)