Added script for generating thumbnails

This commit is contained in:
Marcin Kurczewski 2014-10-17 19:47:27 +02:00
parent a2fefc6560
commit d49aef6e47
3 changed files with 55 additions and 10 deletions

22
scripts/thumbnails.php Normal file
View file

@ -0,0 +1,22 @@
<?php
require_once(__DIR__
. DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . 'src'
. DIRECTORY_SEPARATOR . 'Bootstrap.php');
use Szurubooru\Injector;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Dao\PostDao;
use Szurubooru\Services\PostThumbnailService;
$size = isset($argv[1]) ? $argv[1] : 160;
$postDao = Injector::get(PostDao::class);
$postThumbnailService = Injector::get(PostThumbnailService::class);
foreach ($postDao->findAll() as $post)
{
$thumbnailName = $postThumbnailService->generateIfNeeded($post, $size, $size);
echo '.';
}
echo PHP_EOL;

View file

@ -6,7 +6,7 @@ use Szurubooru\Helpers\MimeHelper;
use Szurubooru\Router;
use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\ThumbnailService;
use Szurubooru\Services\PostThumbnailService;
final class PostContentController extends AbstractController
{
@ -14,20 +14,20 @@ final class PostContentController extends AbstractController
private $fileDao;
private $postService;
private $networkingService;
private $thumbnailService;
private $postThumbnailService;
public function __construct(
Config $config,
PublicFileDao $fileDao,
PostService $postService,
NetworkingService $networkingService,
ThumbnailService $thumbnailService)
PostThumbnailService $postThumbnailService)
{
$this->config = $config;
$this->fileDao = $fileDao;
$this->postService = $postService;
$this->networkingService = $networkingService;
$this->thumbnailService = $thumbnailService;
$this->postThumbnailService = $postThumbnailService;
}
public function registerRoutes(Router $router)
@ -52,12 +52,7 @@ final class PostContentController extends AbstractController
public function getPostThumbnail($postName, $size)
{
$post = $this->postService->getByName($postName);
$sourceName = $post->getThumbnailSourceContentPath();
if (!$this->fileDao->exists($sourceName))
$sourceName = $post->getContentPath();
$thumbnailName = $this->thumbnailService->generateIfNeeded($sourceName, $size, $size);
$thumbnailName = $this->postThumbnailService->generateIfNeeded($post, $size, $size);
$this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName));
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace Szurubooru\Services;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\Post;
use Szurubooru\Services\ThumbnailService;
class PostThumbnailService
{
private $thumbnailService;
private $fileDao;
public function __construct(
PublicFileDao $fileDao,
ThumbnailService $thumbnailService)
{
$this->fileDao = $fileDao;
$this->thumbnailService = $thumbnailService;
}
public function generateIfNeeded(Post $post, $width, $height)
{
$sourceName = $post->getThumbnailSourceContentPath();
if (!$this->fileDao->exists($sourceName))
$sourceName = $post->getContentPath();
return $this->thumbnailService->generateIfNeeded($sourceName, $width, $height);
}
}