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