From 6ddda6431f26bbb6a97dcdc23030f3fc96609d2f Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Wed, 22 Apr 2015 14:43:29 +0000 Subject: [PATCH 1/3] add option for absolute path --- src/Spatie/Glide/GlideImage.php | 36 +++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Spatie/Glide/GlideImage.php b/src/Spatie/Glide/GlideImage.php index 01571d6..12986c2 100644 --- a/src/Spatie/Glide/GlideImage.php +++ b/src/Spatie/Glide/GlideImage.php @@ -7,7 +7,7 @@ class GlideImage { - protected $imagePath; + protected $sourceFile; protected $signKey; @@ -15,16 +15,18 @@ class GlideImage protected $modificationParameters = []; + protected $useAbsolutePath = false; + /** * Set the path to the image that needs to be converted * - * @param $imagePath + * @param $sourceFile * @param array $modificationParameters = [] * @return $this */ - public function load($imagePath, $modificationParameters = []) + public function load($sourceFile, $modificationParameters = []) { - $this->imagePath = $imagePath; + $this->sourceFile = $sourceFile; $this->modify($modificationParameters); @@ -82,7 +84,7 @@ public function getURL() { $urlBuilder = UrlBuilderFactory::create($this->baseURL, $this->signKey); - $encodedPath = implode('/', array_map('rawurlencode', explode('/', $this->imagePath))); + $encodedPath = implode('/', array_map('rawurlencode', explode('/', $this->sourceFile))); return $urlBuilder->getUrl($encodedPath, $this->modificationParameters); } @@ -96,9 +98,7 @@ public function save($outputFile) { $glideApi = GlideApiFactory::create(); - $inputImageData = file_get_contents(Config::get('laravel-glide.source.path').'/'.$this->imagePath); - - $outputImageData = $glideApi->run(Request::create(null, null, $this->modificationParameters), $inputImageData); + $outputImageData = $glideApi->run(Request::create(null, null, $this->modificationParameters), $this->getPathToImage()); file_put_contents($outputFile, $outputImageData); @@ -129,4 +129,24 @@ public function convertParametersToString($modificationParameters) }, $modificationParameters); } + + public function useAbsoluteSourceFilePath() + { + $this->useAbsolutePath = true; + } + + /** + * Get the path to the image + * + * @return string + */ + private function getPathToImage() + { + if( $this->useAbsolutePath) + { + return file_get_contents($this->sourceFile); + } + + return file_get_contents(Config::get('laravel-glide.source.path').'/'.$this->sourceFile); + } } From a42c952e53970aff112cc49a98831a8541292ff3 Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Wed, 22 Apr 2015 14:46:05 +0000 Subject: [PATCH 2/3] make method chainable --- src/Spatie/Glide/GlideImage.php | 7 +++++++ tests/unit/GlideImageTest.php | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Spatie/Glide/GlideImage.php b/src/Spatie/Glide/GlideImage.php index 12986c2..3693bea 100644 --- a/src/Spatie/Glide/GlideImage.php +++ b/src/Spatie/Glide/GlideImage.php @@ -130,9 +130,16 @@ public function convertParametersToString($modificationParameters) } + /** + * Use an absolute path to the sourceFile (instead of using config source) + * + * @return $this + */ public function useAbsoluteSourceFilePath() { $this->useAbsolutePath = true; + + return $this; } /** diff --git a/tests/unit/GlideImageTest.php b/tests/unit/GlideImageTest.php index 4093c24..64bb60c 100644 --- a/tests/unit/GlideImageTest.php +++ b/tests/unit/GlideImageTest.php @@ -27,11 +27,11 @@ public function testImagePath() { $glide = $this->_before(); - $imagePath = 'testFile.jpg'; + $sourceFile = 'testFile.jpg'; - $glide->load($imagePath); + $glide->load($sourceFile); - $this->assertAttributeContains('testFile.jpg', 'imagePath', $glide); + $this->assertAttributeContains('testFile.jpg', 'sourceFile', $glide); } public function testBaseURL() From c65190d4e0ca8ed37e72a4fdc0519922073f6a88 Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Wed, 22 Apr 2015 14:53:22 +0000 Subject: [PATCH 3/3] make method clearer --- src/Spatie/Glide/GlideImage.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Spatie/Glide/GlideImage.php b/src/Spatie/Glide/GlideImage.php index 3693bea..d40f161 100644 --- a/src/Spatie/Glide/GlideImage.php +++ b/src/Spatie/Glide/GlideImage.php @@ -98,7 +98,7 @@ public function save($outputFile) { $glideApi = GlideApiFactory::create(); - $outputImageData = $glideApi->run(Request::create(null, null, $this->modificationParameters), $this->getPathToImage()); + $outputImageData = $glideApi->run(Request::create(null, null, $this->modificationParameters), file_get_contents($this->getPathToImage())); file_put_contents($outputFile, $outputImageData); @@ -151,9 +151,9 @@ private function getPathToImage() { if( $this->useAbsolutePath) { - return file_get_contents($this->sourceFile); + return $this->sourceFile; } - return file_get_contents(Config::get('laravel-glide.source.path').'/'.$this->sourceFile); + return Config::get('laravel-glide.source.path').'/'.$this->sourceFile; } }