2014-09-20 18:30:48 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Services;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Helpers\MimeHelper;
|
|
|
|
use Szurubooru\Services\ImageManipulation\GdImageManipulator;
|
|
|
|
use Szurubooru\Services\ImageManipulation\IImageManipulator;
|
|
|
|
use Szurubooru\Services\ImageManipulation\ImageManipulator;
|
|
|
|
use Szurubooru\Services\ImageManipulation\ImagickImageManipulator;
|
|
|
|
use Szurubooru\Tests\AbstractTestCase;
|
2014-09-20 18:30:48 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
final class ImageManipulatorTest extends AbstractTestCase
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
public static function imageManipulatorProvider()
|
|
|
|
{
|
|
|
|
$manipulators = [];
|
|
|
|
$manipulators[] = self::getImagickImageManipulator();
|
|
|
|
$manipulators[] = self::getGdImageManipulator();
|
|
|
|
$manipulators[] = self::getAutoImageManipulator();
|
|
|
|
return array_map(function($manipulator)
|
|
|
|
{
|
|
|
|
return [$manipulator];
|
|
|
|
}, array_filter($manipulators));
|
|
|
|
}
|
2014-09-20 18:30:48 +02:00
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
public function testImagickAvailability()
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
if (!self::getImagickImageManipulator())
|
|
|
|
$this->markTestSkipped('Imagick is not installed');
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
2014-09-20 18:30:48 +02:00
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
public function testGdAvailability()
|
|
|
|
{
|
|
|
|
if (!self::getGdImageManipulator())
|
|
|
|
$this->markTestSkipped('Gd is not installed');
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
2014-09-20 18:30:48 +02:00
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider imageManipulatorProvider
|
|
|
|
*/
|
|
|
|
public function testImageSize($imageManipulator)
|
|
|
|
{
|
|
|
|
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
|
|
|
|
$this->assertEquals(640, $imageManipulator->getImageWidth($image));
|
|
|
|
$this->assertEquals(480, $imageManipulator->getImageHeight($image));
|
2014-09-20 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider imageManipulatorProvider
|
|
|
|
*/
|
|
|
|
public function testNonImage($imageManipulator)
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
$this->assertNull($imageManipulator->loadFromBuffer($this->getTestFile('flash.swf')));
|
2014-09-20 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider imageManipulatorProvider
|
|
|
|
*/
|
|
|
|
public function testImageResizing($imageManipulator)
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
|
|
|
|
$imageManipulator->resizeImage($image, 400, 500);
|
|
|
|
$this->assertEquals(400, $imageManipulator->getImageWidth($image));
|
|
|
|
$this->assertEquals(500, $imageManipulator->getImageHeight($image));
|
2014-09-20 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider imageManipulatorProvider
|
|
|
|
*/
|
|
|
|
public function testImageCroppingBleedWidth($imageManipulator)
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
|
|
|
|
$imageManipulator->cropImage($image, 640, 480, 200, 200);
|
|
|
|
$this->assertEquals(440, $imageManipulator->getImageWidth($image));
|
|
|
|
$this->assertEquals(280, $imageManipulator->getImageHeight($image));
|
2014-09-20 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider imageManipulatorProvider
|
|
|
|
*/
|
|
|
|
public function testImageCroppingBleedPosition($imageManipulator)
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
|
|
|
|
$imageManipulator->cropImage($image, 640, 480, -200, -200);
|
|
|
|
$this->assertEquals(440, $imageManipulator->getImageWidth($image));
|
|
|
|
$this->assertEquals(280, $imageManipulator->getImageHeight($image));
|
2014-09-20 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider imageManipulatorProvider
|
|
|
|
*/
|
|
|
|
public function testImageCroppingBleedBoth($imageManipulator)
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
|
|
|
|
$imageManipulator->cropImage($image, 642, 481, -1, -1);
|
|
|
|
$this->assertEquals(640, $imageManipulator->getImageWidth($image));
|
|
|
|
$this->assertEquals(480, $imageManipulator->getImageHeight($image));
|
2014-09-20 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider imageManipulatorProvider
|
|
|
|
*/
|
|
|
|
public function testImageCroppingMaxBleeding($imageManipulator)
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
|
|
|
|
$imageManipulator->cropImage($image, 100, 100, 1000, 1000);
|
|
|
|
$this->assertEquals(1, $imageManipulator->getImageWidth($image));
|
|
|
|
$this->assertEquals(1, $imageManipulator->getImageHeight($image));
|
2014-09-20 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider imageManipulatorProvider
|
|
|
|
*/
|
|
|
|
public function testSaving($imageManipulator)
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
$image = $imageManipulator->loadFromBuffer($this->getTestFile('image.jpg'));
|
2014-10-08 14:47:47 +02:00
|
|
|
$jpegBuffer = $imageManipulator->saveToBuffer($image, IImageManipulator::FORMAT_JPEG);
|
|
|
|
$pngBuffer = $imageManipulator->saveToBuffer($image, IImageManipulator::FORMAT_PNG);
|
|
|
|
$this->assertEquals('image/jpeg', MimeHelper::getMimeTypeFromBuffer($jpegBuffer));
|
|
|
|
$this->assertEquals('image/png', MimeHelper::getMimeTypeFromBuffer($pngBuffer));
|
2014-09-20 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
private static function getImagickImageManipulator()
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
if (extension_loaded('imagick'))
|
2014-10-08 14:47:47 +02:00
|
|
|
return new ImagickImageManipulator();
|
2014-09-22 14:00:31 +02:00
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function getGdImageManipulator()
|
|
|
|
{
|
|
|
|
if (extension_loaded('gd'))
|
2014-10-08 14:47:47 +02:00
|
|
|
return new GdImageManipulator();
|
2014-09-22 14:00:31 +02:00
|
|
|
else
|
|
|
|
return null;
|
2014-09-20 18:30:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 14:00:31 +02:00
|
|
|
private static function getAutoImageManipulator()
|
2014-09-20 18:30:48 +02:00
|
|
|
{
|
2014-09-22 14:00:31 +02:00
|
|
|
if (extension_loaded('gd') and extension_loaded('imagick'))
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
return new ImageManipulator(
|
2014-09-22 14:00:31 +02:00
|
|
|
self::getImagickImageManipulator(),
|
|
|
|
self::getGdImageManipulator());
|
|
|
|
}
|
|
|
|
return null;
|
2014-09-20 18:30:48 +02:00
|
|
|
}
|
|
|
|
}
|