mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 16:32:18 +02:00
MDL-37308 files: support transparency when not resizing; new unit test
This commit is contained in:
parent
e245843193
commit
623c947f2b
@ -1563,6 +1563,7 @@ class file_storage {
|
||||
imagecolortransparent($newimg, $colour);
|
||||
imagealphablending($newimg, false);
|
||||
imagesavealpha($newimg, true);
|
||||
imagefill($newimg, 0, 0, $colour);
|
||||
}
|
||||
|
||||
if (!imagecopyresampled($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height)) {
|
||||
@ -1576,6 +1577,8 @@ class file_storage {
|
||||
ob_start();
|
||||
switch ($filerecord['mimetype']) {
|
||||
case 'image/gif':
|
||||
imagealphablending($img, true);
|
||||
imagesavealpha($img, true);
|
||||
imagegif($img);
|
||||
break;
|
||||
|
||||
@ -1589,6 +1592,13 @@ class file_storage {
|
||||
|
||||
case 'image/png':
|
||||
$quality = (int)$quality;
|
||||
|
||||
// Woah nelly! Because PNG quality is in the range 0 - 9 compared to JPEG quality,
|
||||
// the latter of which can go to 100, we need to make sure that quality here is
|
||||
// in a safe range or PHP WILL CRASH AND DIE. You have been warned.
|
||||
$quality = $quality > 9 ? (int)(max(1.0, (float)$quality / 100.0) * 9.0) : $quality;
|
||||
imagealphablending($img, true);
|
||||
imagesavealpha($img, true);
|
||||
imagepng($img, NULL, $quality, NULL);
|
||||
break;
|
||||
|
||||
|
@ -986,6 +986,69 @@ class core_files_file_storage_testcase extends advanced_testcase {
|
||||
$this->assertInstanceOf('stored_file', $converted);
|
||||
}
|
||||
|
||||
public function test_convert_image_png() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(false);
|
||||
|
||||
$filepath = $CFG->dirroot.'/lib/filestorage/tests/fixtures/testimage.png';
|
||||
$syscontext = context_system::instance();
|
||||
$filerecord = array(
|
||||
'contextid' => $syscontext->id,
|
||||
'component' => 'core',
|
||||
'filearea' => 'unittest',
|
||||
'itemid' => 0,
|
||||
'filepath' => '/images/',
|
||||
'filename' => 'testimage.png',
|
||||
);
|
||||
|
||||
$fs = get_file_storage();
|
||||
$original = $fs->create_file_from_pathname($filerecord, $filepath);
|
||||
|
||||
// Vanilla test.
|
||||
$filerecord['filename'] = 'testimage-converted-nosize.png';
|
||||
$vanilla = $fs->convert_image($filerecord, $original);
|
||||
$this->assertInstanceOf('stored_file', $vanilla);
|
||||
// Assert that byte 25 has the ascii value 6 for PNG-24.
|
||||
$this->assertTrue(ord(substr($vanilla->get_content(), 25, 1)) == 6);
|
||||
|
||||
// 10x10 resize test; also testing for a ridiculous quality setting, which
|
||||
// we should if necessary scale to the 0 - 9 range.
|
||||
$filerecord['filename'] = 'testimage-converted-10x10.png';
|
||||
$converted = $fs->convert_image($filerecord, $original, 10, 10, true, 100);
|
||||
$this->assertInstanceOf('stored_file', $converted);
|
||||
// Assert that byte 25 has the ascii value 6 for PNG-24.
|
||||
$this->assertTrue(ord(substr($converted->get_content(), 25, 1)) == 6);
|
||||
|
||||
// Transparency test.
|
||||
$filerecord['filename'] = 'testimage-converted-102x31.png';
|
||||
$converted = $fs->convert_image($filerecord, $original, 102, 31, true, 9);
|
||||
$this->assertInstanceOf('stored_file', $converted);
|
||||
// Assert that byte 25 has the ascii value 6 for PNG-24.
|
||||
$this->assertTrue(ord(substr($converted->get_content(), 25, 1)) == 6);
|
||||
|
||||
$originalfile = imagecreatefromstring($original->get_content());
|
||||
$convertedfile = imagecreatefromstring($converted->get_content());
|
||||
$vanillafile = imagecreatefromstring($vanilla->get_content());
|
||||
|
||||
$originalcolors = imagecolorsforindex($originalfile, imagecolorat($originalfile, 0, 0));
|
||||
$convertedcolors = imagecolorsforindex($convertedfile, imagecolorat($convertedfile, 0, 0));
|
||||
$vanillacolors = imagecolorsforindex($vanillafile, imagecolorat($vanillafile, 0, 0));
|
||||
$this->assertEquals(count($originalcolors), 4);
|
||||
$this->assertEquals(count($convertedcolors), 4);
|
||||
$this->assertEquals(count($vanillacolors), 4);
|
||||
$this->assertEquals($originalcolors['red'], $convertedcolors['red']);
|
||||
$this->assertEquals($originalcolors['green'], $convertedcolors['green']);
|
||||
$this->assertEquals($originalcolors['blue'], $convertedcolors['blue']);
|
||||
$this->assertEquals($originalcolors['alpha'], $convertedcolors['alpha']);
|
||||
$this->assertEquals($originalcolors['red'], $vanillacolors['red']);
|
||||
$this->assertEquals($originalcolors['green'], $vanillacolors['green']);
|
||||
$this->assertEquals($originalcolors['blue'], $vanillacolors['blue']);
|
||||
$this->assertEquals($originalcolors['alpha'], $vanillacolors['alpha']);
|
||||
$this->assertEquals($originalcolors['alpha'], 127);
|
||||
|
||||
}
|
||||
|
||||
private function generate_file_record() {
|
||||
$syscontext = context_system::instance();
|
||||
$filerecord = new stdClass();
|
||||
|
BIN
lib/filestorage/tests/fixtures/testimage.png
vendored
Normal file
BIN
lib/filestorage/tests/fixtures/testimage.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.1 KiB |
Loading…
x
Reference in New Issue
Block a user