Skip to content

Commit abc34dd

Browse files
committed
Added custom write directory
1 parent 4cff854 commit abc34dd

File tree

4 files changed

+108
-19
lines changed

4 files changed

+108
-19
lines changed

src/Folklore/Image/ImageManager.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ public function serve($path, $config = array())
218218
$config = array_merge(array(
219219
'custom_filters_only' => $this->app['config']['image.serve_custom_filters_only'],
220220
'write_image' => $this->app['config']['image.write_image'],
221+
'write_path' => $this->app['config']['image.write_path'],
221222
'options' => array()
222223
),$config);
223224

@@ -252,7 +253,8 @@ public function serve($path, $config = array())
252253
//Write the image
253254
if ($config['write_image'])
254255
{
255-
$destinationPath = dirname($path).'/'.basename($path);
256+
$destinationFolder = isset($config['write_path']) ? $config['write_path']:dirname($path);
257+
$destinationPath = rtrim($destinationFolder, '/').'/'.basename($path);
256258
$image->save($destinationPath);
257259
}
258260

@@ -584,18 +586,26 @@ protected function checkForFile($path) {
584586
if (is_file($path)) {
585587
return $path;
586588
}
587-
588-
// Loop through all the directories files may be uploaded to
589+
590+
//Get directories
589591
$dirs = $this->app['config']['image.src_dirs'];
590-
foreach($dirs as $dir) {
592+
if($this->app['config']['image.write_path'])
593+
{
594+
$dirs[] = $this->app['config']['image.write_path'];
595+
}
591596

597+
// Loop through all the directories files may be uploaded to
598+
foreach($dirs as $dir)
599+
{
600+
$dir = rtrim($dir, '/');
601+
592602
// Check that directory exists
593603
if (!is_dir($dir)) continue;
594-
if (substr($dir, -1, 1) != '/') $dir .= '/';
595604

596605
// Look for the image in the directory
597-
$src = realpath($dir.$path);
598-
if (is_file($src)) {
606+
$src = realpath($dir.'/'.ltrim($path, '/'));
607+
if (is_file($src))
608+
{
599609
return $src;
600610
}
601611
}
@@ -628,15 +638,24 @@ protected function getFiles($path, $withOriginal = true)
628638
$images[] = $path;
629639
}
630640

631-
// Loop through the contents of the source directory and get
641+
// Loop through the contents of the source and write directory and get
632642
// all files that match the pattern
633643
$parts = pathinfo($path);
634-
$files = scandir($parts['dirname']);
635-
foreach($files as $file) {
636-
if (strpos($file, $parts['filename']) === false || !preg_match('#'.$this->pattern().'#', $file)) continue;
637-
$images[] = $parts['dirname'].'/'.$file;
644+
$dirs = [$parts['dirname']];
645+
$dirs = [$parts['dirname']];
646+
if($this->app['config']['image.write_path'])
647+
{
648+
$dirs[] = $this->app['config']['image.write_path'];
638649
}
639-
650+
foreach($dirs as $directory)
651+
{
652+
$files = scandir($directory);
653+
foreach($files as $file) {
654+
if (strpos($file, $parts['filename']) === false || !preg_match('#'.$this->pattern().'#', $file)) continue;
655+
$images[] = $directory.'/'.$file;
656+
}
657+
}
658+
640659
// Return the list
641660
return $images;
642661
}

src/resources/config/image.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@
105105
*/
106106
'write_image' => false,
107107

108+
/*
109+
|--------------------------------------------------------------------------
110+
| Write path
111+
|--------------------------------------------------------------------------
112+
|
113+
| By default, the manipulated images are saved in the same path as the
114+
| as the original image, you can override this path here
115+
|
116+
*/
117+
'write_path' => null,
118+
108119
/*
109120
|--------------------------------------------------------------------------
110121
| Memory limit

tests/ImageTestCase.php

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ class ImageTestCase extends TestCase {
1414
public function setUp()
1515
{
1616
parent::setUp();
17-
18-
$this->app->instance('path.public', __DIR__.'/fixture');
19-
20-
Image::deleteManipulated($this->imagePath);
2117

2218
$this->imageSize = getimagesize(public_path().$this->imagePath);
2319
$this->imageSmallSize = getimagesize(public_path().$this->imageSmallPath);
2420
}
2521

26-
22+
public function tearDown()
23+
{
24+
$customPath = $this->app['path.public'].'/custom';
25+
$this->app['config']->set('image.write_path', $customPath);
26+
Image::deleteManipulated($this->imagePath);
27+
28+
parent::tearDown();
29+
}
2730

2831
public function testURLisValid()
2932
{
@@ -75,6 +78,51 @@ public function testURLisValid()
7578
}
7679
}
7780

81+
public function testServeWriteImage()
82+
{
83+
$this->app['config']->set('image.write_image', true);
84+
85+
$url = Image::url($this->imagePath, 300, 300, [
86+
'crop' => true
87+
]);
88+
$response = $this->call('GET', $url);
89+
90+
$this->assertTrue($response->isOk());
91+
92+
$imagePath = $this->app['path.public'].'/'.basename($url);
93+
$this->assertFileExists($imagePath);
94+
95+
$sizeManipulated = getimagesize($imagePath);
96+
$this->assertEquals($sizeManipulated[0], 300);
97+
$this->assertEquals($sizeManipulated[1], 300);
98+
99+
$this->app['config']->set('image.write_image', false);
100+
}
101+
102+
public function testServeWriteImagePath()
103+
{
104+
$customPath = $this->app['path.public'].'/custom';
105+
$this->app['config']->set('image.write_image', true);
106+
$this->app['config']->set('image.write_path', $customPath);
107+
108+
$url = Image::url($this->imagePath, 300, 300, [
109+
'crop' => true
110+
]);
111+
$response = $this->call('GET', $url);
112+
113+
$this->assertTrue($response->isOk());
114+
115+
$imagePath = $customPath.'/'.basename($url);
116+
$this->assertFileExists($imagePath);
117+
118+
$sizeManipulated = getimagesize($imagePath);
119+
$this->assertEquals($sizeManipulated[0], 300);
120+
$this->assertEquals($sizeManipulated[1], 300);
121+
122+
$this->app['config']->set('image.write_image', false);
123+
$this->app['config']->set('image.write_path', null);
124+
}
125+
78126
public function testServeNoResize()
79127
{
80128

@@ -171,7 +219,16 @@ public function testServeWrongFormat()
171219
$response = $this->call('GET', $url);
172220
}
173221

174-
222+
/**
223+
* Define environment setup.
224+
*
225+
* @param \Illuminate\Foundation\Application $app
226+
* @return void
227+
*/
228+
protected function getEnvironmentSetUp($app)
229+
{
230+
$app->instance('path.public', __DIR__.'/fixture');
231+
}
175232

176233
protected function getPackageProviders($app)
177234
{

tests/fixture/custom/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

0 commit comments

Comments
 (0)