MDL-49627 core: Improve thumbnail generation

We now abstract the actual file generation to a new function.
We are then able to call this function from the other image generation
sources. This allows us to generate thumbnails in a more efficient manner.

* Stored files stay as stored files and are not temporarily re-written to
  disk;
* Strings containing an image remain as a variable and are not temporarily
  written to disk; and
* Arbitrary files are not captured to string.
This commit is contained in:
Andrew Nicols 2015-03-23 09:12:28 +08:00
parent 47d333857d
commit b7067f065e
5 changed files with 184 additions and 12 deletions

View File

@ -1030,4 +1030,33 @@ class stored_file {
send_file_not_found();
}
}
/**
* Generates a thumbnail for this stored_file.
*
* If the GD library has at least version 2 and PNG support is available, the returned data
* is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
* returns contents of a JPEG file with black background containing the thumbnail.
*
* @param int $width the width of the requested thumbnail
* @param int $height the height of the requested thumbnail
* @return string|bool false if a problem occurs, the thumbnail image data otherwise
*/
public function generate_image_thumbnail($width, $height) {
if (empty($width) or empty($height)) {
return false;
}
// Fetch the image information for this image.
$imageinfo = @getimagesizefromstring($this->get_content());
if (empty($imageinfo)) {
return false;
}
// Create a new image from the file.
$original = @imagecreatefromstring($this->get_content());
// Generate the thumbnail.
return generate_image_thumbnail_from_image($original, $imageinfo, $width, $height);
}
}

View File

@ -279,27 +279,76 @@ function process_new_icon($context, $component, $filearea, $itemid, $originalfil
* @return string|bool false if a problem occurs, the thumbnail image data otherwise
*/
function generate_image_thumbnail($filepath, $width, $height) {
global $CFG;
if (empty($filepath) or empty($width) or empty($height)) {
return false;
}
$imageinfo = getimagesize($filepath);
// Fetch the image information for this image.
$imageinfo = @getimagesize($filepath);
if (empty($imageinfo)) {
return false;
}
$originalwidth = $imageinfo[0];
// Create a new image from the file.
$original = @imagecreatefromstring(file_get_contents($filepath));
// Generate the thumbnail.
return generate_image_thumbnail_from_image($original, $imageinfo, $width, $height);
}
/**
* Generates a thumbnail for the given image string.
*
* If the GD library has at least version 2 and PNG support is available, the returned data
* is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
* returns contents of a JPEG file with black background containing the thumbnail.
*
* @param string $filedata The image content as a string
* @param int $width the width of the requested thumbnail
* @param int $height the height of the requested thumbnail
* @return string|bool false if a problem occurs, the thumbnail image data otherwise
*/
function generate_image_thumbnail_from_string($filedata, $width, $height) {
if (empty($filedata) or empty($width) or empty($height)) {
return false;
}
// Fetch the image information for this image.
$imageinfo = @getimagesizefromstring($filedata);
if (empty($imageinfo)) {
return false;
}
// Create a new image from the file.
$original = @imagecreatefromstring($filedata);
// Generate the thumbnail.
return generate_image_thumbnail_from_image($original, $imageinfo, $width, $height);
}
/**
* Generates a thumbnail for the given image string.
*
* If the GD library has at least version 2 and PNG support is available, the returned data
* is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
* returns contents of a JPEG file with black background containing the thumbnail.
*
* @param string $original The image content as a string
* @return string|bool false if a problem occurs, the thumbnail image data otherwise
*/
function generate_image_thumbnail_from_image($original, $imageinfo, $width, $height) {
global $CFG;
if (empty($imageinfo)) {
return false;
}
$originalwidth = $imageinfo[0];
$originalheight = $imageinfo[1];
if (empty($originalwidth) or empty($originalheight)) {
return false;
}
$original = imagecreatefromstring(file_get_contents($filepath));
if (function_exists('imagepng')) {
$imagefnc = 'imagepng';
$filters = PNG_NO_FILTER;
@ -327,12 +376,12 @@ function generate_image_thumbnail($filepath, $width, $height) {
$ratio = min($width / $originalwidth, $height / $originalheight);
if ($ratio < 1) {
$targetwidth = floor($originalwidth * $ratio);
$targetheight = floor($originalheight * $ratio);
$targetwidth = floor($originalwidth * $ratio);
$targetheight = floor($originalheight * $ratio);
} else {
// do not enlarge the original file if it is smaller than the requested thumbnail size
$targetwidth = $originalwidth;
$targetheight = $originalheight;
// Do not enlarge the original file if it is smaller than the requested thumbnail size.
$targetwidth = $originalwidth;
$targetheight = $originalheight;
}
$dstx = floor(($width - $targetwidth) / 2);
@ -340,6 +389,7 @@ function generate_image_thumbnail($filepath, $width, $height) {
imagecopybicubic($thumbnail, $original, $dstx, $dsty, 0, 0, $targetwidth, $targetheight, $originalwidth, $originalheight);
// Capture the image as a string object, rather than straight to file.
ob_start();
if (!$imagefnc($thumbnail, null, $quality, $filters)) {
ob_end_clean();

BIN
lib/tests/fixtures/gd-logo.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

89
lib/tests/gdlib_test.php Normal file
View File

@ -0,0 +1,89 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test gd functionality.
*
* @package core
* @category phpunit
* @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* A set of tests for some of the gd functionality within Moodle.
*
* @package core
* @category phpunit
* @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_gdlib_testcase extends basic_testcase {
private $fixturepath = null;
public function setUp() {
$this->fixturepath = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR;
}
public function test_generate_image_thumbnail() {
global $CFG;
require_once($CFG->libdir . '/gdlib.php');
// Test with meaningless data.
// Now use a fixture.
$pngpath = $this->fixturepath . 'gd-logo.png';
$pngthumb = generate_image_thumbnail($pngpath, 24, 24);
$this->assertTrue(is_string($pngthumb));
// And check that the generated image was of the correct proportions and mimetype.
$imageinfo = getimagesizefromstring($pngthumb);
$this->assertEquals(24, $imageinfo[0]);
$this->assertEquals(24, $imageinfo[1]);
$this->assertEquals('image/png', $imageinfo['mime']);
}
public function test_generate_image_thumbnail_from_string() {
global $CFG;
require_once($CFG->libdir . '/gdlib.php');
// Test with meaningless data.
// First empty values.
$this->assertFalse(generate_image_thumbnail_from_string('', 24, 24));
$this->assertFalse(generate_image_thumbnail_from_string('invalid', 0, 24));
$this->assertFalse(generate_image_thumbnail_from_string('invalid', 24, 0));
// Now an invalid string.
$this->assertFalse(generate_image_thumbnail_from_string('invalid', 24, 24));
// Now use a fixture.
$pngpath = $this->fixturepath . 'gd-logo.png';
$pngdata = file_get_contents($pngpath);
$pngthumb = generate_image_thumbnail_from_string($pngdata, 24, 24);
$this->assertTrue(is_string($pngthumb));
// And check that the generated image was of the correct proportions and mimetype.
$imageinfo = getimagesizefromstring($pngthumb);
$this->assertEquals(24, $imageinfo[0]);
$this->assertEquals(24, $imageinfo[1]);
$this->assertEquals('image/png', $imageinfo['mime']);
}
}

View File

@ -28,6 +28,10 @@ information provided here is intended especially for developers.
* Added support for custom string manager implementations via $CFG->customstringmanager
directive in the config.php. See MDL-49361 for details.
* Add new make_request_directory() for creation of per-request files.
* Added generate_image_thumbnail_from_string. This should be used instead of generate_image_thumbnail when the source is a string.
This prevents the need to write files to disk unnecessarily.
* Added generate_image_thumbnail to stored_file class. This should be used when generating thumbnails for stored files.
This prevents the need to write files to disk unnecessarily.
=== 2.8 ===