Commit e0b8c32f authored by Nicolas Widart's avatar Nicolas Widart

Adding a thumbnailManager

parent e8838c90
...@@ -24,7 +24,7 @@ class ImageServiceProvider extends ServiceProvider ...@@ -24,7 +24,7 @@ class ImageServiceProvider extends ServiceProvider
); );
$this->app['imagy'] = $this->app->share(function ($app) { $this->app['imagy'] = $this->app->share(function ($app) {
return new Imagy($app['config'], new InterventionFactory); return new Imagy(new InterventionFactory, new ThumbnailsManager($app['config'], $app['modules']));
}); });
$this->app->booting(function() $this->app->booting(function()
......
<?php namespace Modules\Media\Image; <?php namespace Modules\Media\Image;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Contracts\Config\Repository;
class Imagy class Imagy
{ {
...@@ -13,21 +12,21 @@ class Imagy ...@@ -13,21 +12,21 @@ class Imagy
* @var \Illuminate\Filesystem\Filesystem * @var \Illuminate\Filesystem\Filesystem
*/ */
private $finder; private $finder;
/**
* @var \Illuminate\Contracts\Config\Repository
*/
private $config;
/** /**
* @var ImageFactoryInterface * @var ImageFactoryInterface
*/ */
private $imageFactory; private $imageFactory;
/**
* @var ThumbnailsManager
*/
private $manager;
public function __construct(Repository $config, ImageFactoryInterface $imageFactory) public function __construct(ImageFactoryInterface $imageFactory, ThumbnailsManager $manager)
{ {
$this->image = App::make('Intervention\Image\ImageManager'); $this->image = App::make('Intervention\Image\ImageManager');
$this->finder = App::make('Illuminate\Filesystem\Filesystem'); $this->finder = App::make('Illuminate\Filesystem\Filesystem');
$this->config = $config;
$this->imageFactory = $imageFactory; $this->imageFactory = $imageFactory;
$this->manager = $manager;
} }
/** /**
...@@ -45,7 +44,7 @@ class Imagy ...@@ -45,7 +44,7 @@ class Imagy
return $filename; return $filename;
} }
$this->makeNew($path, $thumbnail, $filename); $this->makeNew($path, $filename, $thumbnail);
return $filename; return $filename;
} }
...@@ -98,14 +97,16 @@ class Imagy ...@@ -98,14 +97,16 @@ class Imagy
/** /**
* Make a new image * Make a new image
* @param string $path * @param string $path
* @param string $thumbnail
* @param string $filename * @param string $filename
* @param string null $thumbnail
*/ */
private function makeNew($path, $thumbnail, $filename) private function makeNew($path, $filename, $thumbnail = null)
{ {
$image = $this->image->make(public_path() . $path); $image = $this->image->make(public_path() . $path);
foreach ($this->config->get("media::thumbnails.{$thumbnail}") as $manipulation => $options) { $thumbnails = $thumbnail ?: $this->manager->all();
foreach ($thumbnails as $manipulation => $options) {
$image = $this->imageFactory->make($manipulation)->handle($image, $options); $image = $this->imageFactory->make($manipulation)->handle($image, $options);
} }
...@@ -113,4 +114,5 @@ class Imagy ...@@ -113,4 +114,5 @@ class Imagy
$this->writeImage($filename, $image); $this->writeImage($filename, $image);
} }
} }
<?php namespace Modules\Media\Image;
use Illuminate\Contracts\Config\Repository;
use Pingpong\Modules\Module;
class ThumbnailsManager
{
/**
* @var Module
*/
private $module;
/**
* @var Repository
*/
private $config;
/**
* @param Repository $config
* @param Module $module
*/
public function __construct(Repository $config, Module $module)
{
$this->module = $module;
$this->config = $config;
}
public function all()
{
$thumbnails = [];
foreach ($this->module->enabled() as $enabledModule) {
$configuration = $this->config->get(strtolower($enabledModule) . '::thumbnails');
$thumbnails = array_merge($thumbnails, $configuration);
}
return $thumbnails;
}
}
...@@ -23,6 +23,8 @@ class FileService ...@@ -23,6 +23,8 @@ class FileService
// Move the uploaded file to /public/assets/media/ // Move the uploaded file to /public/assets/media/
$file->move(public_path() . '/assets/media', $savedFile->filename); $file->move(public_path() . '/assets/media', $savedFile->filename);
// Create the thumbnails
return $savedFile; return $savedFile;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment