Skip to content

Commit

Permalink
MDL-11000 finally switching to png images with jpg fallback, based on…
Browse files Browse the repository at this point in the history
… code by Luigi Pinca :-)
  • Loading branch information
skodak committed Jul 25, 2010
1 parent 0972665 commit 2cc895a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
39 changes: 29 additions & 10 deletions lib/gdlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* @return bool
* @todo Finish documenting this function
*/
function ImageCopyBicubic ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
function ImageCopyBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {

global $CFG;

Expand Down Expand Up @@ -145,10 +145,34 @@ function process_new_icon($context, $component, $filearea, $itemid, $originalfil
return false;
}

if (function_exists('ImagePng')) {
$imagefnc = 'ImagePng';
$imageext = '.png';
$filters = PNG_NO_FILTER;
$quality = 1;
} else if (function_exists('ImageJpeg')) {
$imagefnc = 'ImageJpeg';
$imageext = '.jpg';
$filters = null; // not used
$quality = 90;
} else {
debugging('Jpeg and png not supported on this server, please fix server configuration');
return false;
}

if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) {
$im1 = ImageCreateTrueColor(100,100);
$im2 = ImageCreateTrueColor(35,35);
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);
}
} else {
$im1 = ImageCreate(100,100);
$im2 = ImageCreate(35,35);
Expand All @@ -166,36 +190,31 @@ 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);

if (!function_exists('ImageJpeg')) {
debugging('Jpeg not supported on this server, please fix server configuration');
return false;
}

$fs = get_file_storage();

$icon = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>'/');

ob_start();
if (!ImageJpeg($im1, NULL, 90)) {
if (!$imagefnc($im1, NULL, $quality, $filters)) {
// keep old icons
ob_end_clean();
return false;
}
$data = ob_get_clean();
ImageDestroy($im1);
$icon['filename'] = 'f1.jpg';
$icon['filename'] = 'f1'.$imageext;
$fs->delete_area_files($context->id, $component, $filearea, $itemid);
$fs->create_file_from_string($icon, $data);

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

return true;
Expand Down
13 changes: 9 additions & 4 deletions pluginfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,12 @@
if ($filename !== 'f1' and $filename !== 'f2') {
redirect($OUTPUT->pix_url('u/f1'));
}
if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')) {
redirect($OUTPUT->pix_url('u/f1'));
if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.png')) {
if (!$file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename.'/.jpg')) {
redirect($OUTPUT->pix_url('u/f1'));
}
}

send_stored_file($file, 60*60*24); // enable long caching, there are many images on each page

} else if ($filearea === 'private' and $context->contextlevel == CONTEXT_USER) {
Expand Down Expand Up @@ -542,8 +545,10 @@
if ($filename !== 'f1' and $filename !== 'f2') {
send_file_not_found();
}
if (!$file = $fs->get_file($context->id, 'group', 'icon', $group->id, '/', $filename.'.jpg')) {
send_file_not_found();
if (!$file = $fs->get_file($context->id, 'group', 'icon', $group->id, '/', $filename.'.png')) {
if (!$file = $fs->get_file($context->id, 'group', 'icon', $group->id, '/', $filename.'.jpg')) {
send_file_not_found();
}
}

session_get_instance()->write_close(); // unlock session during fileserving
Expand Down

0 comments on commit 2cc895a

Please sign in to comment.