diff --git a/README.md b/README.md index fbdc1b6..037679a 100644 --- a/README.md +++ b/README.md @@ -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 ---------- @@ -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'); @@ -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. diff --git a/src/ImageResize.php b/src/ImageResize.php index 0aac065..eb88cce 100644 --- a/src/ImageResize.php +++ b/src/ImageResize.php @@ -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()); } /** @@ -53,7 +118,7 @@ public function __construct($filename) * @return \static * @throws Exception */ - protected function load($filename) + public function load($filename) { $image_info = getimagesize($filename); @@ -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 */ diff --git a/test/Test.php b/test/Test.php index 012da83..12d1be6 100644 --- a/test/Test.php +++ b/test/Test.php @@ -14,26 +14,45 @@ 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); } /** @@ -41,7 +60,7 @@ public function testLoadPng() { * @expectedExceptionMessage Filename cannot be empty */ public function testLoadNoFile() { - new ImageResize(null); + ImageResize::createFromFile(null); } /** @@ -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');