images.lib.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. $regeximgext='\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.xpm|\.xbm'; // See also into product.class.php
  36. // Case filename is not a format image
  37. if (! preg_match('/('.$regeximgext.')$/i',$file,$reg)) return -1;
  38. // Case filename is a format image but not supported by this PHP
  39. $imgfonction='';
  40. if (strtolower($reg[1]) == '.gif') $imgfonction = 'imagecreatefromgif';
  41. if (strtolower($reg[1]) == '.png') $imgfonction = 'imagecreatefrompng';
  42. if (strtolower($reg[1]) == '.jpg') $imgfonction = 'imagecreatefromjpeg';
  43. if (strtolower($reg[1]) == '.jpeg') $imgfonction = 'imagecreatefromjpeg';
  44. if (strtolower($reg[1]) == '.bmp') $imgfonction = 'imagecreatefromwbmp';
  45. if (strtolower($reg[1]) == '.xpm') $imgfonction = 'imagecreatefromxpm';
  46. if (strtolower($reg[1]) == '.xbm') $imgfonction = 'imagecreatefromxbm';
  47. if ($imgfonction)
  48. {
  49. if (! function_exists($imgfonction))
  50. {
  51. // Fonctions de conversion non presente dans ce PHP
  52. return 0;
  53. }
  54. }
  55. // Filename is a format image and supported by this PHP
  56. return 1;
  57. }
  58. /**
  59. * Return size of image file on disk (Supported extensions are gif, jpg, png and bmp)
  60. *
  61. * @param string $file Full path name of file
  62. * @param bool $url Image with url (true or false)
  63. * @return array array('width'=>width, 'height'=>height)
  64. */
  65. function dol_getImageSize($file, $url = false)
  66. {
  67. $ret=array();
  68. if (image_format_supported($file) < 0) return $ret;
  69. $filetoread = $file;
  70. if (!$url)
  71. {
  72. $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image
  73. }
  74. if ($filetoread)
  75. {
  76. $infoImg = getimagesize($filetoread); // Recuperation des infos de l'image
  77. $ret['width']=$infoImg[0]; // Largeur de l'image
  78. $ret['height']=$infoImg[1]; // Hauteur de l'image
  79. }
  80. return $ret;
  81. }
  82. /**
  83. * Resize or crop an image file (Supported extensions are gif, jpg, png and bmp)
  84. *
  85. * @param string $file Path of file to resize/crop
  86. * @param int $mode 0=Resize, 1=Crop
  87. * @param int $newWidth Largeur maximum que dois faire l'image destination (0=keep ratio)
  88. * @param int $newHeight Hauteur maximum que dois faire l'image destination (0=keep ratio)
  89. * @param int $src_x Position of croping image in source image (not use if mode=0)
  90. * @param int $src_y Position of croping image in source image (not use if mode=0)
  91. * @return int File name if OK, error message if KO
  92. */
  93. function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x=0, $src_y=0)
  94. {
  95. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  96. global $conf,$langs;
  97. dol_syslog("dol_imageResizeOrCrop file=".$file." mode=".$mode." newWidth=".$newWidth." newHeight=".$newHeight." src_x=".$src_x." src_y=".$src_y);
  98. // Clean parameters
  99. $file=trim($file);
  100. // Check parameters
  101. if (! $file)
  102. {
  103. // Si le fichier n'a pas ete indique
  104. return 'Bad parameter file';
  105. }
  106. elseif (! file_exists($file))
  107. {
  108. // Si le fichier passe en parametre n'existe pas
  109. return $langs->trans("ErrorFileNotFound",$file);
  110. }
  111. elseif(image_format_supported($file) < 0)
  112. {
  113. return 'This filename '.$file.' does not seem to be an image filename.';
  114. }
  115. elseif(!is_numeric($newWidth) && !is_numeric($newHeight))
  116. {
  117. return 'Wrong value for parameter newWidth or newHeight';
  118. }
  119. elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0)
  120. {
  121. return 'At least newHeight or newWidth must be defined for resizing';
  122. }
  123. elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0))
  124. {
  125. return 'Both newHeight or newWidth must be defined for croping';
  126. }
  127. $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image
  128. $infoImg = getimagesize($filetoread); // Recuperation des infos de l'image
  129. $imgWidth = $infoImg[0]; // Largeur de l'image
  130. $imgHeight = $infoImg[1]; // Hauteur de l'image
  131. if ($mode == 0) // If resize, we check parameters
  132. {
  133. if ($newWidth <= 0)
  134. {
  135. $newWidth=intval(($newHeight / $imgHeight) * $imgWidth); // Keep ratio
  136. }
  137. if ($newHeight <= 0)
  138. {
  139. $newHeight=intval(($newWidth / $imgWidth) * $imgHeight); // Keep ratio
  140. }
  141. }
  142. $imgfonction='';
  143. switch($infoImg[2])
  144. {
  145. case 1: // IMG_GIF
  146. $imgfonction = 'imagecreatefromgif';
  147. break;
  148. case 2: // IMG_JPG
  149. $imgfonction = 'imagecreatefromjpeg';
  150. break;
  151. case 3: // IMG_PNG
  152. $imgfonction = 'imagecreatefrompng';
  153. break;
  154. case 4: // IMG_WBMP
  155. $imgfonction = 'imagecreatefromwbmp';
  156. break;
  157. }
  158. if ($imgfonction)
  159. {
  160. if (! function_exists($imgfonction))
  161. {
  162. // Fonctions de conversion non presente dans ce PHP
  163. return 'Resize not possible. This PHP does not support GD functions '.$imgfonction;
  164. }
  165. }
  166. // Initialisation des variables selon l'extension de l'image
  167. switch($infoImg[2])
  168. {
  169. case 1: // Gif
  170. $img = imagecreatefromgif($filetoread);
  171. $extImg = '.gif'; // File name extension of image
  172. $newquality='NU'; // Quality is not used for this format
  173. break;
  174. case 2: // Jpg
  175. $img = imagecreatefromjpeg($filetoread);
  176. $extImg = '.jpg';
  177. $newquality=100; // % quality maximum
  178. break;
  179. case 3: // Png
  180. $img = imagecreatefrompng($filetoread);
  181. $extImg = '.png';
  182. $newquality=0; // No compression (0-9)
  183. break;
  184. case 4: // Bmp
  185. $img = imagecreatefromwbmp($filetoread);
  186. $extImg = '.bmp';
  187. $newquality='NU'; // Quality is not used for this format
  188. break;
  189. }
  190. // Create empty image
  191. if ($infoImg[2] == 1)
  192. {
  193. // Compatibilite image GIF
  194. $imgThumb = imagecreate($newWidth, $newHeight);
  195. }
  196. else
  197. {
  198. $imgThumb = imagecreatetruecolor($newWidth, $newHeight);
  199. }
  200. // Activate antialiasing for better quality
  201. if (function_exists('imageantialias'))
  202. {
  203. imageantialias($imgThumb, true);
  204. }
  205. // This is to keep transparent alpha channel if exists (PHP >= 4.2)
  206. if (function_exists('imagesavealpha'))
  207. {
  208. imagesavealpha($imgThumb, true);
  209. }
  210. // Initialisation des variables selon l'extension de l'image
  211. switch($infoImg[2])
  212. {
  213. case 1: // Gif
  214. $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // On procede autrement pour le format GIF
  215. imagecolortransparent($imgThumb,$trans_colour);
  216. break;
  217. case 2: // Jpg
  218. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
  219. break;
  220. case 3: // Png
  221. imagealphablending($imgThumb,false); // Pour compatibilite sur certain systeme
  222. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
  223. break;
  224. case 4: // Bmp
  225. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
  226. break;
  227. }
  228. if (function_exists("imagefill")) imagefill($imgThumb, 0, 0, $trans_colour);
  229. 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");
  230. //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
  231. 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
  232. $imgThumbName = $file;
  233. // Check if permission are ok
  234. //$fp = fopen($imgThumbName, "w");
  235. //fclose($fp);
  236. // Create image on disk
  237. switch($infoImg[2])
  238. {
  239. case 1: // Gif
  240. imagegif($imgThumb, $imgThumbName);
  241. break;
  242. case 2: // Jpg
  243. imagejpeg($imgThumb, $imgThumbName, $newquality);
  244. break;
  245. case 3: // Png
  246. imagepng($imgThumb, $imgThumbName, $newquality);
  247. break;
  248. case 4: // Bmp
  249. image2wbmp($imgThumb, $imgThumbName);
  250. break;
  251. }
  252. // Set permissions on file
  253. if (! empty($conf->global->MAIN_UMASK)) @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
  254. // Free memory. This does not delete image.
  255. imagedestroy($img);
  256. imagedestroy($imgThumb);
  257. clearstatcache(); // File was replaced by a modified one, so we clear file caches.
  258. return $imgThumbName;
  259. }
  260. /**
  261. * dolRotateImage if image is a jpg file.
  262. * Currently use an autodetection to know if we can rotate.
  263. * TODO Introduce a new parameter to force rotate.
  264. *
  265. * @param string $file_path Full path to image to rotate
  266. * @return boolean Success or not
  267. */
  268. function dolRotateImage($file_path)
  269. {
  270. $exif = @exif_read_data($file_path);
  271. if ($exif === false) {
  272. return false;
  273. }
  274. $orientation = intval(@$exif['Orientation']);
  275. if (!in_array($orientation, array(3, 6, 8))) {
  276. return false;
  277. }
  278. $image = @imagecreatefromjpeg($file_path);
  279. switch ($orientation) {
  280. case 3:
  281. $image = @imagerotate($image, 180, 0);
  282. break;
  283. case 6:
  284. $image = @imagerotate($image, 270, 0);
  285. break;
  286. case 8:
  287. $image = @imagerotate($image, 90, 0);
  288. break;
  289. default:
  290. return false;
  291. }
  292. $success = imagejpeg($image, $file_path);
  293. // Free up memory (imagedestroy does not delete files):
  294. @imagedestroy($image);
  295. return $success;
  296. }
  297. /**
  298. * Create a thumbnail from an image file (Supported extensions are gif, jpg, png and bmp).
  299. * If file is myfile.jpg, new file may be myfile_small.jpg
  300. *
  301. * @param string $file Path of source file to resize
  302. * @param int $maxWidth Largeur maximum que dois faire la miniature (-1=unchanged, 160 by default)
  303. * @param int $maxHeight Hauteur maximum que dois faire l'image (-1=unchanged, 120 by default)
  304. * @param string $extName Extension to differenciate thumb file name ('_small', '_mini')
  305. * @param int $quality Quality of compression (0=worst, 100=best)
  306. * @param string $outdir Directory where to store thumb
  307. * @param int $targetformat New format of target (IMAGETYPE_GIF, IMAGETYPE_JPG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_WBMP ... or 0 to keep old format)
  308. * @return string Full path of thumb or '' if it fails
  309. */
  310. function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $quality=50, $outdir='thumbs', $targetformat=0)
  311. {
  312. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  313. global $conf,$langs;
  314. dol_syslog("vignette file=".$file." extName=".$extName." maxWidth=".$maxWidth." maxHeight=".$maxHeight." quality=".$quality." outdir=".$outdir." targetformat=".$targetformat);
  315. // Clean parameters
  316. $file=trim($file);
  317. // Check parameters
  318. if (! $file)
  319. {
  320. // Si le fichier n'a pas ete indique
  321. return 'ErrorBadParameters';
  322. }
  323. elseif (! file_exists($file))
  324. {
  325. // Si le fichier passe en parametre n'existe pas
  326. dol_syslog($langs->trans("ErrorFileNotFound",$file),LOG_ERR);
  327. return $langs->trans("ErrorFileNotFound",$file);
  328. }
  329. elseif(image_format_supported($file) < 0)
  330. {
  331. dol_syslog('This file '.$file.' does not seem to be an image format file name.',LOG_WARNING);
  332. return 'ErrorBadImageFormat';
  333. }
  334. elseif(!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1){
  335. // Si la largeur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
  336. dol_syslog('Wrong value for parameter maxWidth',LOG_ERR);
  337. return 'Error: Wrong value for parameter maxWidth';
  338. }
  339. elseif(!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1){
  340. // Si la hauteur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
  341. dol_syslog('Wrong value for parameter maxHeight',LOG_ERR);
  342. return 'Error: Wrong value for parameter maxHeight';
  343. }
  344. $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image
  345. $infoImg = getimagesize($filetoread); // Recuperation des infos de l'image
  346. $imgWidth = $infoImg[0]; // Largeur de l'image
  347. $imgHeight = $infoImg[1]; // Hauteur de l'image
  348. if ($maxWidth == -1) $maxWidth=$infoImg[0]; // If size is -1, we keep unchanged
  349. if ($maxHeight == -1) $maxHeight=$infoImg[1]; // If size is -1, we keep unchanged
  350. // Si l'image est plus petite que la largeur et la hauteur max, on ne cree pas de vignette
  351. if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight)
  352. {
  353. // On cree toujours les vignettes
  354. dol_syslog("File size is smaller than thumb size",LOG_DEBUG);
  355. //return 'Le fichier '.$file.' ne necessite pas de creation de vignette';
  356. }
  357. $imgfonction='';
  358. switch($infoImg[2])
  359. {
  360. case IMAGETYPE_GIF: // 1
  361. $imgfonction = 'imagecreatefromgif';
  362. break;
  363. case IMAGETYPE_JPEG: // 2
  364. $imgfonction = 'imagecreatefromjpeg';
  365. break;
  366. case IMAGETYPE_PNG: // 3
  367. $imgfonction = 'imagecreatefrompng';
  368. break;
  369. case IMAGETYPE_BMP: // 6
  370. // Not supported by PHP GD
  371. break;
  372. case IMAGETYPE_WBMP: // 15
  373. $imgfonction = 'imagecreatefromwbmp';
  374. break;
  375. }
  376. if ($imgfonction)
  377. {
  378. if (! function_exists($imgfonction))
  379. {
  380. // Fonctions de conversion non presente dans ce PHP
  381. return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
  382. }
  383. }
  384. // On cree le repertoire contenant les vignettes
  385. $dirthumb = dirname($file).($outdir?'/'.$outdir:''); // Chemin du dossier contenant les vignettes
  386. dol_mkdir($dirthumb);
  387. // Initialisation des variables selon l'extension de l'image
  388. switch($infoImg[2])
  389. {
  390. case IMAGETYPE_GIF: // 1
  391. $img = imagecreatefromgif($filetoread);
  392. $extImg = '.gif'; // Extension de l'image
  393. break;
  394. case IMAGETYPE_JPEG: // 2
  395. $img = imagecreatefromjpeg($filetoread);
  396. $extImg = (preg_match('/\.jpeg$/',$file)?'.jpeg':'.jpg'); // Extension de l'image
  397. break;
  398. case IMAGETYPE_PNG: // 3
  399. $img = imagecreatefrompng($filetoread);
  400. $extImg = '.png';
  401. break;
  402. case IMAGETYPE_BMP: // 6
  403. // Not supported by PHP GD
  404. $extImg = '.bmp';
  405. break;
  406. case IMAGETYPE_WBMP: // 15
  407. $img = imagecreatefromwbmp($filetoread);
  408. $extImg = '.bmp';
  409. break;
  410. }
  411. if (! is_resource($img))
  412. {
  413. dol_syslog('Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
  414. return 0;
  415. }
  416. // Initialisation des dimensions de la vignette si elles sont superieures a l'original
  417. if($maxWidth > $imgWidth){ $maxWidth = $imgWidth; }
  418. if($maxHeight > $imgHeight){ $maxHeight = $imgHeight; }
  419. $whFact = $maxWidth/$maxHeight; // Facteur largeur/hauteur des dimensions max de la vignette
  420. $imgWhFact = $imgWidth/$imgHeight; // Facteur largeur/hauteur de l'original
  421. // Fixe les dimensions de la vignette
  422. if($whFact < $imgWhFact)
  423. {
  424. // Si largeur determinante
  425. $thumbWidth = $maxWidth;
  426. $thumbHeight = $thumbWidth / $imgWhFact;
  427. }
  428. else
  429. {
  430. // Si hauteur determinante
  431. $thumbHeight = $maxHeight;
  432. $thumbWidth = $thumbHeight * $imgWhFact;
  433. }
  434. $thumbHeight=round($thumbHeight);
  435. $thumbWidth=round($thumbWidth);
  436. // Define target format
  437. if (empty($targetformat)) $targetformat=$infoImg[2];
  438. // Create empty image
  439. if ($targetformat == IMAGETYPE_GIF)
  440. {
  441. // Compatibilite image GIF
  442. $imgThumb = imagecreate($thumbWidth, $thumbHeight);
  443. }
  444. else
  445. {
  446. $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
  447. }
  448. // Activate antialiasing for better quality
  449. if (function_exists('imageantialias'))
  450. {
  451. imageantialias($imgThumb, true);
  452. }
  453. // This is to keep transparent alpha channel if exists (PHP >= 4.2)
  454. if (function_exists('imagesavealpha'))
  455. {
  456. imagesavealpha($imgThumb, true);
  457. }
  458. // Initialisation des variables selon l'extension de l'image
  459. // $targetformat is 0 by default, in such case, we keep original extension
  460. switch($targetformat)
  461. {
  462. case IMAGETYPE_GIF: // 1
  463. $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // On procede autrement pour le format GIF
  464. imagecolortransparent($imgThumb,$trans_colour);
  465. $extImgTarget = '.gif';
  466. $newquality='NU';
  467. break;
  468. case IMAGETYPE_JPEG: // 2
  469. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
  470. $extImgTarget = (preg_match('/\.jpeg$/i',$file)?'.jpeg':'.jpg');
  471. $newquality=$quality;
  472. break;
  473. case IMAGETYPE_PNG: // 3
  474. imagealphablending($imgThumb,false); // Pour compatibilite sur certain systeme
  475. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
  476. $extImgTarget = '.png';
  477. $newquality=$quality-100;
  478. $newquality=round(abs($quality-100)*9/100);
  479. break;
  480. case IMAGETYPE_BMP: // 6
  481. // Not supported by PHP GD
  482. $extImgTarget = '.bmp';
  483. $newquality='NU';
  484. break;
  485. case IMAGETYPE_WBMP: // 15
  486. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
  487. $extImgTarget = '.bmp';
  488. $newquality='NU';
  489. break;
  490. }
  491. if (function_exists("imagefill")) imagefill($imgThumb, 0, 0, $trans_colour);
  492. dol_syslog("vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
  493. //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
  494. imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
  495. $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i','',$file); // On enleve extension quelquesoit la casse
  496. $fileName = basename($fileName);
  497. //$imgThumbName = $dirthumb.'/'.getImageFileNameForSize(basename($file), $extName, $extImgTarget); // Full path of thumb file
  498. $imgThumbName = getImageFileNameForSize($file, $extName, $extImgTarget); // Full path of thumb file
  499. // Check if permission are ok
  500. //$fp = fopen($imgThumbName, "w");
  501. //fclose($fp);
  502. // Create image on disk
  503. switch($targetformat)
  504. {
  505. case IMAGETYPE_GIF: // 1
  506. imagegif($imgThumb, $imgThumbName);
  507. break;
  508. case IMAGETYPE_JPEG: // 2
  509. imagejpeg($imgThumb, $imgThumbName, $newquality);
  510. break;
  511. case IMAGETYPE_PNG: // 3
  512. imagepng($imgThumb, $imgThumbName, $newquality);
  513. break;
  514. case IMAGETYPE_BMP: // 6
  515. // Not supported by PHP GD
  516. break;
  517. case IMAGETYPE_WBMP: // 15
  518. image2wbmp($imgThumb, $imgThumbName);
  519. break;
  520. }
  521. // Set permissions on file
  522. if (! empty($conf->global->MAIN_UMASK)) @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
  523. // Free memory. This does not delete image.
  524. imagedestroy($img);
  525. imagedestroy($imgThumb);
  526. return $imgThumbName;
  527. }