Skip to content

Commit

Permalink
Merge pull request #25 from MatthiasDeWinter/master
Browse files Browse the repository at this point in the history
Add the option for using an absolute path to a source file.
  • Loading branch information
freekmurze committed Apr 22, 2015
2 parents 9e13ac7 + c65190d commit 66a48c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
43 changes: 35 additions & 8 deletions src/Spatie/Glide/GlideImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@

class GlideImage
{
protected $imagePath;
protected $sourceFile;

protected $signKey;

protected $baseURL;

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);

Expand Down Expand Up @@ -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);
}

Expand All @@ -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), file_get_contents($this->getPathToImage()));

file_put_contents($outputFile, $outputImageData);

Expand Down Expand Up @@ -129,4 +129,31 @@ public function convertParametersToString($modificationParameters)
}, $modificationParameters);

}

/**
* Use an absolute path to the sourceFile (instead of using config source)
*
* @return $this
*/
public function useAbsoluteSourceFilePath()
{
$this->useAbsolutePath = true;

return $this;
}

/**
* Get the path to the image
*
* @return string
*/
private function getPathToImage()
{
if( $this->useAbsolutePath)
{
return $this->sourceFile;
}

return Config::get('laravel-glide.source.path').'/'.$this->sourceFile;
}
}
6 changes: 3 additions & 3 deletions tests/unit/GlideImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 66a48c7

Please sign in to comment.