Commit f0a23f53 authored by David Mudrák's avatar David Mudrák
Browse files

MDL-32471 introducing generate_image_thumbnail() function

This helper function uses GD library to generate a thumbnail of the
given image file.
parent f0f4fff9
......@@ -222,3 +222,88 @@ function process_new_icon($context, $component, $filearea, $itemid, $originalfil
return true;
}
/**
* Generates a thumbnail for the given image
*
* 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 $filepath the full path to the original image file
* @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($filepath, $width, $height) {
global $CFG;
if (empty($CFG->gdversion) or empty($filepath) or empty($width) or empty($height)) {
return false;
}
$imageinfo = getimagesize($filepath);
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;
$quality = 1;
} else if (function_exists('imagejpeg')) {
$imagefnc = 'imagejpeg';
$filters = null;
$quality = 90;
} else {
debugging('Neither JPEG nor PNG are supported at this server, please fix the system configuration.');
return false;
}
if (function_exists('imagecreatetruecolor') and $CFG->gdversion >= 2) {
$thumbnail = imagecreatetruecolor($width, $height);
if ($imagefnc === 'imagepng') {
imagealphablending($thumbnail, false);
imagefill($thumbnail, 0, 0, imagecolorallocatealpha($thumbnail, 0, 0, 0, 127));
imagesavealpha($thumbnail, true);
}
} else {
$thumbnail = imagecreate($width, $height);
}
$ratio = min($width / $originalwidth, $height / $originalheight);
if ($ratio < 1) {
$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;
}
$dstx = floor(($width - $targetwidth) / 2);
$dsty = floor(($height - $targetheight) / 2);
imagecopybicubic($thumbnail, $original, $dstx, $dsty, 0, 0, $targetwidth, $targetheight, $originalwidth, $originalheight);
ob_start();
if (!$imagefnc($thumbnail, null, $quality, $filters)) {
ob_end_clean();
return false;
}
$data = ob_get_clean();
imagedestroy($original);
imagedestroy($thumbnail);
return $data;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment