Fixed getting dimensions of videos and flashes

This commit is contained in:
Marcin Kurczewski 2014-10-24 18:25:33 +02:00
parent dd02938a96
commit 60f4dd9d4a
11 changed files with 203 additions and 220 deletions

View file

@ -0,0 +1,130 @@
<?php
namespace Szurubooru\Services;
use Szurubooru\Helpers\MimeHelper;
use Szurubooru\Helpers\ProgramExecutor;
use Szurubooru\Services\ImageManipulation\ImageManipulator;
class ImageConverter
{
const PROGRAM_NAME_DUMP_GNASH = 'dump-gnash';
const PROGRAM_NAME_SWFRENDER = 'swfrender';
const PROGRAM_NAME_FFMPEG = 'ffmpeg';
const PROGRAM_NAME_FFMPEGTHUMBNAILER = 'ffmpegthumbnailer';
private $imageManipulator;
public function __construct(ImageManipulator $imageManipulator)
{
$this->imageManipulator = $imageManipulator;
}
public function createImageFromBuffer($source)
{
$tmpSourcePath = tempnam(sys_get_temp_dir(), 'thumb') . '.dat';
$tmpTargetPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png';
try
{
file_put_contents($tmpSourcePath, $source);
if (!$this->convert($tmpSourcePath, $tmpTargetPath))
throw new \Exception('Error while converting supplied file to image');
$tmpSource = file_get_contents($tmpTargetPath);
return $this->imageManipulator->loadFromBuffer($tmpSource);
}
finally
{
$this->deleteIfExists($tmpSourcePath);
$this->deleteIfExists($tmpTargetPath);
}
}
public function convert($sourcePath, $targetPath)
{
$mime = MimeHelper::getMimeTypeFromFile($sourcePath);
if (MimeHelper::isImage($mime))
{
copy($sourcePath, $targetPath);
return true;
}
if (MimeHelper::isFlash($mime))
return $this->convertFromFlash($sourcePath, $targetPath);
if (MimeHelper::isVideo($mime))
return $this->convertFromVideo($sourcePath, $targetPath);
return false;
}
private function convertFromFlash($sourcePath, $targetPath)
{
if (ProgramExecutor::isProgramAvailable(self::PROGRAM_NAME_DUMP_GNASH))
{
ProgramExecutor::run(
self::PROGRAM_NAME_DUMP_GNASH,
[
'--screenshot', 'last',
'--screenshot-file', $targetPath,
'-1',
'-r1',
'--max-advances', '15',
$sourcePath,
]);
}
if (!file_exists($targetPath) and ProgramExecutor::isProgramAvailable(self::PROGRAM_NAME_SWFRENDER))
{
ProgramExecutor::run(
self::PROGRAM_NAME_SWFRENDER,
[
'swfrender',
$sourcePath,
'-o',
$targetPath,
]);
}
if (!file_exists($targetPath))
return false;
return true;
}
private function convertFromVideo($sourcePath, $targetPath)
{
if (ProgramExecutor::isProgramAvailable(self::PROGRAM_NAME_FFMPEGTHUMBNAILER))
{
ProgramExecutor::run(
self::PROGRAM_NAME_FFMPEGTHUMBNAILER,
[
'-i' . $sourcePath,
'-o' . $targetPath,
'-s0',
'-t12%%'
]);
}
if (!file_exists($targetPath) and ProgramExecutor::isProgramAvailable(self::PROGRAM_NAME_FFMPEG))
{
ProgramExecutor::run(self::PROGRAM_NAME_FFMEPG,
[
'-i', $sourcePath,
'-vframes', '1',
$targetPath
]);
}
if (!file_exists($targetPath))
return false;
return true;
}
private function deleteIfExists($path)
{
if (file_exists($path))
unlink($path);
}
}

View file

@ -18,6 +18,7 @@ use Szurubooru\SearchServices\Requirements\RequirementCompositeValue;
use Szurubooru\SearchServices\Requirements\RequirementSingleValue; use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
use Szurubooru\Services\AuthService; use Szurubooru\Services\AuthService;
use Szurubooru\Services\HistoryService; use Szurubooru\Services\HistoryService;
use Szurubooru\Services\ImageConverter;
use Szurubooru\Services\ImageManipulation\ImageManipulator; use Szurubooru\Services\ImageManipulation\ImageManipulator;
use Szurubooru\Services\NetworkingService; use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\TagService; use Szurubooru\Services\TagService;
@ -36,6 +37,7 @@ class PostService
private $networkingService; private $networkingService;
private $tagService; private $tagService;
private $historyService; private $historyService;
private $imageConverter;
private $imageManipulator; private $imageManipulator;
public function __construct( public function __construct(
@ -49,6 +51,7 @@ class PostService
NetworkingService $networkingService, NetworkingService $networkingService,
TagService $tagService, TagService $tagService,
HistoryService $historyService, HistoryService $historyService,
ImageConverter $imageConverter,
ImageManipulator $imageManipulator) ImageManipulator $imageManipulator)
{ {
$this->config = $config; $this->config = $config;
@ -61,6 +64,7 @@ class PostService
$this->networkingService = $networkingService; $this->networkingService = $networkingService;
$this->tagService = $tagService; $this->tagService = $tagService;
$this->historyService = $historyService; $this->historyService = $historyService;
$this->imageConverter = $imageConverter;
$this->imageManipulator = $imageManipulator; $this->imageManipulator = $imageManipulator;
} }
@ -247,9 +251,17 @@ class PostService
$post->setContent($content); $post->setContent($content);
$image = $this->imageManipulator->loadFromBuffer($content); try
{
$image = $this->imageConverter->createImageFromBuffer($content);
$post->setImageWidth($this->imageManipulator->getImageWidth($image)); $post->setImageWidth($this->imageManipulator->getImageWidth($image));
$post->setImageHeight($this->imageManipulator->getImageHeight($image)); $post->setImageHeight($this->imageManipulator->getImageHeight($image));
}
catch (\Exception $e)
{
$post->setImageWidth(null);
$post->setImageHeight(null);
}
$post->setOriginalFileSize(strlen($content)); $post->setOriginalFileSize(strlen($content));
} }

View file

@ -1,32 +1,38 @@
<?php <?php
namespace Szurubooru\Services\ThumbnailGenerators; namespace Szurubooru\Services;
use Szurubooru\Services\ImageManipulation\IImageManipulator; use Szurubooru\Services\ImageManipulation\IImageManipulator;
use Szurubooru\Services\ImageManipulation\ImageManipulator; use Szurubooru\Services\ImageManipulation\ImageManipulator;
class ImageThumbnailGenerator implements IThumbnailGenerator class ThumbnailGenerator
{ {
private $imageManipulator; const CROP_OUTSIDE = 0;
const CROP_INSIDE = 1;
public function __construct(ImageManipulator $imageManipulator) private $imageManipulator;
private $imageConverter;
public function __construct(ImageManipulator $imageManipulator, ImageConverter $imageConverter)
{ {
$this->imageManipulator = $imageManipulator; $this->imageManipulator = $imageManipulator;
$this->imageConverter = $imageConverter;
} }
public function generate($source, $width, $height, $cropStyle) public function generate($source, $width, $height, $cropStyle)
{ {
try $image = $this->imageConverter->createImageFromBuffer($source);
{ if (!$image)
$image = $this->imageManipulator->loadFromBuffer($source); throw new \Exception('Error while loading supplied image');
$srcWidth = $this->imageManipulator->getImageWidth($image); $srcWidth = $this->imageManipulator->getImageWidth($image);
$srcHeight = $this->imageManipulator->getImageHeight($image); $srcHeight = $this->imageManipulator->getImageHeight($image);
switch ($cropStyle) switch ($cropStyle)
{ {
case IThumbnailGenerator::CROP_OUTSIDE: case ThumbnailGenerator::CROP_OUTSIDE:
$this->cropOutside($image, $srcWidth, $srcHeight, $width, $height); $this->cropOutside($image, $srcWidth, $srcHeight, $width, $height);
break; break;
case IThumbnailGenerator::CROP_INSIDE: case ThumbnailGenerator::CROP_INSIDE:
$this->cropInside($image, $srcWidth, $srcHeight, $width, $height); $this->cropInside($image, $srcWidth, $srcHeight, $width, $height);
break; break;
@ -38,11 +44,6 @@ class ImageThumbnailGenerator implements IThumbnailGenerator
$image, $image,
IImageManipulator::FORMAT_JPEG); IImageManipulator::FORMAT_JPEG);
} }
catch (\Exception $e)
{
return null;
}
}
private function cropOutside(&$image, $srcWidth, $srcHeight, $dstWidth, $dstHeight) private function cropOutside(&$image, $srcWidth, $srcHeight, $dstWidth, $dstHeight)
{ {

View file

@ -1,60 +0,0 @@
<?php
namespace Szurubooru\Services\ThumbnailGenerators;
use Szurubooru\Helpers\ProgramExecutor;
class FlashThumbnailGenerator implements IThumbnailGenerator
{
const PROGRAM_NAME_DUMP_GNASH = 'dump-gnash';
const PROGRAM_NAME_SWFRENDER = 'swfrender';
private $imageThumbnailGenerator;
public function __construct(ImageThumbnailGenerator $imageThumbnailGenerator)
{
$this->imageThumbnailGenerator = $imageThumbnailGenerator;
}
public function generate($source, $width, $height, $cropStyle)
{
$tmpSourcePath = tempnam(sys_get_temp_dir(), 'thumb') . '.dat';
$tmpTargetPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png';
file_put_contents($tmpSourcePath, $source);
if (ProgramExecutor::isProgramAvailable(self::PROGRAM_NAME_DUMP_GNASH))
{
ProgramExecutor::run(
self::PROGRAM_NAME_DUMP_GNASH,
[
'--screenshot', 'last',
'--screenshot-file', $tmpTargetPath,
'-1',
'-r1',
'--max-advances', '15',
$tmpSourcePath,
]);
}
if (!file_exists($tmpTargetPath) and ProgramExecutor::isProgramAvailable(self::PROGRAM_NAME_SWFRENDER))
{
ProgramExecutor::run(
self::PROGRAM_NAME_SWFRENDER,
[
'swfrender',
$tmpSourcePath,
'-o',
$tmpTargetPath,
]);
}
if (!file_exists($tmpTargetPath))
{
unlink($tmpSourcePath);
return null;
}
$ret = $this->imageThumbnailGenerator->generate(file_get_contents($tmpTargetPath), $width, $height, $cropStyle);
unlink($tmpSourcePath);
unlink($tmpTargetPath);
return $ret;
}
}

View file

@ -1,10 +0,0 @@
<?php
namespace Szurubooru\Services\ThumbnailGenerators;
interface IThumbnailGenerator
{
const CROP_OUTSIDE = 0;
const CROP_INSIDE = 1;
public function generate($sourceString, $width, $height, $cropStyle);
}

View file

@ -1,36 +0,0 @@
<?php
namespace Szurubooru\Services\ThumbnailGenerators;
use Szurubooru\Helpers\MimeHelper;
class SmartThumbnailGenerator implements IThumbnailGenerator
{
private $flashThumbnailGenerator;
private $videoThumbnailGenerator;
private $imageThumbnailGenerator;
public function __construct(
FlashThumbnailGenerator $flashThumbnailGenerator,
VideoThumbnailGenerator $videoThumbnailGenerator,
ImageThumbnailGenerator $imageThumbnailGenerator)
{
$this->flashThumbnailGenerator = $flashThumbnailGenerator;
$this->videoThumbnailGenerator = $videoThumbnailGenerator;
$this->imageThumbnailGenerator = $imageThumbnailGenerator;
}
public function generate($source, $width, $height, $cropStyle)
{
$mime = MimeHelper::getMimeTypeFromBuffer($source);
if (MimeHelper::isFlash($mime))
return $this->flashThumbnailGenerator->generate($source, $width, $height, $cropStyle);
if (MimeHelper::isVideo($mime))
return $this->videoThumbnailGenerator->generate($source, $width, $height, $cropStyle);
if (MimeHelper::isImage($mime))
return $this->imageThumbnailGenerator->generate($source, $width, $height, $cropStyle);
return null;
}
}

View file

@ -1,56 +0,0 @@
<?php
namespace Szurubooru\Services\ThumbnailGenerators;
use \Szurubooru\Helpers\ProgramExecutor;
class VideoThumbnailGenerator implements IThumbnailGenerator
{
const PROGRAM_NAME_FFMPEG = 'ffmpeg';
const PROGRAM_NAME_FFMPEGTHUMBNAILER = 'ffmpegthumbnailer';
private $imageThumbnailGenerator;
public function __construct(ImageThumbnailGenerator $imageThumbnailGenerator)
{
$this->imageThumbnailGenerator = $imageThumbnailGenerator;
}
public function generate($source, $width, $height, $cropStyle)
{
$tmpSourcePath = tempnam(sys_get_temp_dir(), 'thumb') . '.dat';
$tmpTargetPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png';
file_put_contents($tmpSourcePath, $source);
if (ProgramExecutor::isProgramAvailable(self::PROGRAM_NAME_FFMPEGTHUMBNAILER))
{
ProgramExecutor::run(
self::PROGRAM_NAME_FFMPEGTHUMBNAILER,
[
'-i' . $tmpSourcePath,
'-o' . $tmpTargetPath,
'-s0',
'-t12%%'
]);
}
if (!file_exists($tmpTargetPath) and ProgramExecutor::isProgramAvailable(self::PROGRAM_NAME_FFMPEG))
{
ProgramExecutor::run(self::PROGRAM_NAME_FFMEPG,
[
'-i', $tmpSourcePath,
'-vframes', '1',
$tmpTargetPath
]);
}
if (!file_exists($tmpTargetPath))
{
unlink($tmpSourcePath);
return null;
}
$ret = $this->imageThumbnailGenerator->generate(file_get_contents($tmpTargetPath), $width, $height, $cropStyle);
unlink($tmpSourcePath);
unlink($tmpTargetPath);
return $ret;
}
}

View file

@ -2,8 +2,7 @@
namespace Szurubooru\Services; namespace Szurubooru\Services;
use Szurubooru\Config; use Szurubooru\Config;
use Szurubooru\Dao\PublicFileDao; use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Services\ThumbnailGenerators\IThumbnailGenerator; use Szurubooru\Services\ThumbnailGenerator;
use Szurubooru\Services\ThumbnailGenerators\SmartThumbnailGenerator;
class ThumbnailService class ThumbnailService
{ {
@ -14,7 +13,7 @@ class ThumbnailService
public function __construct( public function __construct(
Config $config, Config $config,
PublicFileDao $fileDao, PublicFileDao $fileDao,
SmartThumbnailGenerator $thumbnailGenerator) ThumbnailGenerator $thumbnailGenerator)
{ {
$this->config = $config; $this->config = $config;
$this->fileDao = $fileDao; $this->fileDao = $fileDao;
@ -46,11 +45,11 @@ class ThumbnailService
switch ($this->config->misc->thumbnailCropStyle) switch ($this->config->misc->thumbnailCropStyle)
{ {
case 'outside': case 'outside':
$cropStyle = IThumbnailGenerator::CROP_OUTSIDE; $cropStyle = ThumbnailGenerator::CROP_OUTSIDE;
break; break;
case 'inside': case 'inside':
$cropStyle = IThumbnailGenerator::CROP_INSIDE; $cropStyle = humbnailGenerator::CROP_INSIDE;
break; break;
default: default:

View file

@ -9,6 +9,7 @@ use Szurubooru\FormData\UploadFormData;
use Szurubooru\Injector; use Szurubooru\Injector;
use Szurubooru\Services\AuthService; use Szurubooru\Services\AuthService;
use Szurubooru\Services\HistoryService; use Szurubooru\Services\HistoryService;
use Szurubooru\Services\ImageConverter;
use Szurubooru\Services\ImageManipulation\ImageManipulator; use Szurubooru\Services\ImageManipulation\ImageManipulator;
use Szurubooru\Services\NetworkingService; use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\PostService; use Szurubooru\Services\PostService;
@ -29,6 +30,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
private $networkingServiceMock; private $networkingServiceMock;
private $tagService; private $tagService;
private $historyServiceMock; private $historyServiceMock;
private $imageConverterMock;
private $imageManipulatorMock; private $imageManipulatorMock;
public function setUp() public function setUp()
@ -45,6 +47,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$this->tagService = Injector::get(TagService::class); $this->tagService = Injector::get(TagService::class);
$this->historyServiceMock = $this->mock(HistoryService::class); $this->historyServiceMock = $this->mock(HistoryService::class);
$this->configMock->set('database/maxPostSize', 1000000); $this->configMock->set('database/maxPostSize', 1000000);
$this->imageConverterMock = $this->mock(ImageConverter::class);
$this->imageManipulatorMock = $this->mock(ImageManipulator::class); $this->imageManipulatorMock = $this->mock(ImageManipulator::class);
} }
@ -59,6 +62,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$this->authServiceMock->expects($this->once())->method('getLoggedInUser')->willReturn(new User(5)); $this->authServiceMock->expects($this->once())->method('getLoggedInUser')->willReturn(new User(5));
$this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); $this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot());
$this->imageConverterMock->expects($this->never())->method('createImageFromBuffer');
$this->postService = $this->getPostService(); $this->postService = $this->getPostService();
$savedPost = $this->postService->createPost($formData); $savedPost = $this->postService->createPost($formData);
@ -109,6 +113,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); $this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot());
$this->imageConverterMock->expects($this->once())->method('createImageFromBuffer');
$this->postService = $this->getPostService(); $this->postService = $this->getPostService();
$savedPost = $this->postService->createPost($formData); $savedPost = $this->postService->createPost($formData);
@ -128,6 +133,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); $this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot());
$this->imageConverterMock->expects($this->once())->method('createImageFromBuffer');
$this->postService = $this->getPostService(); $this->postService = $this->getPostService();
$savedPost = $this->postService->createPost($formData); $savedPost = $this->postService->createPost($formData);
@ -210,6 +216,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$this->networkingServiceMock, $this->networkingServiceMock,
$this->tagService, $this->tagService,
$this->historyServiceMock, $this->historyServiceMock,
$this->imageConverterMock,
$this->imageManipulatorMock); $this->imageManipulatorMock);
} }
} }

View file

@ -2,19 +2,17 @@
namespace Szurubooru\Tests\Services; namespace Szurubooru\Tests\Services;
use Szurubooru\Helpers\ProgramExecutor; use Szurubooru\Helpers\ProgramExecutor;
use Szurubooru\Injector; use Szurubooru\Injector;
use Szurubooru\Services\ImageConverter;
use Szurubooru\Services\ImageManipulation\ImageManipulator; use Szurubooru\Services\ImageManipulation\ImageManipulator;
use Szurubooru\Services\ThumbnailGenerators\FlashThumbnailGenerator; use Szurubooru\Services\ThumbnailGenerator;
use Szurubooru\Services\ThumbnailGenerators\IThumbnailGenerator;
use Szurubooru\Services\ThumbnailGenerators\SmartThumbnailGenerator;
use Szurubooru\Services\ThumbnailGenerators\VideoThumbnailGenerator;
use Szurubooru\Tests\AbstractTestCase; use Szurubooru\Tests\AbstractTestCase;
final class ThumbnailGeneratorTest extends AbstractTestCase final class ThumbnailGeneratorTest extends AbstractTestCase
{ {
public function testFlashThumbnails() public function testFlashThumbnails()
{ {
if (!ProgramExecutor::isProgramAvailable(FlashThumbnailGenerator::PROGRAM_NAME_DUMP_GNASH) if (!ProgramExecutor::isProgramAvailable(ImageConverter::PROGRAM_NAME_DUMP_GNASH)
and !ProgramExecutor::isProgramAvailable(FlashThumbnailGenerator::PROGRAM_NAME_SWFRENDER)) and !ProgramExecutor::isProgramAvailable(ImageConverter::PROGRAM_NAME_SWFRENDER))
{ {
$this->markTestSkipped('External software necessary to run this test is missing.'); $this->markTestSkipped('External software necessary to run this test is missing.');
} }
@ -26,7 +24,7 @@ final class ThumbnailGeneratorTest extends AbstractTestCase
$this->getTestFile('flash.swf'), $this->getTestFile('flash.swf'),
150, 150,
150, 150,
IThumbnailGenerator::CROP_OUTSIDE); ThumbnailGenerator::CROP_OUTSIDE);
$image = $imageManipulator->loadFromBuffer($result); $image = $imageManipulator->loadFromBuffer($result);
$this->assertEquals(150, $imageManipulator->getImageWidth($image)); $this->assertEquals(150, $imageManipulator->getImageWidth($image));
@ -35,8 +33,8 @@ final class ThumbnailGeneratorTest extends AbstractTestCase
public function testVideoThumbnails() public function testVideoThumbnails()
{ {
if (!ProgramExecutor::isProgramAvailable(VideoThumbnailGenerator::PROGRAM_NAME_FFMPEG) if (!ProgramExecutor::isProgramAvailable(ImageConverter::PROGRAM_NAME_FFMPEG)
and !ProgramExecutor::isProgramAvailable(VideoThumbnailGenerator::PROGRAM_NAME_FFMPEGTHUMBNAILER)) and !ProgramExecutor::isProgramAvailable(ImageConverter::PROGRAM_NAME_FFMPEGTHUMBNAILER))
{ {
$this->markTestSkipped('External software necessary to run this test is missing.'); $this->markTestSkipped('External software necessary to run this test is missing.');
} }
@ -48,7 +46,7 @@ final class ThumbnailGeneratorTest extends AbstractTestCase
$this->getTestFile('video.mp4'), $this->getTestFile('video.mp4'),
150, 150,
150, 150,
IThumbnailGenerator::CROP_OUTSIDE); ThumbnailGenerator::CROP_OUTSIDE);
$image = $imageManipulator->loadFromBuffer($result); $image = $imageManipulator->loadFromBuffer($result);
$this->assertEquals(150, $imageManipulator->getImageWidth($image)); $this->assertEquals(150, $imageManipulator->getImageWidth($image));
@ -64,7 +62,7 @@ final class ThumbnailGeneratorTest extends AbstractTestCase
$this->getTestFile('image.jpg'), $this->getTestFile('image.jpg'),
150, 150,
150, 150,
IThumbnailGenerator::CROP_OUTSIDE); ThumbnailGenerator::CROP_OUTSIDE);
$image = $imageManipulator->loadFromBuffer($result); $image = $imageManipulator->loadFromBuffer($result);
$this->assertEquals(150, $imageManipulator->getImageWidth($image)); $this->assertEquals(150, $imageManipulator->getImageWidth($image));
@ -74,7 +72,7 @@ final class ThumbnailGeneratorTest extends AbstractTestCase
$this->getTestFile('image.jpg'), $this->getTestFile('image.jpg'),
150, 150,
150, 150,
IThumbnailGenerator::CROP_INSIDE); ThumbnailGenerator::CROP_INSIDE);
$image = $imageManipulator->loadFromBuffer($result); $image = $imageManipulator->loadFromBuffer($result);
$this->assertEquals(150, $imageManipulator->getImageWidth($image)); $this->assertEquals(150, $imageManipulator->getImageWidth($image));
@ -86,13 +84,12 @@ final class ThumbnailGeneratorTest extends AbstractTestCase
$thumbnailGenerator = $this->getThumbnailGenerator(); $thumbnailGenerator = $this->getThumbnailGenerator();
$imageManipulator = $this->getImageManipulator(); $imageManipulator = $this->getImageManipulator();
$result = $thumbnailGenerator->generate( $this->setExpectedException(\Exception::class);
$thumbnailGenerator->generate(
$this->getTestFile('text.txt'), $this->getTestFile('text.txt'),
150, 150,
150, 150,
IThumbnailGenerator::CROP_OUTSIDE); ThumbnailGenerator::CROP_OUTSIDE);
$this->assertNull($result);
} }
public function getImageManipulator() public function getImageManipulator()
@ -102,6 +99,6 @@ final class ThumbnailGeneratorTest extends AbstractTestCase
public function getThumbnailGenerator() public function getThumbnailGenerator()
{ {
return Injector::get(SmartThumbnailGenerator::class); return Injector::get(ThumbnailGenerator::class);
} }
} }

View file

@ -1,8 +1,7 @@
<?php <?php
namespace Szurubooru\Tests\Services; namespace Szurubooru\Tests\Services;
use Szurubooru\Dao\PublicFileDao; use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Services\ThumbnailGenerators\IThumbnailGenerator; use Szurubooru\Services\ThumbnailGenerator;
use Szurubooru\Services\ThumbnailGenerators\SmartThumbnailGenerator;
use Szurubooru\Services\ThumbnailService; use Szurubooru\Services\ThumbnailService;
use Szurubooru\Tests\AbstractTestCase; use Szurubooru\Tests\AbstractTestCase;
@ -19,7 +18,7 @@ final class ThumbnailServiceTest extends AbstractTestCase
$this->configMock = $this->mockConfig(); $this->configMock = $this->mockConfig();
$this->fileDaoMock = $this->mock(PublicFileDao::class); $this->fileDaoMock = $this->mock(PublicFileDao::class);
$this->thumbnailServiceMock = $this->mock(ThumbnailService::class); $this->thumbnailServiceMock = $this->mock(ThumbnailService::class);
$this->thumbnailGeneratorMock = $this->mock(SmartThumbnailGenerator::class); $this->thumbnailGeneratorMock = $this->mock(ThumbnailGenerator::class);
} }
public function testGetUsedThumbnailSizes() public function testGetUsedThumbnailSizes()
@ -100,7 +99,7 @@ final class ThumbnailServiceTest extends AbstractTestCase
'content of file', 'content of file',
100, 100,
100, 100,
IThumbnailGenerator::CROP_OUTSIDE) ThumbnailGenerator::CROP_OUTSIDE)
->willReturn(null); ->willReturn(null);
$this->fileDaoMock $this->fileDaoMock
@ -130,7 +129,7 @@ final class ThumbnailServiceTest extends AbstractTestCase
'content of file', 'content of file',
100, 100,
100, 100,
IThumbnailGenerator::CROP_OUTSIDE) ThumbnailGenerator::CROP_OUTSIDE)
->willReturn('content of thumbnail'); ->willReturn('content of thumbnail');
$this->fileDaoMock $this->fileDaoMock