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