Commit dc1bc017 authored by Richard Mansfield's avatar Richard Mansfield

Estimate memory usage before resizing images (bug #784978)

Change-Id: I473d816454baec0dc4a4304fcabdb917eca5f3b7
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 6a70bcc8
......@@ -190,6 +190,8 @@ $cfg->bounceprefix = 'AAA-';
// NOTE: the scalable resize might result in images with one dimesion larger than one of these sizes, but not both
$cfg->imagemaxwidth = 1024;
$cfg->imagemaxheight = 1024;
// Maximum allowed memory usage for thumbnail generation (approximate)
$cfg->maximageresizememory = 104857600;
// paths and arguments for various system commands
$cfg->pathtogzip = '/bin/gzip';
......
......@@ -476,6 +476,7 @@ function is_image_file($path) {
* if an appropriate file could not be located or generated
*/
function get_dataroot_image_path($path, $id, $size=null) {
global $THEME;
$dataroot = get_config('dataroot');
$imagepath = $dataroot . $path;
if (substr($imagepath, -1) == '/') {
......@@ -523,6 +524,24 @@ function get_dataroot_image_path($path, $id, $size=null) {
$imageinfo = getimagesize($originalimage);
$originalmimetype = $imageinfo['mime'];
// gd can eat a lot of memory shrinking large images, so use a placeholder image
// here if necessary
if (isset($imageinfo[0]) && isset($imageinfo[1]) && isset($imageinfo['bits'])) {
$approxmem = $imageinfo[0] * $imageinfo[1] * ($imageinfo['bits'] / 8)
* (isset($imageinfo['channels']) ? $imageinfo['channels'] : 3);
}
if (empty($approxmem) || $approxmem > get_config('maximageresizememory')) {
log_debug("Refusing to resize large image $originalimage $originalmimetype "
. $imageinfo[0] . 'x' . $imageinfo[1] . ' ' . $imageinfo['bits'] . '-bit');
$originalimage = $THEME->get_path('images/no_thumbnail.png');
if (empty($originalimage) || !is_readable($originalimage)) {
return false;
}
$imageinfo = getimagesize($originalimage);
$originalmimetype = $imageinfo['mime'];
}
$format = 'png';
switch ($originalmimetype) {
case 'image/jpeg':
......
Markdown is supported
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