dolibarr  17.0.4
images.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2007 Regis Houssin <regis.houssin@inodbox.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 <https://www.gnu.org/licenses/>.
17  * or see https://www.gnu.org/
18  */
19 
25 // Define size of logo small and mini
26 // TODO Remove this and call getDefaultImageSizes() instead
27 $maxwidthsmall = 480;
28 $maxheightsmall = 270; // Near 16/9eme
29 $maxwidthmini = 128;
30 $maxheightmini = 72; // 16/9eme
31 $quality = 80;
32 
39 {
40  $maxwidthsmall = 480;
41  $maxheightsmall = 270; // Near 16/9eme
42  $maxwidthmini = 128;
43  $maxheightmini = 72; // 16/9eme
44  $quality = 80;
45 
46  return array(
47  'maxwidthsmall' => $maxwidthsmall,
48  'maxheightsmall' => $maxheightsmall,
49  'maxwidthmini' => $maxwidthmini,
50  'maxheightmini' => $maxheightmini,
51  'quality' => $quality
52  );
53 }
54 
61 function getListOfPossibleImageExt($acceptsvg = 0)
62 {
63  global $conf;
64 
65  $regeximgext = '\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm'; // See also into product.class.php
66  if ($acceptsvg || !empty($conf->global->MAIN_ALLOW_SVG_FILES_AS_IMAGES)) {
67  $regeximgext .= '|\.svg'; // Not allowed by default. SVG can contains javascript
68  }
69 
70  return $regeximgext;
71 }
72 
80 function image_format_supported($file, $acceptsvg = 0)
81 {
82  $regeximgext = getListOfPossibleImageExt();
83 
84  // Case filename is not a format image
85  $reg = array();
86  if (!preg_match('/('.$regeximgext.')$/i', $file, $reg)) {
87  return -1;
88  }
89 
90  // Case filename is a format image but not supported by this PHP
91  $imgfonction = '';
92  if (strtolower($reg[1]) == '.gif') {
93  $imgfonction = 'imagecreatefromgif';
94  }
95  if (strtolower($reg[1]) == '.jpg') {
96  $imgfonction = 'imagecreatefromjpeg';
97  }
98  if (strtolower($reg[1]) == '.jpeg') {
99  $imgfonction = 'imagecreatefromjpeg';
100  }
101  if (strtolower($reg[1]) == '.png') {
102  $imgfonction = 'imagecreatefrompng';
103  }
104  if (strtolower($reg[1]) == '.bmp') {
105  $imgfonction = 'imagecreatefromwbmp';
106  }
107  if (strtolower($reg[1]) == '.webp') {
108  $imgfonction = 'imagecreatefromwebp';
109  }
110  if (strtolower($reg[1]) == '.xpm') {
111  $imgfonction = 'imagecreatefromxpm';
112  }
113  if (strtolower($reg[1]) == '.xbm') {
114  $imgfonction = 'imagecreatefromxbm';
115  }
116  if (strtolower($reg[1]) == '.svg') {
117  $imgfonction = 'imagecreatefromsvg'; // Never available
118  }
119  if ($imgfonction) {
120  if (!function_exists($imgfonction)) {
121  // Fonctions of conversion not available in this PHP
122  return 0;
123  }
124 
125  // Filename is a format image and supported for conversion by this PHP
126  return 1;
127  }
128 
129  return 0;
130 }
131 
132 
140 function dol_getImageSize($file, $url = false)
141 {
142  $ret = array();
143 
144  if (image_format_supported($file) < 0) {
145  return $ret;
146  }
147 
148  $filetoread = $file;
149  if (!$url) {
150  $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image
151  }
152 
153  if ($filetoread) {
154  $infoImg = getimagesize($filetoread); // Recuperation des infos de l'image
155  if ($infoImg) {
156  $ret['width'] = $infoImg[0]; // Largeur de l'image
157  $ret['height'] = $infoImg[1]; // Hauteur de l'image
158  } else {
159  $ret['width'] = $ret['height'] = '';
160  }
161  }
162 
163  return $ret;
164 }
165 
166 
181 function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite = '', $newquality = 0)
182 {
183  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
184 
185  global $conf, $langs;
186 
187  dol_syslog("dol_imageResizeOrCrop file=".$file." mode=".$mode." newWidth=".$newWidth." newHeight=".$newHeight." src_x=".$src_x." src_y=".$src_y);
188 
189  // Clean parameters
190  $file = trim($file);
191 
192  // Check parameters
193  if (!$file) {
194  // Si le fichier n'a pas ete indique
195  return 'Bad parameter file';
196  } elseif (!file_exists($file)) {
197  // Si le fichier passe en parametre n'existe pas
198  return $langs->trans("ErrorFileNotFound", $file);
199  } elseif (image_format_supported($file) < 0) {
200  return 'This filename '.$file.' does not seem to be an image filename.';
201  } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
202  return 'Wrong value for parameter newWidth or newHeight';
203  } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
204  return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
205  } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
206  return 'Both newHeight or newWidth must be defined for croping';
207  }
208 
209  $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image
210 
211  $infoImg = getimagesize($filetoread); // Get data about src image
212  $imgWidth = $infoImg[0]; // Largeur de l'image
213  $imgHeight = $infoImg[1]; // Hauteur de l'image
214 
215  $imgTargetName = ($filetowrite ? $filetowrite : $file);
216  $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
217 
218  if ($mode == 0) { // If resize, we check parameters
219  if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
220  $newWidth = $imgWidth;
221  $newHeight = $imgHeight;
222  }
223 
224  if ($newWidth <= 0) {
225  $newWidth = intval(($newHeight / $imgHeight) * $imgWidth); // Keep ratio
226  }
227  if ($newHeight <= 0) {
228  $newHeight = intval(($newWidth / $imgWidth) * $imgHeight); // Keep ratio
229  }
230  }
231 
232  // Test function to read source image exists
233  $imgfonction = '';
234  switch ($infoImg[2]) {
235  case 1: // IMG_GIF
236  $imgfonction = 'imagecreatefromgif';
237  break;
238  case 2: // IMG_JPG
239  $imgfonction = 'imagecreatefromjpeg';
240  break;
241  case 3: // IMG_PNG
242  $imgfonction = 'imagecreatefrompng';
243  break;
244  case 4: // IMG_WBMP
245  $imgfonction = 'imagecreatefromwbmp';
246  break;
247  case 18: // IMG_WEBP
248  $imgfonction = 'imagecreatefromwebp';
249  break;
250  }
251  if ($imgfonction) {
252  if (!function_exists($imgfonction)) {
253  // Fonctions de conversion non presente dans ce PHP
254  return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
255  }
256  }
257 
258  // Test function to write target image exists
259  if ($filetowrite) {
260  $imgfonction = '';
261  switch ($newExt) {
262  case 'gif': // IMG_GIF
263  $imgfonction = 'imagecreatefromgif';
264  break;
265  case 'jpg': // IMG_JPG
266  case 'jpeg': // IMG_JPEG
267  $imgfonction = 'imagecreatefromjpeg';
268  break;
269  case 'png': // IMG_PNG
270  $imgfonction = 'imagecreatefrompng';
271  break;
272  case 'bmp': // IMG_WBMP
273  $imgfonction = 'imagecreatefromwbmp';
274  break;
275  case 'webp': // IMG_WEBP
276  $imgfonction = 'imagecreatefromwebp';
277  break;
278  }
279  if ($imgfonction) {
280  if (!function_exists($imgfonction)) {
281  // Fonctions de conversion non presente dans ce PHP
282  return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
283  }
284  }
285  }
286 
287  // Read source image file
288  switch ($infoImg[2]) {
289  case 1: // Gif
290  $img = imagecreatefromgif($filetoread);
291  $extImg = '.gif'; // File name extension of image
292  break;
293  case 2: // Jpg
294  $img = imagecreatefromjpeg($filetoread);
295  $extImg = '.jpg';
296  break;
297  case 3: // Png
298  $img = imagecreatefrompng($filetoread);
299  $extImg = '.png';
300  break;
301  case 4: // Bmp
302  $img = imagecreatefromwbmp($filetoread);
303  $extImg = '.bmp';
304  break;
305  case 18: // Webp
306  $img = imagecreatefromwebp($filetoread);
307  $extImg = '.webp';
308  break;
309  }
310 
311  // Create empty image for target
312  if ($newExt == 'gif') {
313  // Compatibility image GIF
314  $imgTarget = imagecreate($newWidth, $newHeight);
315  } else {
316  $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
317  }
318 
319  // Activate antialiasing for better quality
320  if (function_exists('imageantialias')) {
321  imageantialias($imgTarget, true);
322  }
323 
324  // This is to keep transparent alpha channel if exists (PHP >= 4.2)
325  if (function_exists('imagesavealpha')) {
326  imagesavealpha($imgTarget, true);
327  }
328 
329  // Set transparent color according to image extension
330  $trans_colour = -1; // By default, undefined
331  switch ($newExt) {
332  case 'gif': // Gif
333  $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255); // On procede autrement pour le format GIF
334  imagecolortransparent($imgTarget, $trans_colour);
335  break;
336  case 'jpg': // Jpg
337  case 'jpeg': // Jpeg
338  $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
339  break;
340  case 'png': // Png
341  imagealphablending($imgTarget, false); // Pour compatibilite sur certain systeme
342  $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127); // Keep transparent channel
343  break;
344  case 'bmp': // Bmp
345  $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
346  break;
347  case 'webp': // Webp
348  $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
349  break;
350  }
351  if (function_exists("imagefill") && $trans_colour > 0) {
352  imagefill($imgTarget, 0, 0, $trans_colour);
353  }
354 
355  dol_syslog("dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
356  //imagecopyresized($imgTarget, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
357  imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight)); // Insere l'image de base redimensionnee
358 
359  // Check if permission are ok
360  //$fp = fopen($imgTargetName, "w");
361  //fclose($fp);
362 
363  // Create image on disk (overwrite file if exists)
364  switch ($newExt) {
365  case 'gif': // Gif
366  $newquality = 'NU'; // Quality is not used for this format
367  imagegif($imgTarget, $imgTargetName);
368  break;
369  case 'jpg': // Jpg
370  case 'jpeg': // Jpeg
371  $newquality = ($newquality ? $newquality : '100'); // % quality maximum
372  imagejpeg($imgTarget, $imgTargetName, $newquality);
373  break;
374  case 'png': // Png
375  $newquality = 0; // No compression (0-9)
376  imagepng($imgTarget, $imgTargetName, $newquality);
377  break;
378  case 'bmp': // Bmp
379  $newquality = 'NU'; // Quality is not used for this format
380  imagewbmp($imgTarget, $imgTargetName);
381  break;
382  case 'webp': // Webp
383  $newquality = ($newquality ? $newquality : '100'); // % quality maximum
384  imagewebp($imgTarget, $imgTargetName, $newquality);
385  break;
386  default:
387  dol_syslog("images.lib.php::imageResizeOrCrop() Format ".$newExt." is not supported", LOG_WARNING);
388  }
389 
390  // Set permissions on file
391  if (!empty($conf->global->MAIN_UMASK)) {
392  @chmod($imgTargetName, octdec($conf->global->MAIN_UMASK));
393  }
394 
395  // Free memory. This does not delete image.
396  imagedestroy($img);
397  imagedestroy($imgTarget);
398 
399  clearstatcache(); // File was replaced by a modified one, so we clear file caches.
400 
401  return $imgTargetName;
402 }
403 
404 
413 function dolRotateImage($file_path)
414 {
415  return correctExifImageOrientation($file_path, $file_path);
416 }
417 
418 
427 function correctExifImageOrientation($fileSource, $fileDest, $quality = 95)
428 {
429  if (function_exists('exif_read_data')) {
430  $exif = @exif_read_data($fileSource);
431  if ($exif && isset($exif['Orientation'])) {
432  $infoImg = getimagesize($fileSource); // Get image infos
433 
434  $orientation = $exif['Orientation'];
435  if ($orientation != 1) {
436  $img = imagecreatefromjpeg($fileSource);
437  $deg = 0;
438  switch ($orientation) {
439  case 3:
440  $deg = 180;
441  break;
442  case 6:
443  $deg = 270;
444  break;
445  case 8:
446  $deg = 90;
447  break;
448  }
449  if ($deg) {
450  if ($infoImg[2] === 'IMAGETYPE_PNG') { // In fact there is no exif on PNG but just in case
451  imagealphablending($img, false);
452  imagesavealpha($img, true);
453  $img = imagerotate($img, $deg, imageColorAllocateAlpha($img, 0, 0, 0, 127));
454  imagealphablending($img, false);
455  imagesavealpha($img, true);
456  } else {
457  $img = imagerotate($img, $deg, 0);
458  }
459  }
460  // then rewrite the rotated image back to the disk as $fileDest
461  if ($fileDest === false) {
462  return $img;
463  } else {
464  // In fact there exif is only for JPG but just in case
465  // Create image on disk
466  $image = false;
467 
468  switch ($infoImg[2]) {
469  case IMAGETYPE_GIF: // 1
470  $image = imagegif($img, $fileDest);
471  break;
472  case IMAGETYPE_JPEG: // 2
473  $image = imagejpeg($img, $fileDest, $quality);
474  break;
475  case IMAGETYPE_PNG: // 3
476  $image = imagepng($img, $fileDest, $quality);
477  break;
478  case IMAGETYPE_BMP: // 6
479  // Not supported by PHP GD
480  break;
481  case IMAGETYPE_WBMP: // 15
482  $image = imagewbmp($img, $fileDest);
483  break;
484  }
485 
486  // Free up memory (imagedestroy does not delete files):
487  @imagedestroy($img);
488 
489  return $image;
490  }
491  } // if there is some rotation necessary
492  } // if have the exif orientation info
493  } // if function exists
494 
495  return false;
496 }
497 
511 function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName = '_small', $quality = 50, $outdir = 'thumbs', $targetformat = 0)
512 {
513  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
514 
515  global $conf, $langs;
516 
517  dol_syslog("vignette file=".$file." extName=".$extName." maxWidth=".$maxWidth." maxHeight=".$maxHeight." quality=".$quality." outdir=".$outdir." targetformat=".$targetformat);
518 
519  // Clean parameters
520  $file = trim($file);
521 
522  // Check parameters
523  if (!$file) {
524  // Si le fichier n'a pas ete indique
525  return 'ErrorBadParameters';
526  } elseif (!file_exists($file)) {
527  // Si le fichier passe en parametre n'existe pas
528  dol_syslog($langs->trans("ErrorFileNotFound", $file), LOG_ERR);
529  return $langs->trans("ErrorFileNotFound", $file);
530  } elseif (image_format_supported($file) < 0) {
531  dol_syslog('This file '.$file.' does not seem to be an image format file name.', LOG_WARNING);
532  return 'ErrorBadImageFormat';
533  } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
534  // Si la largeur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
535  dol_syslog('Wrong value for parameter maxWidth', LOG_ERR);
536  return 'Error: Wrong value for parameter maxWidth';
537  } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
538  // Si la hauteur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
539  dol_syslog('Wrong value for parameter maxHeight', LOG_ERR);
540  return 'Error: Wrong value for parameter maxHeight';
541  }
542 
543  $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image
544 
545  $infoImg = getimagesize($filetoread); // Recuperation des infos de l'image
546  $imgWidth = $infoImg[0]; // Largeur de l'image
547  $imgHeight = $infoImg[1]; // Hauteur de l'image
548 
549  $ort = false;
550  if (function_exists('exif_read_data')) {
551  $exif = @exif_read_data($filetoread);
552  if ($exif && !empty($exif['Orientation'])) {
553  $ort = $exif['Orientation'];
554  }
555  }
556 
557  if ($maxWidth == -1) {
558  $maxWidth = $infoImg[0]; // If size is -1, we keep unchanged
559  }
560  if ($maxHeight == -1) {
561  $maxHeight = $infoImg[1]; // If size is -1, we keep unchanged
562  }
563 
564  // Si l'image est plus petite que la largeur et la hauteur max, on ne cree pas de vignette
565  if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
566  // On cree toujours les vignettes
567  dol_syslog("File size is smaller than thumb size", LOG_DEBUG);
568  //return 'Le fichier '.$file.' ne necessite pas de creation de vignette';
569  }
570 
571  $imgfonction = '';
572  switch ($infoImg[2]) {
573  case IMAGETYPE_GIF: // 1
574  $imgfonction = 'imagecreatefromgif';
575  break;
576  case IMAGETYPE_JPEG: // 2
577  $imgfonction = 'imagecreatefromjpeg';
578  break;
579  case IMAGETYPE_PNG: // 3
580  $imgfonction = 'imagecreatefrompng';
581  break;
582  case IMAGETYPE_BMP: // 6
583  // Not supported by PHP GD
584  break;
585  case IMAGETYPE_WBMP: // 15
586  $imgfonction = 'imagecreatefromwbmp';
587  break;
588  }
589  if ($imgfonction) {
590  if (!function_exists($imgfonction)) {
591  // Fonctions de conversion non presente dans ce PHP
592  return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
593  }
594  }
595 
596  // On cree le repertoire contenant les vignettes
597  $dirthumb = dirname($file).($outdir ? '/'.$outdir : ''); // Chemin du dossier contenant les vignettes
598  dol_mkdir($dirthumb);
599 
600  // Initialisation des variables selon l'extension de l'image
601  $img = null;
602  switch ($infoImg[2]) {
603  case IMAGETYPE_GIF: // 1
604  $img = imagecreatefromgif($filetoread);
605  $extImg = '.gif'; // Extension de l'image
606  break;
607  case IMAGETYPE_JPEG: // 2
608  $img = imagecreatefromjpeg($filetoread);
609  $extImg = (preg_match('/\.jpeg$/', $file) ? '.jpeg' : '.jpg'); // Extension de l'image
610  break;
611  case IMAGETYPE_PNG: // 3
612  $img = imagecreatefrompng($filetoread);
613  $extImg = '.png';
614  break;
615  case IMAGETYPE_BMP: // 6
616  // Not supported by PHP GD
617  $extImg = '.bmp';
618  break;
619  case IMAGETYPE_WBMP: // 15
620  $img = imagecreatefromwbmp($filetoread);
621  $extImg = '.bmp';
622  break;
623  }
624 
625  // Before PHP8, img was a resource, With PHP8, it is a GdImage
626  if (!is_resource($img) && !($img instanceof \GdImage)) {
627  dol_syslog('Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
628  return 0;
629  }
630 
631  $exifAngle = false;
632  if ($ort && !empty($conf->global->MAIN_USE_EXIF_ROTATION)) {
633  switch ($ort) {
634  case 3: // 180 rotate left
635  $exifAngle = 180;
636  break;
637  case 6: // 90 rotate right
638  $exifAngle = -90;
639  // changing sizes
640  $trueImgWidth = $infoImg[1];
641  $trueImgHeight = $infoImg[0];
642  break;
643  case 8: // 90 rotate left
644  $exifAngle = 90;
645  // changing sizes
646  $trueImgWidth = $infoImg[1]; // Largeur de l'image
647  $trueImgHeight = $infoImg[0]; // Hauteur de l'image
648  break;
649  }
650  }
651 
652  if ($exifAngle) {
653  $rotated = false;
654 
655  if ($infoImg[2] === 'IMAGETYPE_PNG') { // In fact there is no exif on PNG but just in case
656  imagealphablending($img, false);
657  imagesavealpha($img, true);
658  $rotated = imagerotate($img, $exifAngle, imageColorAllocateAlpha($img, 0, 0, 0, 127));
659  imagealphablending($rotated, false);
660  imagesavealpha($rotated, true);
661  } else {
662  $rotated = imagerotate($img, $exifAngle, 0);
663  }
664 
665  // replace image with good orientation
666  if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
667  $img = $rotated;
668  $imgWidth = $trueImgWidth;
669  $imgHeight = $trueImgHeight;
670  }
671  }
672 
673  // Initialisation des dimensions de la vignette si elles sont superieures a l'original
674  if ($maxWidth > $imgWidth) {
675  $maxWidth = $imgWidth;
676  }
677  if ($maxHeight > $imgHeight) {
678  $maxHeight = $imgHeight;
679  }
680 
681  $whFact = $maxWidth / $maxHeight; // Facteur largeur/hauteur des dimensions max de la vignette
682  $imgWhFact = $imgWidth / $imgHeight; // Facteur largeur/hauteur de l'original
683 
684  // Fixe les dimensions de la vignette
685  if ($whFact < $imgWhFact) {
686  // Si largeur determinante
687  $thumbWidth = $maxWidth;
688  $thumbHeight = $thumbWidth / $imgWhFact;
689  } else {
690  // Si hauteur determinante
691  $thumbHeight = $maxHeight;
692  $thumbWidth = $thumbHeight * $imgWhFact;
693  }
694  $thumbHeight = round($thumbHeight);
695  $thumbWidth = round($thumbWidth);
696 
697  // Define target format
698  if (empty($targetformat)) {
699  $targetformat = $infoImg[2];
700  }
701 
702  // Create empty image
703  if ($targetformat == IMAGETYPE_GIF) {
704  // Compatibilite image GIF
705  $imgThumb = imagecreate($thumbWidth, $thumbHeight);
706  } else {
707  $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
708  }
709 
710  // Activate antialiasing for better quality
711  if (function_exists('imageantialias')) {
712  imageantialias($imgThumb, true);
713  }
714 
715  // This is to keep transparent alpha channel if exists (PHP >= 4.2)
716  if (function_exists('imagesavealpha')) {
717  imagesavealpha($imgThumb, true);
718  }
719 
720  // Initialisation des variables selon l'extension de l'image
721  // $targetformat is 0 by default, in such case, we keep original extension
722  switch ($targetformat) {
723  case IMAGETYPE_GIF: // 1
724  $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // On procede autrement pour le format GIF
725  imagecolortransparent($imgThumb, $trans_colour);
726  $extImgTarget = '.gif';
727  $newquality = 'NU';
728  break;
729  case IMAGETYPE_JPEG: // 2
730  $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
731  $extImgTarget = (preg_match('/\.jpeg$/i', $file) ? '.jpeg' : '.jpg');
732  $newquality = $quality;
733  break;
734  case IMAGETYPE_PNG: // 3
735  imagealphablending($imgThumb, false); // Pour compatibilite sur certain systeme
736  $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
737  $extImgTarget = '.png';
738  $newquality = $quality - 100;
739  $newquality = round(abs($quality - 100) * 9 / 100);
740  break;
741  case IMAGETYPE_BMP: // 6
742  // Not supported by PHP GD
743  $extImgTarget = '.bmp';
744  $newquality = 'NU';
745  break;
746  case IMAGETYPE_WBMP: // 15
747  $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
748  $extImgTarget = '.bmp';
749  $newquality = 'NU';
750  break;
751  }
752  if (function_exists("imagefill")) {
753  imagefill($imgThumb, 0, 0, $trans_colour);
754  }
755 
756  dol_syslog("vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
757  //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
758  imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
759 
760  $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i', '', $file); // On enleve extension quelquesoit la casse
761  $fileName = basename($fileName);
762  //$imgThumbName = $dirthumb.'/'.getImageFileNameForSize(basename($file), $extName, $extImgTarget); // Full path of thumb file
763  $imgThumbName = getImageFileNameForSize($file, $extName, $extImgTarget); // Full path of thumb file
764 
765 
766  // Check if permission are ok
767  //$fp = fopen($imgThumbName, "w");
768  //fclose($fp);
769 
770  // Create image on disk
771  switch ($targetformat) {
772  case IMAGETYPE_GIF: // 1
773  imagegif($imgThumb, $imgThumbName);
774  break;
775  case IMAGETYPE_JPEG: // 2
776  imagejpeg($imgThumb, $imgThumbName, $newquality);
777  break;
778  case IMAGETYPE_PNG: // 3
779  imagepng($imgThumb, $imgThumbName, $newquality);
780  break;
781  case IMAGETYPE_BMP: // 6
782  // Not supported by PHP GD
783  break;
784  case IMAGETYPE_WBMP: // 15
785  imagewbmp($imgThumb, $imgThumbName);
786  break;
787  }
788 
789  // Set permissions on file
790  if (!empty($conf->global->MAIN_UMASK)) {
791  @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
792  }
793 
794  // Free memory. This does not delete image.
795  imagedestroy($img);
796  imagedestroy($imgThumb);
797 
798  return $imgThumbName;
799 }
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
getImageFileNameForSize($file, $extName, $extImgTarget='')
Return the filename of file to get the thumbs.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
vignette($file, $maxWidth=160, $maxHeight=120, $extName='_small', $quality=50, $outdir='thumbs', $targetformat=0)
Create a thumbnail from an image file (Supported extensions are gif, jpg, png and bmp).
Definition: images.lib.php:511
getListOfPossibleImageExt($acceptsvg=0)
Return if a filename is file name of a supported image format.
Definition: images.lib.php:61
getDefaultImageSizes()
Return default values for image sizes.
Definition: images.lib.php:38
correctExifImageOrientation($fileSource, $fileDest, $quality=95)
Add exif orientation correction for image.
Definition: images.lib.php:427
dolRotateImage($file_path)
dolRotateImage if image is a jpg file.
Definition: images.lib.php:413
dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x=0, $src_y=0, $filetowrite='', $newquality=0)
Resize or crop an image file (Supported extensions are gif, jpg, png, bmp and webp)
Definition: images.lib.php:181
dol_getImageSize($file, $url=false)
Return size of image file on disk (Supported extensions are gif, jpg, png, bmp and webp)
Definition: images.lib.php:140
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.
Definition: images.lib.php:80