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