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