From 2cc895aab75affc23548b2ec068073091c5c72c4 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 25 Jul 2010 16:28:09 +0000 Subject: [PATCH] MDL-11000 finally switching to png images with jpg fallback, based on code by Luigi Pinca :-) --- lib/gdlib.php | 39 +++++++++++++++++++++++++++++---------- pluginfile.php | 13 +++++++++---- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/lib/gdlib.php b/lib/gdlib.php index fc801d9aaf9..147f26b4aad 100644 --- a/lib/gdlib.php +++ b/lib/gdlib.php @@ -43,7 +43,7 @@ defined('MOODLE_INTERNAL') || die(); * @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; @@ -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); @@ -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; diff --git a/pluginfile.php b/pluginfile.php index c08296a028a..bcc5291f937 100644 --- a/pluginfile.php +++ b/pluginfile.php @@ -303,9 +303,12 @@ if ($component === 'blog') { 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) { @@ -542,8 +545,10 @@ if ($component === 'blog') { 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