From 1c7e6588ead98ab4d3c54c6781939f6f837d3ef2 Mon Sep 17 00:00:00 2001 From: Freek Post Date: Mon, 26 Jan 2015 14:34:34 +0100 Subject: [PATCH 1/6] Add support for string-based file loading and saving. --- .gitignore | 224 ++++++++++++++++++++++++++++++++++++++++++++ src/ImageResize.php | 102 +++++++++++++++++--- test/Test.php | 33 ++++++- 3 files changed, 344 insertions(+), 15 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4181702 --- /dev/null +++ b/.gitignore @@ -0,0 +1,224 @@ +################# +## PHPStorm +################# +.idea/ + +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml +*.publishproj + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + + +############# +## Python +############# + +*.py[cod] + +# Packages +*.egg +*.egg-info +dist/ +build/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg +.idea/deployment.xml +.idea/php.xml +.idea/vagrant.xml diff --git a/src/ImageResize.php b/src/ImageResize.php index 0aac065..7c09072 100644 --- a/src/ImageResize.php +++ b/src/ImageResize.php @@ -39,21 +39,87 @@ 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) + public function __construct($filename=null) { - $this->load($filename); + 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)); } /** - * Loads image source and its properties to the instanciated object + * Load image from string + * + * @param string $imagedata + * @return ImageResize + * @throws \Exception + */ + public function loadFromString($imagedata) + { + $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()); + } + + /** + * Load a file + * * @param string $filename - * @return \static - * @throws Exception + * @return ImageResize + * @throws \Exception */ - protected function load($filename) + public function load($filename) { $image_info = getimagesize($filename); @@ -161,11 +227,25 @@ public function save($filename, $image_type = null, $quality = null, $permission return $this; } - + /** - * Outpus image source to browser - * @param string $image_type - * @param integer $quality + * 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(); + } + + /** + * Outputs image source to browser + * + * @param int $image_type + * @param int $quality */ public function output($image_type = null, $quality = null) { diff --git a/test/Test.php b/test/Test.php index 012da83..293b442 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('R0lGODlhDwAPAJEAAAQCzMzO/Pz+/P///yH5BAEAAAMALAAAAAAPAA8AAAIghI95Aeytnkyh2suU3jwJcXybaJDdiaYqpWVG1bgwvBYAOw==', base64_encode($image)); + } + public function testOutputGif() { $image = $this->createImage(200, 100, 'gif'); From bed959f34fd2bb4e972dfa2bd38901f181d9ed77 Mon Sep 17 00:00:00 2001 From: Freek Post Date: Mon, 26 Jan 2015 14:45:47 +0100 Subject: [PATCH 2/6] Docblock --- src/ImageResize.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ImageResize.php b/src/ImageResize.php index 7c09072..3e8da04 100644 --- a/src/ImageResize.php +++ b/src/ImageResize.php @@ -7,6 +7,11 @@ /** * Handles thumb image(s) according to the original source given. */ +/** + * Image Resize + * + * @package Eventviva + */ class ImageResize { const cropTOP = 1; From 92d7f34ef3fd988d77e9cc04f8792d9bf88ca1a9 Mon Sep 17 00:00:00 2001 From: Freek Post Date: Mon, 26 Jan 2015 14:55:53 +0100 Subject: [PATCH 3/6] Fixed string test --- test/Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Test.php b/test/Test.php index 293b442..12d1be6 100644 --- a/test/Test.php +++ b/test/Test.php @@ -206,7 +206,7 @@ public function testSaveChmod() { public function testToString(){ $resize = ImageResize::createFromString(base64_decode($this->image_string)); $image = $resize->toString(); - $this->assertEquals('R0lGODlhDwAPAJEAAAQCzMzO/Pz+/P///yH5BAEAAAMALAAAAAAPAA8AAAIghI95Aeytnkyh2suU3jwJcXybaJDdiaYqpWVG1bgwvBYAOw==', base64_encode($image)); + $this->assertEquals(79, strlen($image)); } public function testOutputGif() { From 5a860d7bf58c6026be66363644742352c78ffe68 Mon Sep 17 00:00:00 2001 From: Freek Post Date: Mon, 26 Jan 2015 15:23:40 +0100 Subject: [PATCH 4/6] Updated readme and removed gitignore --- .gitignore | 224 ----------------------------------------------------- README.md | 25 +++++- 2 files changed, 24 insertions(+), 225 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 4181702..0000000 --- a/.gitignore +++ /dev/null @@ -1,224 +0,0 @@ -################# -## PHPStorm -################# -.idea/ - -################# -## Eclipse -################# - -*.pydevproject -.project -.metadata -bin/ -tmp/ -*.tmp -*.bak -*.swp -*~.nib -local.properties -.classpath -.settings/ -.loadpath - -# External tool builders -.externalToolBuilders/ - -# Locally stored "Eclipse launch configurations" -*.launch - -# CDT-specific -.cproject - -# PDT-specific -.buildpath - - -################# -## Visual Studio -################# - -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. - -# User-specific files -*.suo -*.user -*.sln.docstates - -# Build results - -[Dd]ebug/ -[Rr]elease/ -x64/ -build/ -[Bb]in/ -[Oo]bj/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -*_i.c -*_p.c -*.ilk -*.meta -*.obj -*.pch -*.pdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.log -*.scc - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opensdf -*.sdf -*.cachefile - -# Visual Studio profiler -*.psess -*.vsp -*.vspx - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# NCrunch -*.ncrunch* -.*crunch*.local.xml - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.Publish.xml -*.pubxml -*.publishproj - -# NuGet Packages Directory -## TODO: If you have NuGet Package Restore enabled, uncomment the next line -#packages/ - -# Windows Azure Build Output -csx -*.build.csdef - -# Windows Store app package directory -AppPackages/ - -# Others -sql/ -*.Cache -ClientBin/ -[Ss]tyle[Cc]op.* -~$* -*~ -*.dbmdl -*.[Pp]ublish.xml -*.pfx -*.publishsettings - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file to a newer -# Visual Studio version. Backup files are not needed, because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm - -# SQL Server files -App_Data/*.mdf -App_Data/*.ldf - -############# -## Windows detritus -############# - -# Windows image file caches -Thumbs.db -ehthumbs.db - -# Folder config file -Desktop.ini - -# Recycle Bin used on file shares -$RECYCLE.BIN/ - -# Mac crap -.DS_Store - - -############# -## Python -############# - -*.py[cod] - -# Packages -*.egg -*.egg-info -dist/ -build/ -eggs/ -parts/ -var/ -sdist/ -develop-eggs/ -.installed.cfg - -# Installer logs -pip-log.txt - -# Unit test / coverage reports -.coverage -.tox - -#Translations -*.mo - -#Mr Developer -.mr.developer.cfg -.idea/deployment.xml -.idea/php.xml -.idea/vagrant.xml 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. From da4e0e60cd2da247516460fe9c07eff804b73f92 Mon Sep 17 00:00:00 2001 From: Freek Post Date: Mon, 26 Jan 2015 16:37:29 +0100 Subject: [PATCH 5/6] Merged --- src/ImageResize.php | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/ImageResize.php b/src/ImageResize.php index 3e8da04..520e48c 100644 --- a/src/ImageResize.php +++ b/src/ImageResize.php @@ -7,11 +7,6 @@ /** * Handles thumb image(s) according to the original source given. */ -/** - * Image Resize - * - * @package Eventviva - */ class ImageResize { const cropTOP = 1; @@ -68,7 +63,7 @@ public static function createFromString($imageData){ $s->loadFromString($imageData); return $s; } - + /** * Constructor * @@ -77,7 +72,7 @@ public static function createFromString($imageData){ */ public function __construct($filename=null) { - if(!empty($filename)){ + if(!empty($filename)) { $this->load($filename); } } @@ -118,11 +113,10 @@ public function loadFromString($imagedata) } /** - * Load a file - * + * Loads image source and its properties to the instanciated object * @param string $filename - * @return ImageResize - * @throws \Exception + * @return \static + * @throws Exception */ public function load($filename) { @@ -247,10 +241,9 @@ public function toString($image_type = null, $quality = null){ } /** - * Outputs image source to browser - * - * @param int $image_type - * @param int $quality + * Outpus image source to browser + * @param string $image_type + * @param integer $quality */ public function output($image_type = null, $quality = null) { From 5dcbb689292548eaa7d7d91d059cba539b11a7d5 Mon Sep 17 00:00:00 2001 From: Freek Post Date: Mon, 26 Jan 2015 16:58:02 +0100 Subject: [PATCH 6/6] Merge --- src/ImageResize.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageResize.php b/src/ImageResize.php index 520e48c..eb88cce 100644 --- a/src/ImageResize.php +++ b/src/ImageResize.php @@ -241,7 +241,7 @@ public function toString($image_type = null, $quality = null){ } /** - * Outpus image source to browser + * Outputs image source to browser * @param string $image_type * @param integer $quality */