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