Skip to content

Added some docblocks and support for string-based file loading and saving. #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ $image->save('image2.jpg');

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

Loading and saving images from string
----------

To load an image from a string:

```php
$image = ImageResize::createFromString(base64_decode('R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=='));
$image->scale(50);
$image->save('image.jpg');
```

You can also return the result as a string:

```php
$image = ImageResize::createFromString(base64_decode('R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=='));
$image->scale(50);
$result = $image->toString();
```

Displaying
----------

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

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.

You can also pass the quality directly to the `save()` and `output()` methods:
You can also pass the quality directly to the `save()`, `output()` and `toString()` methods:

```php
$image = new ImageResize('image.jpg');
Expand All @@ -136,6 +155,10 @@ $image->save('image2.jpg', null, 100);
$image = new ImageResize('image.jpg');
$image->resizeToWidth(300);
$image->output(IMAGETYPE_PNG, 4);

$image = new ImageResize('image.jpg');
$image->scale(50);
$result = $image->toString(IMAGETYPE_PNG, 4);
```

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.
Expand Down
90 changes: 84 additions & 6 deletions src/ImageResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,77 @@ class ImageResize
protected $source_h;

/**
* Constructor
* Create instance from a file
*
* @param string $filename
* @return ImageResize
* @throws \Exception
*/
public static function createFromFile($filename){
$s = new self();
$s->load($filename);
return $s;
}

/**
* Create instance from a strng
*
* @param string $imageData
* @return ImageResize
* @throws \exception
*/
public static function createFromString($imageData){
$s = new self();
$s->loadFromString($imageData);
return $s;
}

/**
* Constructor
*
* @param string|null $filename
* @throws \Exception
*/
public function __construct($filename=null)
{
if(!empty($filename)) {
$this->load($filename);
}
}

/**
* Get image size from string
*
* @param string $imagedata
* @return array
*/
protected function getImagesizeFromString($imagedata){
return @getimagesize('data://application/octet-stream;base64,' . base64_encode($imagedata));
}

/**
* Load image from string
*
* @param string $imagedata
* @return ImageResize
* @throws \Exception
*/
public function __construct($filename)
public function loadFromString($imagedata)
{
$this->load($filename);
$image_info = $this->getImagesizeFromString($imagedata);
if(!$image_info) {
throw new \Exception('Could not load image from string');
}

list (
$this->original_w,
$this->original_h,
$this->source_type
) = $image_info;

$this->source_image = imagecreatefromstring($imagedata);

return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
}

/**
Expand All @@ -53,7 +118,7 @@ public function __construct($filename)
* @return \static
* @throws Exception
*/
protected function load($filename)
public function load($filename)
{
$image_info = getimagesize($filename);

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

return $this;
}


/**
* Return image as string
*
* @param int $image_type
* @param int $quality
* @return string
*/
public function toString($image_type = null, $quality = null){
ob_start();
$this->save(null, $image_type, $quality);
return ob_get_clean();
}

/**
* Outpus image source to browser
* Outputs image source to browser
* @param string $image_type
* @param integer $quality
*/
Expand Down
33 changes: 29 additions & 4 deletions test/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,53 @@ class ImageResizeTest extends PHPUnit_Framework_TestCase
);

private $unsupported_image = 'Qk08AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABABAAAAAAAAYAAAASCwAAEgsAAAAAAAAAAAAA/38AAAAA';
private $image_string = 'R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==';

public function testLoadString(){
$resize = ImageResize::createFromString(base64_decode($this->image_string));

$this->assertEquals(IMAGETYPE_GIF, $resize->source_type);
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
}

public function testLoadGif() {
$image = $this->createImage(1, 1, 'gif');
$resize = new ImageResize($image);
$resize = ImageResize::createFromFile($image);

$this->assertEquals(IMAGETYPE_GIF, $resize->source_type);
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
}

public function testLoadJpg() {
$image = $this->createImage(1, 1, 'jpeg');
$resize = new ImageResize($image);
$resize = ImageResize::createFromFile($image);

$this->assertEquals(IMAGETYPE_JPEG, $resize->source_type);
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
}

public function testLoadPng() {
$image = $this->createImage(1, 1, 'png');
$resize = new ImageResize($image);
$resize = ImageResize::createFromFile($image);

$this->assertEquals(IMAGETYPE_PNG, $resize->source_type);
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
}

/**
* @expectedException Exception
* @expectedExceptionMessage Could not load image from string
*/
public function testInvalidString(){
ImageResize::createFromString($this->unsupported_image);
}

/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage Filename cannot be empty
*/
public function testLoadNoFile() {
new ImageResize(null);
ImageResize::createFromFile(null);
}

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

public function testToString(){
$resize = ImageResize::createFromString(base64_decode($this->image_string));
$image = $resize->toString();
$this->assertEquals(79, strlen($image));
}

public function testOutputGif() {
$image = $this->createImage(200, 100, 'gif');

Expand Down