Skip to content

Commit

Permalink
MDL-18763 add 512x512px user avatar support
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed May 19, 2012
1 parent 4f7f2a8 commit 33d9f44
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3413,7 +3413,7 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null) {
}

// fix file name automatically
if ($filename !== 'f1' and $filename !== 'f2') {
if ($filename !== 'f1' and $filename !== 'f2' and $filename !== 'f3') {
$filename = 'f1';
}

Expand All @@ -3426,20 +3426,28 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null) {
redirect($theme->pix_url('u/'.$filename, 'moodle')); // intentionally not cached
}

if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.png')) {
if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')) {
// bad reference - try to prevent future retries as hard as possible!
if ($user = $DB->get_record('user', array('id'=>$context->instanceid), 'id, picture')) {
if ($user->picture == 1 or $user->picture > 10) {
$DB->set_field('user', 'picture', 0, array('id'=>$user->id));
if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'.png')) {
if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'.jpg')) {
if ($filename === 'f3') {
// f3 400x400px was introduced in 2.3, there might be only the smaller version.
if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', 'f1.png')) {
$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', 'f1.jpg');
}
}
// no redirect here because it is not cached
$theme = theme_config::load($themename);
$imagefile = $theme->resolve_image_location('u/'.$filename, 'moodle');
send_file($imagefile, basename($imagefile), 60*60*24*14);
}
}
if (!$file) {
// bad reference - try to prevent future retries as hard as possible!
if ($user = $DB->get_record('user', array('id'=>$context->instanceid), 'id, picture')) {
if ($user->picture > 0) {
$DB->set_field('user', 'picture', 0, array('id'=>$user->id));
}
}
// no redirect here because it is not cached
$theme = theme_config::load($themename);
$imagefile = $theme->resolve_image_location('u/'.$filename, 'moodle');
send_file($imagefile, basename($imagefile), 60*60*24*14);
}

send_stored_file($file, 60*60*24*365, 0, false, array('preview' => $preview)); // enable long caching, there are many images on each page

Expand Down
20 changes: 20 additions & 0 deletions lib/gdlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,27 @@ function process_new_icon($context, $component, $filearea, $itemid, $originalfil
if (function_exists('imagecreatetruecolor') and $CFG->gdversion >= 2) {
$im1 = imagecreatetruecolor(100, 100);
$im2 = imagecreatetruecolor(35, 35);
$im3 = imagecreatetruecolor(512, 512);
if ($image->type == IMAGETYPE_PNG and $imagefnc === 'imagepng') {
imagealphablending($im1, false);
$color = imagecolorallocatealpha($im1, 0, 0, 0, 127);
imagefill($im1, 0, 0, $color);
imagesavealpha($im1, true);

imagealphablending($im2, false);
$color = imagecolorallocatealpha($im2, 0, 0, 0, 127);
imagefill($im2, 0, 0, $color);
imagesavealpha($im2, true);

imagealphablending($im3, false);
$color = imagecolorallocatealpha($im3, 0, 0, 0, 127);
imagefill($im3, 0, 0, $color);
imagesavealpha($im3, true);
}
} else {
$im1 = imagecreate(100, 100);
$im2 = imagecreate(35, 35);
$im3 = imagecreate(512, 512);
}

$cx = $image->width / 2;
Expand All @@ -192,6 +200,7 @@ function process_new_icon($context, $component, $filearea, $itemid, $originalfil

imagecopybicubic($im1, $im, 0, 0, $cx - $half, $cy - $half, 100, 100, $half * 2, $half * 2);
imagecopybicubic($im2, $im, 0, 0, $cx - $half, $cy - $half, 35, 35, $half * 2, $half * 2);
imagecopybicubic($im3, $im, 0, 0, $cx - $half, $cy - $half, 512, 512, $half * 2, $half * 2);

$fs = get_file_storage();

Expand Down Expand Up @@ -220,6 +229,17 @@ function process_new_icon($context, $component, $filearea, $itemid, $originalfil
$icon['filename'] = 'f2'.$imageext;
$fs->create_file_from_string($icon, $data);

ob_start();
if (!$imagefnc($im3, NULL, $quality, $filters)) {
ob_end_clean();
$fs->delete_area_files($context->id, $component, $filearea, $itemid);
return false;
}
$data = ob_get_clean();
imagedestroy($im3);
$icon['filename'] = 'f3'.$imageext;
$fs->create_file_from_string($icon, $data);

return $file1->get_id();
}

Expand Down
3 changes: 3 additions & 0 deletions lib/outputcomponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ public function get_url(moodle_page $page, renderer_base $renderer = null) {
} else if ($this->size === true or $this->size == 1) {
$filename = 'f1';
$size = 100;
} else if ($this->size > 100) {
$filename = 'f3';
$size = (int)$this->size;
} else if ($this->size >= 50) {
$filename = 'f1';
$size = (int)$this->size;
Expand Down
Binary file added pix/u/f3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions theme/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ This files describes API changes in /theme/* themes,
information provided here is intended especially for theme designer.


=== 2.3 ===

optional changes:
* add new u/f3.png image when theme contains customised f1 and f2 default user images

=== 2.2 ===

required changes:
Expand Down

0 comments on commit 33d9f44

Please sign in to comment.