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