Skip to content

Commit

Permalink
Merge eb2c1ae into 11607b9
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroendesloovere committed May 7, 2018
2 parents 11607b9 + eb2c1ae commit 6968454
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 94 deletions.
5 changes: 5 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ services:
arguments:
- "@fork.settings"

ForkCMS\Utility\Thumbnails:
public: true
arguments:
- "%site.path_www%"

templating:
class: Frontend\Core\Engine\TwigTemplate
public: true
Expand Down
5 changes: 5 additions & 0 deletions app/config/config_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ services:
tags:
- { name: data_collector, template: "database_data_collector.html.twig", id: "database" }

ForkCMS\Utility\Thumbnails:
public: true
arguments:
- "%site.path_www%"

monolog.full_trace_formatter:
class: Monolog\Formatter\LineFormatter
calls:
Expand Down
5 changes: 5 additions & 0 deletions app/config/config_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ services:
- "%database.port%"
calls:
- [ setDebug, [ "%kernel.debug%" ]]

ForkCMS\Utility\Thumbnails:
public: true
arguments:
- "%site.path_www%"
6 changes: 6 additions & 0 deletions app/config/console.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ services:
class: Console\Core\CacheClearCommand
tags:
- { name: console.command }
forkcms.console.thumbnails.generate:
class: Console\Thumbnails\GenerateThumbnailsCommand
arguments:
- "@ForkCMS\\Utility\\Thumbnails"
tags:
- { name: console.command }
1 change: 0 additions & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ $backend->initialize('Console');

$application = new Application($kernel);
$application->add(new \Console\Locale\ImportLocaleCommand());
$application->add(new \Console\Thumbnails\GenerateThumbnailsCommand());
if ($kernel->getContainer()->get('fork.settings') instanceof ModulesSettings) {
$application->add(
new \Console\Locale\EnableLocaleCommand(
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,10 @@
"**/tests/",
"**/Test/"
]
},
"autoload-dev": {
"psr-4": {
"ForkCMS\\Tests\\": "tests/"
}
}
}
79 changes: 11 additions & 68 deletions src/Common/Core/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace Common\Core;

use ForkCMS\App\BaseModel;
use ForkCMS\Utility\Thumbnails;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
Expand Down Expand Up @@ -153,49 +152,26 @@ public static function generatePassword(
* 128px, the width will be calculated based on the aspect ratio.
*
* @param string $path The path wherein the thumbnail-folders will be stored.
* @param string $sourceFile The location of the source file.
* @param string $sourceFile The location of the source file
*
* @deprecated Please use the service `forkcms.utility.thumbnails` instead.
*/
public static function generateThumbnails(string $path, string $sourceFile): void
{
// get folder listing
$folders = self::getThumbnailFolders($path);
$filename = basename($sourceFile);

// loop folders
foreach ($folders as $folder) {
// generate the thumbnail
$thumbnail = new \SpoonThumbnail($sourceFile, $folder['width'], $folder['height']);
$thumbnail->setAllowEnlargement(true);

// if the width & height are specified we should ignore the aspect ratio
if ($folder['width'] !== null && $folder['height'] !== null) {
$thumbnail->setForceOriginalAspectRatio(false);
}
$thumbnail->parseToFile($folder['path'] . '/' . $filename);
}
self::get(Thumbnails::class)->generate($path, $sourceFile);
}

/**
* Delete thumbnails based on the folders in the path
*
* @param string $path The path wherein the thumbnail-folders exist.
* @param string|null $thumbnail The filename to be deleted.
*
* @deprecated Please use the service `forkcms.utility.thumbnails` instead.
*/
public static function deleteThumbnails(string $path, ?string $thumbnail): void
{
// if there is no image provided we can't do anything
if ($thumbnail === null || $thumbnail === '') {
return;
}

$finder = new Finder();
$filesystem = new Filesystem();
foreach ($finder->directories()->in($path) as $directory) {
$fileName = $directory->getRealPath() . '/' . $thumbnail;
if (is_file($fileName)) {
$filesystem->remove($fileName);
}
}
self::get(Thumbnails::class)->delete($path, $thumbnail);
}

/**
Expand All @@ -205,45 +181,12 @@ public static function deleteThumbnails(string $path, ?string $thumbnail): void
* @param bool $includeSource Should the source-folder be included in the return-array.
*
* @return array
*
* @deprecated Please use the service `forkcms.utility.thumbnails` instead.
*/
public static function getThumbnailFolders(string $path, bool $includeSource = false): array
{
$return = [];
$filesystem = new Filesystem();
if (!$filesystem->exists($path)) {
return $return;
}
$finder = new Finder();
$finder->name('/^([0-9]*)x([0-9]*)$/');
if ($includeSource) {
$finder->name('source');
}

foreach ($finder->directories()->in($path)->depth('== 0') as $directory) {
$chunks = explode('x', $directory->getBasename(), 2);
if (!$includeSource && count($chunks) !== 2) {
continue;
}

$item = [];
$item['dirname'] = $directory->getBasename();
$item['path'] = $directory->getRealPath();
if (mb_substr($path, 0, mb_strlen(PATH_WWW)) === PATH_WWW) {
$item['url'] = mb_substr($path, mb_strlen(PATH_WWW));
}

if ($item['dirname'] === 'source') {
$item['width'] = null;
$item['height'] = null;
} else {
$item['width'] = ($chunks[0] !== '') ? (int) $chunks[0] : null;
$item['height'] = ($chunks[1] !== '') ? (int) $chunks[1] : null;
}

$return[] = $item;
}

return $return;
return self::get(Thumbnails::class)->getFolders($path, $includeSource);
}

/**
Expand Down
49 changes: 26 additions & 23 deletions src/Console/Thumbnails/GenerateThumbnailsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Common\Core\Model;
use Exception;
use ForkCMS\Utility\Thumbnails;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -15,6 +16,12 @@
*/
class GenerateThumbnailsCommand extends Command
{
/** @var Finder */
private $finder;

/** @var Thumbnails */
private $thumbnails;

protected function configure(): void
{
$this->setName('forkcms:thumbnails:generate')
Expand All @@ -28,41 +35,37 @@ protected function configure(): void
);
}

protected function execute(InputInterface $input, OutputInterface $output): void
public function __construct(Thumbnails $thumbnails)
{
// Get input values
$folderOption = $input->getOption('folder');
$this->finder = new Finder();
$this->thumbnails = $thumbnails;

if (!isset($folderOption)) {
throw new Exception('Please specify a foldername "--folder=XXX"');
}

// Get path to locale file
$folderPath = $this->getFolderPath($folderOption);
parent::__construct();
}

$this->generateThumbnails($folderPath, $output);
protected function execute(InputInterface $input, OutputInterface $output): void
{
$this->generateThumbnails($this->getFolderPath($input), $output);
}

private function generateThumbnails(string $folderPath, OutputInterface $output): void
{
$finder = new Finder();
$finder->files()->in($folderPath)->name('/^.*\.(jpg|jpeg|png|gif)$/i');
$this->finder->files()->in($folderPath)->name('/^.*\.(jpg|jpeg|png|gif)$/i');

foreach ($finder as $file) {
Model::generateThumbnails($folderPath, $file->getRealPath());
foreach ($this->finder as $file) {
$this->thumbnails->generate($folderPath, $file->getRealPath());
$output->writeln('<info>Creating thumbnail for ' . $file->getBasename() . '...</info>');
}
}

/**
* Get the folder path according to the input options
*
* @param string $folderOption
*
* @return string
*/
private function getFolderPath(string $folderOption): string
private function getFolderPath(InputInterface $input): string
{
return __DIR__ . '/../../..' . '/src/Frontend/Files/' . $folderOption;
$folderOption = $input->getOption('folder');

if (!isset($folderOption)) {
throw new Exception('Please specify a foldername "--folder=XXX" from /src/Frontend/Files where you want to generate thumbnails for.');
}

return realpath(__DIR__ . '/../../../src/Frontend/Files/' . $folderOption);
}
}

0 comments on commit 6968454

Please sign in to comment.