From 48ed8d440869b64f532c079055660bbb42fefe8b Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 25 Oct 2015 19:42:00 +0100 Subject: [PATCH] FIX The thumb of user into top menu was using the image in full size. This make a large download at each page call. We must use the mini thumbs. --- htdocs/core/class/html.form.class.php | 10 +++++++-- htdocs/core/lib/functions.lib.php | 29 +++++++++++++++++++++++++++ htdocs/main.inc.php | 2 +- htdocs/user/class/user.class.php | 5 +++-- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index b75bd01d2aa..9b0ef17b465 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -5134,10 +5134,11 @@ class Form * @param int $width Width of photo * @param int $height Height of photo (auto if 0) * @param int $caneditfield Add edit fields + * @param string $imagesize 'mini', 'small' or '' (original) * @param string $cssclass CSS name to use on img for photo * @return string HTML code to output photo */ - static function showphoto($modulepart, $object, $width=100, $height=0, $caneditfield=0, $cssclass='photowithmargin') + static function showphoto($modulepart, $object, $width=100, $height=0, $caneditfield=0, $cssclass='photowithmargin', $imagesize='') { global $conf,$langs; @@ -5156,7 +5157,12 @@ class Form else if ($modulepart=='userphoto') { $dir=$conf->user->dir_output; - if ($object->photo) $file=get_exdir($id, 2, 0, 0, $object, 'user').$object->photo; + if (! empty($object->photo)) + { + if ($imagesize == 'mini') $file=get_exdir($id, 2, 0, 0, $object, 'user').getImageFileNameForSize($object->photo, '_mini'); + else if ($imagesize == 'small') $file=get_exdir($id, 2, 0, 0, $object, 'user').getImageFileNameForSize($object->photo, '_small'); + else $file=get_exdir($id, 2, 0, 0, $object, 'user').$object->photo; + } if (! empty($conf->global->MAIN_OLD_IMAGE_LINKS)) $altfile=$object->id.".jpg"; // For backward compatibility $email=$object->email; } diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 290cdb2300b..04c2c02a7d5 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5118,3 +5118,32 @@ function natural_search($fields, $value, $mode=0, $nofirstand=0) return $res; } +/** + * Return the filename of file to get the thumbs + * + * @param string $file Original filename + * @param string $extName Extension to differenciate thumb file name ('', '_small', '_mini') + * @param string $extImgTarget Force image format for thumbs. Use '' to keep same extension than original image. + * @return string New file name + */ +function getImageFileNameForSize($file, $extName, $extImgTarget='') +{ + $dirName = dirname($file); + if ($dirName == '.') $dirName=''; + + $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i','',$file); // On enleve extension quelquesoit la casse + $fileName = basename($fileName); + + if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.jpg$/i',$file)?'.jpg':''); + if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.jpeg$/i',$file)?'.jpeg':''); + if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.gif$/i',$file)?'.gif':''); + if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.png$/i',$file)?'.png':''); + if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.bmp$/i',$file)?'.bmp':''); + + if (! $extImgTarget) return $file; + + $subdir=''; + if ($extName) $subdir = 'thumbs/'; + + return $dirName.$subdir.$fileName.$extName.$extImgTarget; // New filename for thumb +} diff --git a/htdocs/main.inc.php b/htdocs/main.inc.php index 51147a03a9c..92d421d42cb 100644 --- a/htdocs/main.inc.php +++ b/htdocs/main.inc.php @@ -1443,7 +1443,7 @@ function top_menu($head, $title='', $target='', $disablejs=0, $disablehead=0, $a // User photo $toprightmenu.='
'; // Login name with tooltip diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index e375a3c3d80..4459dfc3fc0 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -1813,14 +1813,15 @@ class User extends CommonObject * @param int $width Width of image * @param int $height Height of image * @param string $cssclass Force a css class + * @param string $imagesize 'mini', 'small' or '' (original) * @return string String with URL link */ - function getPhotoUrl($width, $height, $cssclass='') + function getPhotoUrl($width, $height, $cssclass='', $imagesize='') { $result=''; $result.=''; - $result.=Form::showphoto('userphoto', $this, $width, $height, 0, $cssclass); + $result.=Form::showphoto('userphoto', $this, $width, $height, 0, $cssclass, $imagesize); $result.=''; return $result;