Commit 1236bb87 authored by Richard Mansfield's avatar Richard Mansfield

Scripts to serve image files can take both maxwidth & maxheight params (bug 1771)

parent 59bd2087
......@@ -45,7 +45,17 @@ else {
$downloadurl .= '&view=' . $viewid;
}
if (!empty($size)) {
$downloadurl .= '&size=' . $size;
if (is_string($size)) {
$downloadurl .= '&size=' . $size;
$size = array('size' => $size);
} else if (is_array($size)) {
$sizeparam = array('w' => 'width', 'h' => 'height', 'maxw' => 'maxwidth', 'maxh' => 'maxheight');
foreach ($sizeparam as $k => $v) {
if (isset($size[$k])) {
$downloadurl .= '&' . $v . '=' . $size[$k];
}
}
}
}
$downloadurl .= '&download=1';
$options['downloadurl'] = $downloadurl;
......@@ -96,7 +106,7 @@ else {
}
}
$path = $file->get_path(array('size' => $size));
$path = $file->get_path($size);
$title = $file->download_title();
if ($contenttype = $file->override_content_type()) {
$options['overridecontenttype'] = $contenttype;
......
......@@ -975,7 +975,7 @@ class ArtefactTypeImage extends ArtefactTypeFile {
$url .= '&size=' . $options['size'];
}
else {
$url .= '&size=20x20';
$url .= '&maxheight=20&maxwidth=20';
}
return $url;
......@@ -983,8 +983,7 @@ class ArtefactTypeImage extends ArtefactTypeFile {
public function get_path($data=array()) {
require_once('file.php');
$size = (isset($data['size'])) ? $data['size'] : null;
$result = get_dataroot_image_path('artefact/file/', $this->id, $size);
$result = get_dataroot_image_path('artefact/file/', $this->id, $data);
return $result;
}
......@@ -999,7 +998,7 @@ class ArtefactTypeImage extends ArtefactTypeFile {
public function render_self($options) {
$result = parent::render_self($options);
$result['html'] = '<div class="fr filedata-icon" style="text-align: center;"><h4>' . get_string('Preview', 'artefact.file') . '</h4><img src="'
. hsc(get_config('wwwroot') . 'artefact/file/download.php?file=' . $this->id . '&view=' . $options['viewid'] . '&maxwidth=400')
. hsc(get_config('wwwroot') . 'artefact/file/download.php?file=' . $this->id . '&view=' . $options['viewid'] . '&maxwidth=400&maxheight=180')
. '" alt=""></div>' . $result['html'];
return $result;
}
......
......@@ -512,6 +512,11 @@ function image_get_new_dimensions($oldx, $oldy, $size) {
$newy = $size['h'];
$newx = ($oldx * $newy) / $oldy;
}
else if (isset($size['maxw']) && isset($size['maxh'])) {
$scale = min(min($size['maxw'], $oldx) / $oldx, min($size['maxh'], $oldy) / $oldy);
$newx = $oldx * $scale;
$newy = $oldy * $scale;
}
else if (isset($size['maxw'])) {
// Else if just maximum width
if ($oldx > $size['maxw']) {
......
......@@ -985,6 +985,7 @@ function get_imagesize_parameters($sizeparam='size', $widthparam='width', $heigh
}
return array('h' => $height);
}
$max = array();
if ($maxwidth) {
if ($maxwidth > $imagemaxwidth) {
throw new ParameterException('Requested image size is too big');
......@@ -992,7 +993,7 @@ function get_imagesize_parameters($sizeparam='size', $widthparam='width', $heigh
if ($maxwidth < 16) {
throw new ParameterException('Requested image size is too small');
}
return array('maxw' => $maxwidth);
$max['maxw'] = $maxwidth;
}
if ($maxheight) {
if ($maxheight > $imagemaxheight) {
......@@ -1001,7 +1002,10 @@ function get_imagesize_parameters($sizeparam='size', $widthparam='width', $heigh
if ($maxheight < 16) {
throw new ParameterException('Requested image size is too small');
}
return array('maxh' => $maxheight);
$max['maxh'] = $maxheight;
}
if (!empty($max)) {
return $max;
}
return null;
}
......
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