Skip to content

Commit d972116

Browse files
committed
Merge pull request #16 from Sjaakmoes/master
Renamed toString() to get(), implemented __toString()
2 parents ef0505a + 0064b3e commit d972116

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,15 @@ You can also return the result as a string:
102102
```php
103103
$image = ImageResize::createFromString(base64_decode('R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=='));
104104
$image->scale(50);
105-
$result = $image->toString();
105+
$result = $image->get();
106+
```
107+
108+
Magic `__toString()` is also supported:
109+
110+
```php
111+
$image = ImageResize::createFromString(base64_decode('R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=='));
112+
$image->resize(10, 10);
113+
$result = (string)$image;
106114
```
107115

108116
Displaying
@@ -145,7 +153,7 @@ $image->save('image2.jpg');
145153

146154
By default they are set to 75 and 0 respectively. See the manual entries for [`imagejpeg()`](http://www.php.net/manual/en/function.imagejpeg.php) and [`imagepng()`](http://www.php.net/manual/en/function.imagepng.php) for more info.
147155

148-
You can also pass the quality directly to the `save()`, `output()` and `toString()` methods:
156+
You can also pass the quality directly to the `save()`, `output()` and `get()` methods:
149157

150158
```php
151159
$image = new ImageResize('image.jpg');
@@ -158,7 +166,7 @@ $image->output(IMAGETYPE_PNG, 4);
158166

159167
$image = new ImageResize('image.jpg');
160168
$image->scale(50);
161-
$result = $image->toString(IMAGETYPE_PNG, 4);
169+
$result = $image->get(IMAGETYPE_PNG, 4);
162170
```
163171

164172
We're passing `null` for the image type in the example above to skip over it and provide the quality. In this case, the image type is assumed to be the same as the input.

src/ImageResize.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,27 @@ public function save($filename, $image_type = null, $quality = null, $permission
228228
}
229229

230230
/**
231-
* Return image as string
231+
* Get image as string
232232
*
233233
* @param int $image_type
234234
* @param int $quality
235235
* @return string
236236
*/
237-
public function toString($image_type = null, $quality = null){
237+
public function get($image_type = null, $quality = null){
238238
ob_start();
239239
$this->save(null, $image_type, $quality);
240240
return ob_get_clean();
241241
}
242242

243+
/**
244+
* Convert the image to string with the current settings
245+
*
246+
* @return string
247+
*/
248+
public function __toString(){
249+
return $this->get();
250+
}
251+
243252
/**
244253
* Outputs image source to browser
245254
* @param string $image_type

test/Test.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,15 @@ public function testSaveChmod() {
203203
$this->assertEquals(600, substr(decoct(fileperms($filename)), 3));
204204
}
205205

206+
public function testGet(){
207+
$resize = ImageResize::createFromString(base64_decode($this->image_string));
208+
$image = $resize->get();
209+
$this->assertEquals(79, strlen($image));
210+
}
211+
206212
public function testToString(){
207213
$resize = ImageResize::createFromString(base64_decode($this->image_string));
208-
$image = $resize->toString();
214+
$image = (string)$resize;
209215
$this->assertEquals(79, strlen($image));
210216
}
211217

0 commit comments

Comments
 (0)