2012-12-04 05:15:23 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\GDBackend;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Core\Cache;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
/**
|
|
|
|
* Tests for the {@link GD} class.
|
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class GDTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
public static $filenames = array(
|
|
|
|
'gif' => 'test_gif.gif',
|
|
|
|
'jpg' => 'test_jpg.jpg',
|
|
|
|
'png8' => 'test_png8.png',
|
|
|
|
'png32' => 'test_png32.png'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
GDBackend::flush();
|
|
|
|
}
|
|
|
|
|
2014-01-02 16:59:34 +01:00
|
|
|
public function tearDown() {
|
2015-09-15 04:52:02 +02:00
|
|
|
GDBackend::flush();
|
|
|
|
parent::tearDown();
|
2014-01-02 16:59:34 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
/**
|
|
|
|
* Loads all images into an associative array of GD objects.
|
|
|
|
* Optionally applies an operation to each GD
|
|
|
|
* @param callable $callback Action to perform on each GD
|
|
|
|
* @return array List of GD
|
|
|
|
*/
|
|
|
|
protected function applyToEachImage($callback = null) {
|
|
|
|
$gds = array();
|
|
|
|
foreach(self::$filenames as $type => $file) {
|
|
|
|
$fullPath = realpath(dirname(__FILE__) . '/gdtest/' . $file);
|
2015-09-15 04:52:02 +02:00
|
|
|
$gd = new GDBackend();
|
|
|
|
$gd->loadFrom($fullPath);
|
2012-12-04 05:15:23 +01:00
|
|
|
if($callback) {
|
|
|
|
$gd = $callback($gd);
|
|
|
|
}
|
|
|
|
$gds[$type] = $gd;
|
|
|
|
}
|
|
|
|
return $gds;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
/**
|
|
|
|
* Takes samples from the given GD at 5 pixel increments
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param GDBackend $gd The source image
|
2012-12-04 05:15:23 +01:00
|
|
|
* @param integer $horizontal Number of samples to take horizontally
|
|
|
|
* @param integer $vertical Number of samples to take vertically
|
|
|
|
* @return array List of colours for each sample, each given as an associative
|
|
|
|
* array with red, blue, green, and alpha components
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
protected function sampleAreas(GDBackend $gd, $horizontal = 4, $vertical = 4) {
|
2012-12-04 05:15:23 +01:00
|
|
|
$samples = array();
|
|
|
|
for($y = 0; $y < $vertical; $y++) {
|
|
|
|
for($x = 0; $x < $horizontal; $x++) {
|
2013-03-19 10:51:39 +01:00
|
|
|
$colour = imagecolorat($gd->getImageResource(), $x * 5, $y * 5);
|
2016-08-19 00:51:35 +02:00
|
|
|
$samples[] = imagecolorsforindex($gd->getImageResource(), $colour);
|
2012-12-04 05:15:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $samples;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
/**
|
|
|
|
* Asserts that two colour channels are equivalent within a given tolerance range
|
|
|
|
* @param integer $expected
|
|
|
|
* @param integer $actual
|
|
|
|
* @param integer $tolerance
|
|
|
|
*/
|
|
|
|
protected function assertColourEquals($expected, $actual, $tolerance = 0) {
|
|
|
|
$match =
|
2014-08-15 08:53:05 +02:00
|
|
|
($expected + $tolerance >= $actual) &&
|
|
|
|
($expected - $tolerance <= $actual);
|
2012-12-04 05:15:23 +01:00
|
|
|
$this->assertTrue($match);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
/**
|
|
|
|
* Asserts that all samples given correctly correspond to a greyscale version
|
|
|
|
* of the test image pattern
|
|
|
|
* @param array $samples List of 16 colour samples representing each of the
|
|
|
|
* 8 x 8 squares on the image pattern
|
|
|
|
* @param int $alphaBits Depth of alpha channel in bits
|
|
|
|
* @param int $tolerance Reasonable tolerance level for colour comparison
|
|
|
|
*/
|
|
|
|
protected function assertGreyscale($samples, $alphaBits = 0, $tolerance = 0) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
// Check that all colour samples match
|
|
|
|
foreach($samples as $sample) {
|
|
|
|
$matches =
|
2014-08-15 08:53:05 +02:00
|
|
|
($sample['red'] === $sample['green']) &&
|
2012-12-04 05:15:23 +01:00
|
|
|
($sample['blue'] === $sample['green']);
|
|
|
|
$this->assertTrue($matches, 'Assert colour is greyscale');
|
|
|
|
if(!$matches) return;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
// check various sample points
|
2016-04-21 09:26:46 +02:00
|
|
|
$this->assertColourEquals(76, $samples[0]['red'], $tolerance);
|
|
|
|
$this->assertColourEquals(149, $samples[2]['red'], $tolerance);
|
2012-12-04 05:15:23 +01:00
|
|
|
$this->assertColourEquals(0, $samples[8]['red'], $tolerance);
|
|
|
|
$this->assertColourEquals(127, $samples[9]['red'], $tolerance);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
// check alpha of various points
|
|
|
|
switch($alphaBits) {
|
|
|
|
case 0:
|
|
|
|
$this->assertColourEquals(0, $samples[2]['alpha'], $tolerance);
|
|
|
|
$this->assertColourEquals(0, $samples[12]['alpha'], $tolerance);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
$this->assertColourEquals(0, $samples[2]['alpha'], $tolerance);
|
|
|
|
$this->assertColourEquals(127, $samples[12]['alpha'], $tolerance);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->assertColourEquals(63, $samples[2]['alpha'], $tolerance);
|
|
|
|
$this->assertColourEquals(127, $samples[12]['alpha'], $tolerance);
|
|
|
|
break;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
/**
|
|
|
|
* Tests that images are correctly transformed to greyscale
|
|
|
|
*/
|
|
|
|
function testGreyscale() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
// Apply greyscaling to each image
|
2013-06-21 00:32:08 +02:00
|
|
|
$images = $this->applyToEachImage(function(GDBackend $gd) {
|
2012-12-04 05:15:23 +01:00
|
|
|
return $gd->greyscale();
|
|
|
|
});
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
// Test GIF (256 colour, transparency)
|
|
|
|
$samplesGIF = $this->sampleAreas($images['gif']);
|
|
|
|
$this->assertGreyscale($samplesGIF, 1);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
// Test JPG
|
|
|
|
$samplesJPG = $this->sampleAreas($images['jpg']);
|
|
|
|
$this->assertGreyscale($samplesJPG, 0, 4);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
// Test PNG 8 (indexed with alpha transparency)
|
|
|
|
$samplesPNG8 = $this->sampleAreas($images['png8']);
|
|
|
|
$this->assertGreyscale($samplesPNG8, 8, 4);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-04 05:15:23 +01:00
|
|
|
// Test PNG 32 (full alpha transparency)
|
|
|
|
$samplesPNG32 = $this->sampleAreas($images['png32']);
|
|
|
|
$this->assertGreyscale($samplesPNG32, 8);
|
|
|
|
}
|
2014-01-02 16:59:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that GD doesn't attempt to load images when they're deemed unavailable
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testImageSkippedWhenUnavailable() {
|
|
|
|
$fullPath = realpath(dirname(__FILE__) . '/gdtest/test_jpg.jpg');
|
2015-09-15 04:52:02 +02:00
|
|
|
$gd = new GDBackend_ImageUnavailable();
|
|
|
|
$gd->loadFrom($fullPath);
|
2014-01-02 16:59:34 +01:00
|
|
|
|
|
|
|
/* Ensure no image resource is created if the image is unavailable */
|
|
|
|
$this->assertNull($gd->getImageResource());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the integrity of the manipulation cache when an error occurs
|
|
|
|
*/
|
|
|
|
public function testCacheIntegrity() {
|
2015-09-15 04:52:02 +02:00
|
|
|
$fullPath = realpath(dirname(__FILE__) . '/gdtest/nonimagedata.jpg');
|
2014-01-02 16:59:34 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Load invalid file
|
|
|
|
$gd = new GDBackend();
|
|
|
|
$gd->loadFrom($fullPath);
|
2014-01-02 16:59:34 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Cache should refer to this file
|
2016-09-09 08:43:05 +02:00
|
|
|
$cache = Cache::factory('GDBackend_Manipulations');
|
2015-09-15 04:52:02 +02:00
|
|
|
$key = sha1(implode('|', array($fullPath, filemtime($fullPath))));
|
|
|
|
$data = $cache->load($key);
|
|
|
|
$this->assertEquals('1', $data);
|
2014-01-02 16:59:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that GD::failedResample() returns true for the current image
|
|
|
|
* manipulation only if it previously failed
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testFailedResample() {
|
2015-09-15 04:52:02 +02:00
|
|
|
$fullPath = realpath(dirname(__FILE__) . '/gdtest/nonimagedata.jpg');
|
|
|
|
$fullPath2 = realpath(dirname(__FILE__) . '/gdtest/test_gif.gif');
|
2014-01-02 16:59:34 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Load invalid file
|
|
|
|
$gd = new GDBackend();
|
|
|
|
$gd->loadFrom($fullPath);
|
2014-01-02 16:59:34 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Cache should refre to this file
|
|
|
|
$this->assertTrue($gd->failedResample($fullPath, filemtime($fullPath)));
|
|
|
|
$this->assertFalse($gd->failedResample($fullPath2, filemtime($fullPath2)));
|
2014-01-02 16:59:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
class GDBackend_ImageUnavailable extends GDBackend implements TestOnly {
|
2014-01-02 16:59:34 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
public function failedResample($arg = null) {
|
2015-09-15 04:52:02 +02:00
|
|
|
return true;
|
2014-01-02 16:59:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class GDBackend_Failure_Exception extends Exception {
|
|
|
|
|
|
|
|
}
|