images.lib.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. * dolRotateImage if image is a jpg file.
  258. * Currently use an autodetection to know if we can rotate.
  259. * TODO Introduce a new parameter to force rotate.
  260. *
  261. * @param string $file_path Full path to image to rotate
  262. * @return boolean Success or not
  263. */
  264. function dolRotateImage($file_path)
  265. {
  266. $exif = @exif_read_data($file_path);
  267. if ($exif === false) {
  268. return false;
  269. }
  270. $orientation = intval(@$exif['Orientation']);
  271. if (!in_array($orientation, array(3, 6, 8))) {
  272. return false;
  273. }
  274. $image = @imagecreatefromjpeg($file_path);
  275. switch ($orientation) {
  276. case 3:
  277. $image = @imagerotate($image, 180, 0);
  278. break;
  279. case 6:
  280. $image = @imagerotate($image, 270, 0);
  281. break;
  282. case 8:
  283. $image = @imagerotate($image, 90, 0);
  284. break;
  285. default:
  286. return false;
  287. }
  288. $success = imagejpeg($image, $file_path);
  289. // Free up memory (imagedestroy does not delete files):
  290. @imagedestroy($image);
  291. return $success;
  292. }
  293. /**
  294. * Create a thumbnail from an image file (Supported extensions are gif, jpg, png and bmp).
  295. * If file is myfile.jpg, new file may be myfile_small.jpg
  296. *
  297. * @param string $file Path of source file to resize
  298. * @param int $maxWidth Largeur maximum que dois faire la miniature (-1=unchanged, 160 by default)
  299. * @param int $maxHeight Hauteur maximum que dois faire l'image (-1=unchanged, 120 by default)
  300. * @param string $extName Extension to differenciate thumb file name ('_small', '_mini')
  301. * @param int $quality Quality of compression (0=worst, 100=best)
  302. * @param string $outdir Directory where to store thumb
  303. * @param int $targetformat New format of target (IMAGETYPE_GIF, IMAGETYPE_JPG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_WBMP ... or 0 to keep old format)
  304. * @return string Full path of thumb or '' if it fails
  305. */
  306. function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $quality=50, $outdir='thumbs', $targetformat=0)
  307. {
  308. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  309. global $conf,$langs;
  310. dol_syslog("vignette file=".$file." extName=".$extName." maxWidth=".$maxWidth." maxHeight=".$maxHeight." quality=".$quality." outdir=".$outdir." targetformat=".$targetformat);
  311. // Clean parameters
  312. $file=trim($file);
  313. // Check parameters
  314. if (! $file)
  315. {
  316. // Si le fichier n'a pas ete indique
  317. return 'ErrorBadParameters';
  318. }
  319. elseif (! file_exists($file))
  320. {
  321. // Si le fichier passe en parametre n'existe pas
  322. dol_syslog($langs->trans("ErrorFileNotFound",$file),LOG_ERR);
  323. return $langs->trans("ErrorFileNotFound",$file);
  324. }
  325. elseif(image_format_supported($file) < 0)
  326. {
  327. dol_syslog('This file '.$file.' does not seem to be an image format file name.',LOG_WARNING);
  328. return 'ErrorBadImageFormat';
  329. }
  330. elseif(!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1){
  331. // Si la largeur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
  332. dol_syslog('Wrong value for parameter maxWidth',LOG_ERR);
  333. return 'Error: Wrong value for parameter maxWidth';
  334. }
  335. elseif(!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1){
  336. // Si la hauteur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
  337. dol_syslog('Wrong value for parameter maxHeight',LOG_ERR);
  338. return 'Error: Wrong value for parameter maxHeight';
  339. }
  340. $fichier = realpath($file); // Chemin canonique absolu de l'image
  341. $dir = dirname($file); // Chemin du dossier contenant l'image
  342. $infoImg = getimagesize($fichier); // Recuperation des infos de l'image
  343. $imgWidth = $infoImg[0]; // Largeur de l'image
  344. $imgHeight = $infoImg[1]; // Hauteur de l'image
  345. if ($maxWidth == -1) $maxWidth=$infoImg[0]; // If size is -1, we keep unchanged
  346. if ($maxHeight == -1) $maxHeight=$infoImg[1]; // If size is -1, we keep unchanged
  347. // Si l'image est plus petite que la largeur et la hauteur max, on ne cree pas de vignette
  348. if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight)
  349. {
  350. // On cree toujours les vignettes
  351. dol_syslog("File size is smaller than thumb size",LOG_DEBUG);
  352. //return 'Le fichier '.$file.' ne necessite pas de creation de vignette';
  353. }
  354. $imgfonction='';
  355. switch($infoImg[2])
  356. {
  357. case IMAGETYPE_GIF: // 1
  358. $imgfonction = 'imagecreatefromgif';
  359. break;
  360. case IMAGETYPE_JPEG: // 2
  361. $imgfonction = 'imagecreatefromjpeg';
  362. break;
  363. case IMAGETYPE_PNG: // 3
  364. $imgfonction = 'imagecreatefrompng';
  365. break;
  366. case IMAGETYPE_BMP: // 6
  367. // Not supported by PHP GD
  368. break;
  369. case IMAGETYPE_WBMP: // 15
  370. $imgfonction = 'imagecreatefromwbmp';
  371. break;
  372. }
  373. if ($imgfonction)
  374. {
  375. if (! function_exists($imgfonction))
  376. {
  377. // Fonctions de conversion non presente dans ce PHP
  378. return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
  379. }
  380. }
  381. // On cree le repertoire contenant les vignettes
  382. $dirthumb = $dir.($outdir?'/'.$outdir:''); // Chemin du dossier contenant les vignettes
  383. dol_mkdir($dirthumb);
  384. // Initialisation des variables selon l'extension de l'image
  385. switch($infoImg[2])
  386. {
  387. case IMAGETYPE_GIF: // 1
  388. $img = imagecreatefromgif($fichier);
  389. $extImg = '.gif'; // Extension de l'image
  390. break;
  391. case IMAGETYPE_JPEG: // 2
  392. $img = imagecreatefromjpeg($fichier);
  393. $extImg = (preg_match('/\.jpeg$/',$file)?'.jpeg':'.jpg'); // Extension de l'image
  394. break;
  395. case IMAGETYPE_PNG: // 3
  396. $img = imagecreatefrompng($fichier);
  397. $extImg = '.png';
  398. break;
  399. case IMAGETYPE_BMP: // 6
  400. // Not supported by PHP GD
  401. $extImg = '.bmp';
  402. break;
  403. case IMAGETYPE_WBMP: // 15
  404. $img = imagecreatefromwbmp($fichier);
  405. $extImg = '.bmp';
  406. break;
  407. }
  408. if (! is_resource($img))
  409. {
  410. dol_syslog('Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
  411. return 0;
  412. }
  413. // Initialisation des dimensions de la vignette si elles sont superieures a l'original
  414. if($maxWidth > $imgWidth){ $maxWidth = $imgWidth; }
  415. if($maxHeight > $imgHeight){ $maxHeight = $imgHeight; }
  416. $whFact = $maxWidth/$maxHeight; // Facteur largeur/hauteur des dimensions max de la vignette
  417. $imgWhFact = $imgWidth/$imgHeight; // Facteur largeur/hauteur de l'original
  418. // Fixe les dimensions de la vignette
  419. if($whFact < $imgWhFact)
  420. {
  421. // Si largeur determinante
  422. $thumbWidth = $maxWidth;
  423. $thumbHeight = $thumbWidth / $imgWhFact;
  424. }
  425. else
  426. {
  427. // Si hauteur determinante
  428. $thumbHeight = $maxHeight;
  429. $thumbWidth = $thumbHeight * $imgWhFact;
  430. }
  431. $thumbHeight=round($thumbHeight);
  432. $thumbWidth=round($thumbWidth);
  433. // Define target format
  434. if (empty($targetformat)) $targetformat=$infoImg[2];
  435. // Create empty image
  436. if ($targetformat == IMAGETYPE_GIF)
  437. {
  438. // Compatibilite image GIF
  439. $imgThumb = imagecreate($thumbWidth, $thumbHeight);
  440. }
  441. else
  442. {
  443. $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
  444. }
  445. // Activate antialiasing for better quality
  446. if (function_exists('imageantialias'))
  447. {
  448. imageantialias($imgThumb, true);
  449. }
  450. // This is to keep transparent alpha channel if exists (PHP >= 4.2)
  451. if (function_exists('imagesavealpha'))
  452. {
  453. imagesavealpha($imgThumb, true);
  454. }
  455. // Initialisation des variables selon l'extension de l'image
  456. // $targetformat is 0 by default, in such case, we keep original extension
  457. switch($targetformat)
  458. {
  459. case IMAGETYPE_GIF: // 1
  460. $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // On procede autrement pour le format GIF
  461. imagecolortransparent($imgThumb,$trans_colour);
  462. $extImgTarget = '.gif';
  463. $newquality='NU';
  464. break;
  465. case IMAGETYPE_JPEG: // 2
  466. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
  467. $extImgTarget = (preg_match('/\.jpeg$/i',$file)?'.jpeg':'.jpg');
  468. $newquality=$quality;
  469. break;
  470. case IMAGETYPE_PNG: // 3
  471. imagealphablending($imgThumb,false); // Pour compatibilite sur certain systeme
  472. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
  473. $extImgTarget = '.png';
  474. $newquality=$quality-100;
  475. $newquality=round(abs($quality-100)*9/100);
  476. break;
  477. case IMAGETYPE_BMP: // 6
  478. // Not supported by PHP GD
  479. $extImgTarget = '.bmp';
  480. $newquality='NU';
  481. break;
  482. case IMAGETYPE_WBMP: // 15
  483. $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
  484. $extImgTarget = '.bmp';
  485. $newquality='NU';
  486. break;
  487. }
  488. if (function_exists("imagefill")) imagefill($imgThumb, 0, 0, $trans_colour);
  489. dol_syslog("vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
  490. //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
  491. imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
  492. $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i','',$file); // On enleve extension quelquesoit la casse
  493. $fileName = basename($fileName);
  494. //$imgThumbName = $dirthumb.'/'.getImageFileNameForSize(basename($file), $extName, $extImgTarget); // Full path of thumb file
  495. $imgThumbName = getImageFileNameForSize($file, $extName, $extImgTarget); // Full path of thumb file
  496. // Check if permission are ok
  497. //$fp = fopen($imgThumbName, "w");
  498. //fclose($fp);
  499. // Create image on disk
  500. switch($targetformat)
  501. {
  502. case IMAGETYPE_GIF: // 1
  503. imagegif($imgThumb, $imgThumbName);
  504. break;
  505. case IMAGETYPE_JPEG: // 2
  506. imagejpeg($imgThumb, $imgThumbName, $newquality);
  507. break;
  508. case IMAGETYPE_PNG: // 3
  509. imagepng($imgThumb, $imgThumbName, $newquality);
  510. break;
  511. case IMAGETYPE_BMP: // 6
  512. // Not supported by PHP GD
  513. break;
  514. case IMAGETYPE_WBMP: // 15
  515. image2wbmp($imgThumb, $imgThumbName);
  516. break;
  517. }
  518. // Set permissions on file
  519. if (! empty($conf->global->MAIN_UMASK)) @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
  520. // Free memory. This does not delete image.
  521. imagedestroy($img);
  522. imagedestroy($imgThumb);
  523. return $imgThumbName;
  524. }