images.lib.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <?php
  2. /* Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2007 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. * or see http://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/lib/images.lib.php
  21. * \brief Set of function for manipulating images
  22. */
  23. // Define size of logo small and mini
  24. $maxwidthsmall=270;$maxheightsmall=150;
  25. $maxwidthmini=128;$maxheightmini=72;
  26. $quality = 80;
  27. /**
  28. * Return if a filename is file name of a supported image format
  29. *
  30. * @param string $file Filename
  31. * @return int -1=Not image filename, 0=Image filename but format not supported by PHP, 1=Image filename with format supported
  32. */
  33. function image_format_supported($file)
  34. {
  35. // Case filename is not a format image
  36. if (! preg_match('/(\.gif|\.jpg|\.jpeg|\.png|\.bmp)$/i',$file,$reg)) return -1;
  37. // Case filename is a format image but not supported by this PHP
  38. $imgfonction='';
  39. if (strtolower($reg[1]) == '.gif') $imgfonction = 'imagecreatefromgif';
  40. if (strtolower($reg[1]) == '.png') $imgfonction = 'imagecreatefrompng';
  41. if (strtolower($reg[1]) == '.jpg') $imgfonction = 'imagecreatefromjpeg';
  42. if (strtolower($reg[1]) == '.jpeg') $imgfonction = 'imagecreatefromjpeg';
  43. if (strtolower($reg[1]) == '.bmp') $imgfonction = 'imagecreatefromwbmp';
  44. if ($imgfonction)
  45. {
  46. if (! function_exists($imgfonction))
  47. {
  48. // Fonctions de conversion non presente dans ce PHP
  49. return 0;
  50. }
  51. }
  52. // Filename is a format image and supported by this PHP
  53. return 1;
  54. }
  55. /**
  56. * Return size of image file on disk (Supported extensions are gif, jpg, png and bmp)
  57. *
  58. * @param string $file Full path name of file
  59. * @param bool $url Image with url (true or false)
  60. * @return array array('width'=>width, 'height'=>height)
  61. */
  62. function dol_getImageSize($file, $url = false)
  63. {
  64. $ret=array();
  65. if (image_format_supported($file) < 0) return $ret;
  66. $fichier = $file;
  67. if (!$url)
  68. {
  69. $fichier = realpath($file); // Chemin canonique absolu de l'image
  70. $dir = dirname($file); // Chemin du dossier contenant l'image
  71. }
  72. $infoImg = getimagesize($fichier); // Recuperation des infos de l'image
  73. $ret['width']=$infoImg[0]; // Largeur de l'image
  74. $ret['height']=$infoImg[1]; // Hauteur de l'image
  75. return $ret;
  76. }
  77. /**
  78. * Resize or crop an image file (Supported extensions are gif, jpg, png and bmp)
  79. *
  80. * @param string $file Path of file to resize/crop
  81. * @param int $mode 0=Resize, 1=Crop
  82. * @param int $newWidth Largeur maximum que dois faire l'image destination (0=keep ratio)
  83. * @param int $newHeight Hauteur maximum que dois faire l'image destination (0=keep ratio)
  84. * @param int $src_x Position of croping image in source image (not use if mode=0)
  85. * @param int $src_y Position of croping image in source image (not use if mode=0)
  86. * @return int File name if OK, error message if KO
  87. */
  88. function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x=0, $src_y=0)
  89. {
  90. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  91. global $conf,$langs;
  92. dol_syslog("dol_imageResizeOrCrop file=".$file." mode=".$mode." newWidth=".$newWidth." newHeight=".$newHeight." src_x=".$src_x." src_y=".$src_y);
  93. // Clean parameters
  94. $file=trim($file);
  95. // Check parameters
  96. if (! $file)
  97. {
  98. // Si le fichier n'a pas ete indique
  99. return 'Bad parameter file';
  100. }
  101. elseif (! file_exists($file))
  102. {
  103. // Si le fichier passe en parametre n'existe pas
  104. return $langs->trans("ErrorFileNotFound",$file);
  105. }
  106. elseif(image_format_supported($file) < 0)
  107. {
  108. return 'This filename '.$file.' does not seem to be an image filename.';
  109. }
  110. elseif(!is_numeric($newWidth) && !is_numeric($newHeight))
  111. {
  112. return 'Wrong value for parameter newWidth or newHeight';
  113. }
  114. elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0)
  115. {
  116. return 'At least newHeight or newWidth must be defined for resizing';
  117. }
  118. elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0))
  119. {
  120. return 'Both newHeight or newWidth must be defined for croping';
  121. }
  122. $fichier = realpath($file); // Chemin canonique absolu de l'image
  123. $dir = dirname($file); // Chemin du dossier contenant l'image
  124. $infoImg = getimagesize($fichier); // Recuperation des infos de l'image
  125. $imgWidth = $infoImg[0]; // Largeur de l'image
  126. $imgHeight = $infoImg[1]; // Hauteur de l'image
  127. if ($mode == 0) // If resize, we check parameters
  128. {
  129. if ($newWidth <= 0)
  130. {
  131. $newWidth=intval(($newHeight / $imgHeight) * $imgWidth); // Keep ratio
  132. }
  133. if ($newHeight <= 0)
  134. {
  135. $newHeight=intval(($newWidth / $imgWidth) * $imgHeight); // Keep ratio
  136. }
  137. }
  138. $imgfonction='';
  139. switch($infoImg[2])
  140. {
  141. case 1: // IMG_GIF
  142. $imgfonction = 'imagecreatefromgif';
  143. break;
  144. case 2: // IMG_JPG
  145. $imgfonction = 'imagecreatefromjpeg';
  146. break;
  147. case 3: // IMG_PNG
  148. $imgfonction = 'imagecreatefrompng';
  149. break;
  150. case 4: // IMG_WBMP
  151. $imgfonction = 'imagecreatefromwbmp';
  152. break;
  153. }
  154. if ($imgfonction)
  155. {
  156. if (! function_exists($imgfonction))
  157. {
  158. // Fonctions de conversion non presente dans ce PHP
  159. return 'Resize not possible. This PHP does not support GD functions '.$imgfonction;
  160. }
  161. }
  162. // Initialisation des variables selon l'extension de l'image
  163. switch($infoImg[2])
  164. {
  165. case 1: // Gif
  166. $img = imagecreatefromgif($fichier);
  167. $extImg = '.gif'; // File name extension of image
  168. $newquality='NU'; // Quality is not used for this format
  169. break;
  170. case 2: // Jpg
  171. $img = imagecreatefromjpeg($fichier);
  172. $extImg = '.jpg';
  173. $newquality=100; // % quality maximum
  174. break;
  175. case 3: // Png
  176. $img = imagecreatefrompng($fichier);
  177. $extImg = '.png';
  178. $newquality=0; // No compression (0-9)
  179. break;
  180. case 4: // Bmp
  181. $img = imagecreatefromwbmp($fichier);
  182. $extImg = '.bmp';
  183. $newquality='NU'; // Quality is not used for this format
  184. break;
  185. }
  186. // Create empty image
  187. if ($infoImg[2] == 1)
  188. {
  189. // Compatibilite image GIF
  190. $imgThumb = imagecreate($newWidth, $newHeight);
  191. }
  192. else
  193. {
  194. $imgThumb = imagecreatetruecolor($newWidth, $newHeight);
  195. }
  196. // Activate antialiasing for better quality
  197. if (function_exists('imageantialias'))
  198. {
  199. imageantialias($imgThumb, true);
  200. }
  201. // This is to keep transparent alpha channel if exists (PHP >= 4.2)
  202. if (function_exists('imagesavealpha'))
  203. {
  204. imagesavealpha($imgThumb, true);
  205. }
  206. // Initialisation des variables selon l'extension de l'image
  207. switch($infoImg[2])
  208. {
  209. case 1: // Gif
  210. $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // On procede autrement pour le format GIF
  211. imagecolortransparent($imgThumb,$trans_colour);
  212. break;
  213. case 2: // Jpg
  214. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
  215. break;
  216. case 3: // Png
  217. imagealphablending($imgThumb,false); // Pour compatibilite sur certain systeme
  218. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
  219. break;
  220. case 4: // Bmp
  221. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
  222. break;
  223. }
  224. if (function_exists("imagefill")) imagefill($imgThumb, 0, 0, $trans_colour);
  225. dol_syslog("dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as $extImg, newquality=$newquality");
  226. //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
  227. imagecopyresampled($imgThumb, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode==0?$imgWidth:$newWidth), ($mode==0?$imgHeight:$newHeight)); // Insere l'image de base redimensionnee
  228. $imgThumbName = $file;
  229. // Check if permission are ok
  230. //$fp = fopen($imgThumbName, "w");
  231. //fclose($fp);
  232. // Create image on disk
  233. switch($infoImg[2])
  234. {
  235. case 1: // Gif
  236. imagegif($imgThumb, $imgThumbName);
  237. break;
  238. case 2: // Jpg
  239. imagejpeg($imgThumb, $imgThumbName, $newquality);
  240. break;
  241. case 3: // Png
  242. imagepng($imgThumb, $imgThumbName, $newquality);
  243. break;
  244. case 4: // Bmp
  245. image2wbmp($imgThumb, $imgThumbName);
  246. break;
  247. }
  248. // Set permissions on file
  249. if (! empty($conf->global->MAIN_UMASK)) @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
  250. // Free memory. This does not delete image.
  251. imagedestroy($img);
  252. imagedestroy($imgThumb);
  253. clearstatcache(); // File was replaced by a modified one, so we clear file caches.
  254. return $imgThumbName;
  255. }
  256. /**
  257. * Create a thumbnail from an image file (Supported extensions are gif, jpg, png and bmp).
  258. * If file is myfile.jpg, new file may be myfile_small.jpg
  259. *
  260. * @param string $file Path of source file to resize
  261. * @param int $maxWidth Largeur maximum que dois faire la miniature (-1=unchanged, 160 by default)
  262. * @param int $maxHeight Hauteur maximum que dois faire l'image (-1=unchanged, 120 by default)
  263. * @param string $extName Extension to differenciate thumb file name ('_small', '_mini')
  264. * @param int $quality Quality of compression (0=worst, 100=best)
  265. * @param string $outdir Directory where to store thumb
  266. * @param int $targetformat New format of target (1,2,3,... or 0 to keep old format)
  267. * @return string Full path of thumb or '' if it fails
  268. */
  269. function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $quality=50, $outdir='thumbs', $targetformat=0)
  270. {
  271. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  272. global $conf,$langs;
  273. dol_syslog("vignette file=".$file." extName=".$extName." maxWidth=".$maxWidth." maxHeight=".$maxHeight." quality=".$quality." outdir=".$outdir." targetformat=".$targetformat);
  274. // Clean parameters
  275. $file=trim($file);
  276. // Check parameters
  277. if (! $file)
  278. {
  279. // Si le fichier n'a pas ete indique
  280. return 'ErrorBadParameters';
  281. }
  282. elseif (! file_exists($file))
  283. {
  284. // Si le fichier passe en parametre n'existe pas
  285. dol_syslog($langs->trans("ErrorFileNotFound",$file),LOG_ERR);
  286. return $langs->trans("ErrorFileNotFound",$file);
  287. }
  288. elseif(image_format_supported($file) < 0)
  289. {
  290. dol_syslog('This file '.$file.' does not seem to be an image format file name.',LOG_WARNING);
  291. return 'ErrorBadImageFormat';
  292. }
  293. elseif(!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1){
  294. // Si la largeur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
  295. dol_syslog('Wrong value for parameter maxWidth',LOG_ERR);
  296. return 'Error: Wrong value for parameter maxWidth';
  297. }
  298. elseif(!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1){
  299. // Si la hauteur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
  300. dol_syslog('Wrong value for parameter maxHeight',LOG_ERR);
  301. return 'Error: Wrong value for parameter maxHeight';
  302. }
  303. $fichier = realpath($file); // Chemin canonique absolu de l'image
  304. $dir = dirname($file); // Chemin du dossier contenant l'image
  305. $infoImg = getimagesize($fichier); // Recuperation des infos de l'image
  306. $imgWidth = $infoImg[0]; // Largeur de l'image
  307. $imgHeight = $infoImg[1]; // Hauteur de l'image
  308. if ($maxWidth == -1) $maxWidth=$infoImg[0]; // If size is -1, we keep unchanged
  309. if ($maxHeight == -1) $maxHeight=$infoImg[1]; // If size is -1, we keep unchanged
  310. // Si l'image est plus petite que la largeur et la hauteur max, on ne cree pas de vignette
  311. if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight)
  312. {
  313. // On cree toujours les vignettes
  314. dol_syslog("File size is smaller than thumb size",LOG_DEBUG);
  315. //return 'Le fichier '.$file.' ne necessite pas de creation de vignette';
  316. }
  317. $imgfonction='';
  318. switch($infoImg[2])
  319. {
  320. case IMAGETYPE_GIF: // 1
  321. $imgfonction = 'imagecreatefromgif';
  322. break;
  323. case IMAGETYPE_JPEG: // 2
  324. $imgfonction = 'imagecreatefromjpeg';
  325. break;
  326. case IMAGETYPE_PNG: // 3
  327. $imgfonction = 'imagecreatefrompng';
  328. break;
  329. case IMAGETYPE_BMP: // 6
  330. // Not supported by PHP GD
  331. break;
  332. case IMAGETYPE_WBMP: // 15
  333. $imgfonction = 'imagecreatefromwbmp';
  334. break;
  335. }
  336. if ($imgfonction)
  337. {
  338. if (! function_exists($imgfonction))
  339. {
  340. // Fonctions de conversion non presente dans ce PHP
  341. return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
  342. }
  343. }
  344. // On cree le repertoire contenant les vignettes
  345. $dirthumb = $dir.($outdir?'/'.$outdir:''); // Chemin du dossier contenant les vignettes
  346. dol_mkdir($dirthumb);
  347. // Initialisation des variables selon l'extension de l'image
  348. switch($infoImg[2])
  349. {
  350. case IMAGETYPE_GIF: // 1
  351. $img = imagecreatefromgif($fichier);
  352. $extImg = '.gif'; // Extension de l'image
  353. break;
  354. case IMAGETYPE_JPEG: // 2
  355. $img = imagecreatefromjpeg($fichier);
  356. $extImg = (preg_match('/\.jpeg$/',$file)?'.jpeg':'.jpg'); // Extension de l'image
  357. break;
  358. case IMAGETYPE_PNG: // 3
  359. $img = imagecreatefrompng($fichier);
  360. $extImg = '.png';
  361. break;
  362. case IMAGETYPE_BMP: // 6
  363. // Not supported by PHP GD
  364. $extImg = '.bmp';
  365. break;
  366. case IMAGETYPE_WBMP: // 15
  367. $img = imagecreatefromwbmp($fichier);
  368. $extImg = '.bmp';
  369. break;
  370. }
  371. if (! is_resource($img))
  372. {
  373. dol_syslog('Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
  374. return 0;
  375. }
  376. // Initialisation des dimensions de la vignette si elles sont superieures a l'original
  377. if($maxWidth > $imgWidth){ $maxWidth = $imgWidth; }
  378. if($maxHeight > $imgHeight){ $maxHeight = $imgHeight; }
  379. $whFact = $maxWidth/$maxHeight; // Facteur largeur/hauteur des dimensions max de la vignette
  380. $imgWhFact = $imgWidth/$imgHeight; // Facteur largeur/hauteur de l'original
  381. // Fixe les dimensions de la vignette
  382. if($whFact < $imgWhFact)
  383. {
  384. // Si largeur determinante
  385. $thumbWidth = $maxWidth;
  386. $thumbHeight = $thumbWidth / $imgWhFact;
  387. }
  388. else
  389. {
  390. // Si hauteur determinante
  391. $thumbHeight = $maxHeight;
  392. $thumbWidth = $thumbHeight * $imgWhFact;
  393. }
  394. $thumbHeight=round($thumbHeight);
  395. $thumbWidth=round($thumbWidth);
  396. // Define target format
  397. if (empty($targetformat)) $targetformat=$infoImg[2];
  398. // Create empty image
  399. if ($targetformat == IMAGETYPE_GIF)
  400. {
  401. // Compatibilite image GIF
  402. $imgThumb = imagecreate($thumbWidth, $thumbHeight);
  403. }
  404. else
  405. {
  406. $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
  407. }
  408. // Activate antialiasing for better quality
  409. if (function_exists('imageantialias'))
  410. {
  411. imageantialias($imgThumb, true);
  412. }
  413. // This is to keep transparent alpha channel if exists (PHP >= 4.2)
  414. if (function_exists('imagesavealpha'))
  415. {
  416. imagesavealpha($imgThumb, true);
  417. }
  418. // Initialisation des variables selon l'extension de l'image
  419. switch($targetformat)
  420. {
  421. case IMAGETYPE_GIF: // 1
  422. $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // On procede autrement pour le format GIF
  423. imagecolortransparent($imgThumb,$trans_colour);
  424. $extImgTarget = '.gif';
  425. $newquality='NU';
  426. break;
  427. case IMAGETYPE_JPEG: // 2
  428. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
  429. $extImgTarget = (preg_match('/\.jpeg$/',$file)?'.jpeg':'.jpg');
  430. $newquality=$quality;
  431. break;
  432. case IMAGETYPE_PNG: // 3
  433. imagealphablending($imgThumb,false); // Pour compatibilite sur certain systeme
  434. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
  435. $extImgTarget = '.png';
  436. $newquality=$quality-100;
  437. $newquality=round(abs($quality-100)*9/100);
  438. break;
  439. case IMAGETYPE_BMP: // 6
  440. // Not supported by PHP GD
  441. $extImgTarget = '.bmp';
  442. $newquality='NU';
  443. break;
  444. case IMAGETYPE_WBMP: // 15
  445. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
  446. $extImgTarget = '.bmp';
  447. $newquality='NU';
  448. break;
  449. }
  450. if (function_exists("imagefill")) imagefill($imgThumb, 0, 0, $trans_colour);
  451. dol_syslog("vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
  452. //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
  453. imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
  454. $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i','',$file); // On enleve extension quelquesoit la casse
  455. $fileName = basename($fileName);
  456. $imgThumbName = $dirthumb.'/'.$fileName.$extName.$extImgTarget; // Chemin complet du fichier de la vignette
  457. // Check if permission are ok
  458. //$fp = fopen($imgThumbName, "w");
  459. //fclose($fp);
  460. // Create image on disk
  461. switch($targetformat)
  462. {
  463. case IMAGETYPE_GIF: // 1
  464. imagegif($imgThumb, $imgThumbName);
  465. break;
  466. case IMAGETYPE_JPEG: // 2
  467. imagejpeg($imgThumb, $imgThumbName, $newquality);
  468. break;
  469. case IMAGETYPE_PNG: // 3
  470. imagepng($imgThumb, $imgThumbName, $newquality);
  471. break;
  472. case IMAGETYPE_BMP: // 6
  473. // Not supported by PHP GD
  474. break;
  475. case IMAGETYPE_WBMP: // 15
  476. image2wbmp($imgThumb, $imgThumbName);
  477. break;
  478. }
  479. // Set permissions on file
  480. if (! empty($conf->global->MAIN_UMASK)) @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
  481. // Free memory. This does not delete image.
  482. imagedestroy($img);
  483. imagedestroy($imgThumb);
  484. return $imgThumbName;
  485. }
  486. /**
  487. * This function returns the html for the moneymeter.
  488. *
  489. * @param int $actualValue amount of actual money
  490. * @param int $pendingValue amount of money of pending memberships
  491. * @param int $intentValue amount of intended money (that's without the amount of actual money)
  492. * @return string thermometer htmlLegenda
  493. */
  494. function moneyMeter($actualValue=0, $pendingValue=0, $intentValue=0)
  495. {
  496. global $langs;
  497. // variables
  498. $height="200";
  499. $maximumValue=125000;
  500. $imageDir = "http://eucd.info/images/therm/";
  501. $imageTop = $imageDir . "therm_top.png";
  502. $imageMiddleActual = $imageDir . "therm_actual.png";
  503. $imageMiddlePending = $imageDir . "therm_pending.png";
  504. $imageMiddleIntent = $imageDir . "therm_intent.png";
  505. $imageMiddleGoal = $imageDir . "therm_goal.png";
  506. $imageIndex = $imageDir . "therm_index.png";
  507. $imageBottom = $imageDir . "therm_bottom.png";
  508. $imageColorActual = $imageDir . "therm_color_actual.png";
  509. $imageColorPending = $imageDir . "therm_color_pending.png";
  510. $imageColorIntent = $imageDir . "therm_color_intent.png";
  511. $formThermTop = '
  512. <!-- Thermometer Begin -->
  513. <table cellpadding="0" cellspacing="4" border="0">
  514. <tr><td>
  515. <table cellpadding="0" cellspacing="0" border="0">
  516. <tr>
  517. <td colspan="2"><img src="' . $imageTop . '" width="58" height="6" border="0"></td>
  518. </tr>
  519. <tr>
  520. <td>
  521. <table cellpadding="0" cellspacing="0" border="0">';
  522. $formSection = '
  523. <tr><td><img src="{image}" width="26" height="{height}" border="0"></td></tr>';
  524. $formThermbottom = '
  525. </table>
  526. </td>
  527. <td><img src="' . $imageIndex . '" width="32" height="200" border="0"></td>
  528. </tr>
  529. <tr>
  530. <td colspan="2"><img src="' . $imageBottom . '" width="58" height="32" border="0"></td>
  531. </tr>
  532. </table>
  533. </td>
  534. </tr></table>';
  535. // legenda
  536. $legendaActual = "&euro; " . round($actualValue);
  537. $legendaPending = "&euro; " . round($pendingValue);
  538. $legendaIntent = "&euro; " . round($intentValue);
  539. $legendaTotal = "&euro; " . round($actualValue + $pendingValue + $intentValue);
  540. $formLegenda = '
  541. <table cellpadding="0" cellspacing="0" border="0">
  542. <tr><td><img src="' . $imageColorActual . '" width="9" height="9">&nbsp;</td><td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>'.$langs->trans("Paid").':<br>' . $legendaActual . '</b></font></td></tr>
  543. <tr><td><img src="' . $imageColorPending . '" width="9" height="9">&nbsp;</td><td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">'.$langs->trans("Waiting").':<br>' . $legendaPending . '</font></td></tr>
  544. <tr><td><img src="' . $imageColorIntent . '" width="9" height="9">&nbsp;</td><td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">'.$langs->trans("Promesses").':<br>' . $legendaIntent . '</font></td></tr>
  545. <tr><td>&nbsp;</td><td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Total:<br>' . $legendaTotal . '</font></td></tr>
  546. </table>
  547. <!-- Thermometer End -->';
  548. // check and edit some values
  549. $error = 0;
  550. if ( $maximumValue <= 0 || $height <= 0 || $actualValue < 0 || $pendingValue < 0 || $intentValue < 0)
  551. {
  552. return "The money meter could not be processed<br>\n";
  553. }
  554. if ( $actualValue > $maximumValue )
  555. {
  556. $actualValue = $maximumValue;
  557. $pendingValue = 0;
  558. $intentValue = 0;
  559. }
  560. else
  561. {
  562. if ( ($actualValue + $pendingValue) > $maximumValue )
  563. {
  564. $pendingValue = $maximumValue - $actualValue;
  565. $intentValue = 0;
  566. }
  567. else
  568. {
  569. if ( ($actualValue + $pendingValue + $intentValue) > $maximumValue )
  570. {
  571. $intentValue = $maximumValue - $actualValue - $pendingValue;
  572. }
  573. }
  574. }
  575. // start writing the html (from bottom to top)
  576. // bottom
  577. $thermometer = $formThermbottom;
  578. // actual
  579. $sectionHeight = round(($actualValue / $maximumValue) * $height);
  580. $totalHeight = $sectionHeight;
  581. if ( $sectionHeight > 0 )
  582. {
  583. $section = $formSection;
  584. $section = str_replace("{image}", $imageMiddleActual, $section);
  585. $section = str_replace("{height}", $sectionHeight, $section);
  586. $thermometer = $section . $thermometer;
  587. }
  588. // pending
  589. $sectionHeight = round(($pendingValue / $maximumValue) * $height);
  590. $totalHeight += $sectionHeight;
  591. if ( $sectionHeight > 0 )
  592. {
  593. $section = $formSection;
  594. $section = str_replace("{image}", $imageMiddlePending, $section);
  595. $section = str_replace("{height}", $sectionHeight, $section);
  596. $thermometer = $section . $thermometer;
  597. }
  598. // intent
  599. $sectionHeight = round(($intentValue / $maximumValue) * $height);
  600. $totalHeight += $sectionHeight;
  601. if ( $sectionHeight > 0 )
  602. {
  603. $section = $formSection;
  604. $section = str_replace("{image}", $imageMiddleIntent, $section);
  605. $section = str_replace("{height}", $sectionHeight, $section);
  606. $thermometer = $section . $thermometer;
  607. }
  608. // goal
  609. $sectionHeight = $height- $totalHeight;
  610. if ( $sectionHeight > 0 )
  611. {
  612. $section = $formSection;
  613. $section = str_replace("{image}", $imageMiddleGoal, $section);
  614. $section = str_replace("{height}", $sectionHeight, $section);
  615. $thermometer = $section . $thermometer;
  616. }
  617. // top
  618. $thermometer = $formThermTop . $thermometer;
  619. return $thermometer . $formLegenda;
  620. }