Skip to content

Commit ef0505a

Browse files
committed
Merge pull request #15 from Sjaakmoes/master
Added some docblocks and support for string-based file loading and saving.
2 parents 88dfe8b + 5dcbb68 commit ef0505a

File tree

3 files changed

+137
-11
lines changed

3 files changed

+137
-11
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ $image->save('image2.jpg');
8686

8787
This will cause your image to skew if you do not use the same width/height ratio as the source image.
8888

89+
Loading and saving images from string
90+
----------
91+
92+
To load an image from a string:
93+
94+
```php
95+
$image = ImageResize::createFromString(base64_decode('R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=='));
96+
$image->scale(50);
97+
$image->save('image.jpg');
98+
```
99+
100+
You can also return the result as a string:
101+
102+
```php
103+
$image = ImageResize::createFromString(base64_decode('R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=='));
104+
$image->scale(50);
105+
$result = $image->toString();
106+
```
107+
89108
Displaying
90109
----------
91110

@@ -126,7 +145,7 @@ $image->save('image2.jpg');
126145

127146
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.
128147

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

131150
```php
132151
$image = new ImageResize('image.jpg');
@@ -136,6 +155,10 @@ $image->save('image2.jpg', null, 100);
136155
$image = new ImageResize('image.jpg');
137156
$image->resizeToWidth(300);
138157
$image->output(IMAGETYPE_PNG, 4);
158+
159+
$image = new ImageResize('image.jpg');
160+
$image->scale(50);
161+
$result = $image->toString(IMAGETYPE_PNG, 4);
139162
```
140163

141164
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: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,77 @@ class ImageResize
3939
protected $source_h;
4040

4141
/**
42-
* Constructor
42+
* Create instance from a file
43+
*
4344
* @param string $filename
45+
* @return ImageResize
46+
* @throws \Exception
47+
*/
48+
public static function createFromFile($filename){
49+
$s = new self();
50+
$s->load($filename);
51+
return $s;
52+
}
53+
54+
/**
55+
* Create instance from a strng
56+
*
57+
* @param string $imageData
58+
* @return ImageResize
59+
* @throws \exception
60+
*/
61+
public static function createFromString($imageData){
62+
$s = new self();
63+
$s->loadFromString($imageData);
64+
return $s;
65+
}
66+
67+
/**
68+
* Constructor
69+
*
70+
* @param string|null $filename
71+
* @throws \Exception
72+
*/
73+
public function __construct($filename=null)
74+
{
75+
if(!empty($filename)) {
76+
$this->load($filename);
77+
}
78+
}
79+
80+
/**
81+
* Get image size from string
82+
*
83+
* @param string $imagedata
84+
* @return array
85+
*/
86+
protected function getImagesizeFromString($imagedata){
87+
return @getimagesize('data://application/octet-stream;base64,' . base64_encode($imagedata));
88+
}
89+
90+
/**
91+
* Load image from string
92+
*
93+
* @param string $imagedata
94+
* @return ImageResize
95+
* @throws \Exception
4496
*/
45-
public function __construct($filename)
97+
public function loadFromString($imagedata)
4698
{
47-
$this->load($filename);
99+
$image_info = $this->getImagesizeFromString($imagedata);
100+
if(!$image_info) {
101+
throw new \Exception('Could not load image from string');
102+
}
103+
104+
list (
105+
$this->original_w,
106+
$this->original_h,
107+
$this->source_type
108+
) = $image_info;
109+
110+
$this->source_image = imagecreatefromstring($imagedata);
111+
112+
return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
48113
}
49114

50115
/**
@@ -53,7 +118,7 @@ public function __construct($filename)
53118
* @return \static
54119
* @throws Exception
55120
*/
56-
protected function load($filename)
121+
public function load($filename)
57122
{
58123
$image_info = getimagesize($filename);
59124

@@ -161,9 +226,22 @@ public function save($filename, $image_type = null, $quality = null, $permission
161226

162227
return $this;
163228
}
164-
229+
230+
/**
231+
* Return image as string
232+
*
233+
* @param int $image_type
234+
* @param int $quality
235+
* @return string
236+
*/
237+
public function toString($image_type = null, $quality = null){
238+
ob_start();
239+
$this->save(null, $image_type, $quality);
240+
return ob_get_clean();
241+
}
242+
165243
/**
166-
* Outpus image source to browser
244+
* Outputs image source to browser
167245
* @param string $image_type
168246
* @param integer $quality
169247
*/

test/Test.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,53 @@ class ImageResizeTest extends PHPUnit_Framework_TestCase
1414
);
1515

1616
private $unsupported_image = 'Qk08AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABABAAAAAAAAYAAAASCwAAEgsAAAAAAAAAAAAA/38AAAAA';
17+
private $image_string = 'R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==';
18+
19+
public function testLoadString(){
20+
$resize = ImageResize::createFromString(base64_decode($this->image_string));
21+
22+
$this->assertEquals(IMAGETYPE_GIF, $resize->source_type);
23+
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
24+
}
1725

1826
public function testLoadGif() {
1927
$image = $this->createImage(1, 1, 'gif');
20-
$resize = new ImageResize($image);
28+
$resize = ImageResize::createFromFile($image);
2129

2230
$this->assertEquals(IMAGETYPE_GIF, $resize->source_type);
31+
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
2332
}
2433

2534
public function testLoadJpg() {
2635
$image = $this->createImage(1, 1, 'jpeg');
27-
$resize = new ImageResize($image);
36+
$resize = ImageResize::createFromFile($image);
2837

2938
$this->assertEquals(IMAGETYPE_JPEG, $resize->source_type);
39+
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
3040
}
3141

3242
public function testLoadPng() {
3343
$image = $this->createImage(1, 1, 'png');
34-
$resize = new ImageResize($image);
44+
$resize = ImageResize::createFromFile($image);
3545

3646
$this->assertEquals(IMAGETYPE_PNG, $resize->source_type);
47+
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
48+
}
49+
50+
/**
51+
* @expectedException Exception
52+
* @expectedExceptionMessage Could not load image from string
53+
*/
54+
public function testInvalidString(){
55+
ImageResize::createFromString($this->unsupported_image);
3756
}
3857

3958
/**
4059
* @expectedException PHPUnit_Framework_Error
4160
* @expectedExceptionMessage Filename cannot be empty
4261
*/
4362
public function testLoadNoFile() {
44-
new ImageResize(null);
63+
ImageResize::createFromFile(null);
4564
}
4665

4766
/**
@@ -184,6 +203,12 @@ public function testSaveChmod() {
184203
$this->assertEquals(600, substr(decoct(fileperms($filename)), 3));
185204
}
186205

206+
public function testToString(){
207+
$resize = ImageResize::createFromString(base64_decode($this->image_string));
208+
$image = $resize->toString();
209+
$this->assertEquals(79, strlen($image));
210+
}
211+
187212
public function testOutputGif() {
188213
$image = $this->createImage(200, 100, 'gif');
189214

0 commit comments

Comments
 (0)