dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
27// Define size of logo small and mini
28// TODO Remove this and call getDefaultImageSizes() instead
29$maxwidthsmall = 480;
30$maxheightsmall = 270; // Near 16/9eme
31$maxwidthmini = 128;
32$maxheightmini = 72; // 16/9eme
33$quality = 80;
34
35if (!defined('IMAGETYPE_WEBP')) {
36 define('IMAGETYPE_WEBP', 18);
37}
38
39
46{
47 $maxwidthsmall = 480;
48 $maxheightsmall = 270; // Near 16/9eme
49 $maxwidthmini = 128;
50 $maxheightmini = 72; // 16/9eme
51 $quality = 80;
52
53 return array(
54 'maxwidthsmall' => $maxwidthsmall,
55 'maxheightsmall' => $maxheightsmall,
56 'maxwidthmini' => $maxwidthmini,
57 'maxheightmini' => $maxheightmini,
58 'quality' => $quality
59 );
60}
61
68function getListOfPossibleImageExt($acceptsvg = 0)
69{
70 $regeximgext = '\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm'; // See also into product.class.php
71 if ($acceptsvg || getDolGlobalString('MAIN_ALLOW_SVG_FILES_AS_IMAGES')) {
72 $regeximgext .= '|\.svg'; // Not allowed by default. SVG can contains javascript
73 }
74
75 return $regeximgext;
76}
77
85function image_format_supported($file, $acceptsvg = 0)
86{
87 $regeximgext = getListOfPossibleImageExt();
88
89 // Case filename is not a format image
90 $reg = array();
91 if (!preg_match('/('.$regeximgext.')$/i', $file, $reg)) {
92 return -1;
93 }
94
95 // Case filename is a format image but not supported by this PHP
96 $imgfonction = '';
97 if (strtolower($reg[1]) == '.gif') {
98 $imgfonction = 'imagecreatefromgif';
99 }
100 if (strtolower($reg[1]) == '.jpg') {
101 $imgfonction = 'imagecreatefromjpeg';
102 }
103 if (strtolower($reg[1]) == '.jpeg') {
104 $imgfonction = 'imagecreatefromjpeg';
105 }
106 if (strtolower($reg[1]) == '.png') {
107 $imgfonction = 'imagecreatefrompng';
108 }
109 if (strtolower($reg[1]) == '.bmp') {
110 $imgfonction = 'imagecreatefromwbmp';
111 }
112 if (strtolower($reg[1]) == '.webp') {
113 $imgfonction = 'imagecreatefromwebp';
114 }
115 if (strtolower($reg[1]) == '.xpm') {
116 $imgfonction = 'imagecreatefromxpm';
117 }
118 if (strtolower($reg[1]) == '.xbm') {
119 $imgfonction = 'imagecreatefromxbm';
120 }
121 if (strtolower($reg[1]) == '.svg') {
122 $imgfonction = 'imagecreatefromsvg'; // Never available
123 }
124 if ($imgfonction) {
125 if (!function_exists($imgfonction)) {
126 // Functions of conversion not available in this PHP
127 return 0;
128 }
129
130 // Filename is a format image and supported for conversion by this PHP
131 return 1;
132 }
133
134 return 0;
135}
136
137
145function dol_getImageSize($file, $url = false)
146{
147 $ret = array();
148
149 if (image_format_supported($file) < 0) {
150 return $ret;
151 }
152
153 $filetoread = $file;
154 if (!$url) {
155 $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image
156 }
157
158 if ($filetoread) {
159 $infoImg = getimagesize($filetoread); // Recuperation des infos de l'image
160 if ($infoImg) {
161 $ret['width'] = $infoImg[0]; // Largeur de l'image
162 $ret['height'] = $infoImg[1]; // Hauteur de l'image
163 } else {
164 $ret['width'] = $ret['height'] = '';
165 }
166 }
167
168 return $ret;
169}
170
171
186function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite = '', $newquality = 0)
187{
188 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
189
190 global $langs;
191
192 dol_syslog("dol_imageResizeOrCrop file=".$file." mode=".$mode." newWidth=".$newWidth." newHeight=".$newHeight." src_x=".$src_x." src_y=".$src_y);
193
194 // Clean parameters
195 $file = trim($file);
196
197 // Check parameters
198 if (!$file) {
199 // Si le fichier n'a pas ete indique
200 return 'Bad parameter file';
201 } elseif (!file_exists($file)) {
202 // Si le fichier passe en parameter n'existe pas
203 return $langs->trans("ErrorFileNotFound", $file);
204 } elseif (image_format_supported($file) < 0) {
205 return 'This filename '.$file.' does not seem to be an image filename.';
206 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
207 return 'Wrong value for parameter newWidth or newHeight';
208 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
209 return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
210 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
211 return 'Both newHeight or newWidth must be defined for croping';
212 }
213
214 $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image
215
216 $infoImg = getimagesize($filetoread); // Get data about src image
217 $imgWidth = $infoImg[0]; // Largeur de l'image
218 $imgHeight = $infoImg[1]; // Hauteur de l'image
219
220 $imgTargetName = ($filetowrite ? $filetowrite : $file);
221 $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
222
223 if ($mode == 0) { // If resize, we check parameters
224 if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
225 $newWidth = $imgWidth;
226 $newHeight = $imgHeight;
227 }
228
229 if ($newWidth <= 0) {
230 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth); // Keep ratio
231 }
232 if ($newHeight <= 0) {
233 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight); // Keep ratio
234 }
235 }
236
237 // Test function to read source image exists
238 $imgfonction = '';
239 switch ($infoImg[2]) {
240 case 1: // IMG_GIF
241 $imgfonction = 'imagecreatefromgif';
242 break;
243 case 2: // IMG_JPG
244 $imgfonction = 'imagecreatefromjpeg';
245 break;
246 case 3: // IMG_PNG
247 $imgfonction = 'imagecreatefrompng';
248 break;
249 case 4: // IMG_WBMP
250 $imgfonction = 'imagecreatefromwbmp';
251 break;
252 case 18: // IMG_WEBP
253 $imgfonction = 'imagecreatefromwebp';
254 break;
255 }
256 if ($imgfonction) {
257 if (!function_exists($imgfonction)) {
258 // Functions de conversion non presente dans ce PHP
259 return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
260 }
261 }
262
263 // Test function to write target image exists
264 if ($filetowrite) {
265 $imgfonction = '';
266 switch ($newExt) {
267 case 'gif': // IMG_GIF
268 $imgfonction = 'imagecreatefromgif';
269 break;
270 case 'jpg': // IMG_JPG
271 case 'jpeg': // IMG_JPEG
272 $imgfonction = 'imagecreatefromjpeg';
273 break;
274 case 'png': // IMG_PNG
275 $imgfonction = 'imagecreatefrompng';
276 break;
277 case 'bmp': // IMG_WBMP
278 $imgfonction = 'imagecreatefromwbmp';
279 break;
280 case 'webp': // IMG_WEBP
281 $imgfonction = 'imagecreatefromwebp';
282 break;
283 }
284 if ($imgfonction) {
285 if (!function_exists($imgfonction)) {
286 // Functions de conversion non presente dans ce PHP
287 return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
288 }
289 }
290 }
291
292 // Read source image file
293 $img = null;
294 $extImg = null;
295 switch ($infoImg[2]) {
296 case 1: // Gif
297 $img = imagecreatefromgif($filetoread);
298 $extImg = '.gif'; // File name extension of image
299 break;
300 case 2: // Jpg
301 $img = imagecreatefromjpeg($filetoread);
302 $extImg = '.jpg';
303 break;
304 case 3: // Png
305 $img = imagecreatefrompng($filetoread);
306 $extImg = '.png';
307 break;
308 case 4: // Bmp
309 $img = imagecreatefromwbmp($filetoread);
310 $extImg = '.bmp';
311 break;
312 case 18: // Webp
313 $img = imagecreatefromwebp($filetoread);
314 $extImg = '.webp';
315 break;
316 }
317
318 // Create empty image for target
319 if ($newExt == 'gif') {
320 // Compatibility image GIF
321 $imgTarget = imagecreate($newWidth, $newHeight);
322 } else {
323 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
324 }
325
326 // Activate antialiasing for better quality
327 if (function_exists('imageantialias')) {
328 imageantialias($imgTarget, true);
329 }
330
331 // This is to keep transparent alpha channel if exists (PHP >= 4.2)
332 if (function_exists('imagesavealpha')) {
333 imagesavealpha($imgTarget, true);
334 }
335
336 // Set transparent color according to image extension
337 $trans_colour = -1; // By default, undefined
338 switch ($newExt) {
339 case 'gif': // Gif
340 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255); // The method is different for the GIF format
341 imagecolortransparent($imgTarget, $trans_colour);
342 break;
343 case 'jpg': // Jpg
344 case 'jpeg': // Jpeg
345 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
346 break;
347 case 'png': // Png
348 imagealphablending($imgTarget, false); // For compatibility with certain systems
349 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127); // Keep transparent channel
350 break;
351 case 'bmp': // Bmp
352 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
353 break;
354 case 'webp': // Webp
355 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
356 break;
357 }
358 if (function_exists("imagefill") && $trans_colour > 0) {
359 imagefill($imgTarget, 0, 0, $trans_colour);
360 }
361
362 dol_syslog("dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
363 //imagecopyresized($imgTarget, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
364 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
365
366 // Check if permission are ok
367 //$fp = fopen($imgTargetName, "w");
368 //fclose($fp);
369
370 // Create image on disk (overwrite file if exists)
371 switch ($newExt) {
372 case 'gif': // Gif
373 $newquality = 'NU'; // Quality is not used for this format
374 imagegif($imgTarget, $imgTargetName);
375 break;
376 case 'jpg': // Jpg
377 case 'jpeg': // Jpeg
378 $newquality = ($newquality ? $newquality : '100'); // % quality maximum
379 imagejpeg($imgTarget, $imgTargetName, $newquality);
380 break;
381 case 'png': // Png
382 $newquality = 0; // No compression (0-9)
383 imagepng($imgTarget, $imgTargetName, $newquality);
384 break;
385 case 'bmp': // Bmp
386 $newquality = 'NU'; // Quality is not used for this format
387 imagewbmp($imgTarget, $imgTargetName);
388 break;
389 case 'webp': // Webp
390 $newquality = ($newquality ? $newquality : '100'); // % quality maximum
391 imagewebp($imgTarget, $imgTargetName, $newquality);
392 break;
393 default:
394 dol_syslog("images.lib.php::imageResizeOrCrop() Format ".$newExt." is not supported", LOG_WARNING);
395 }
396
397 // Set permissions on file
398 dolChmod($imgTargetName);
399
400 // Free memory. This does not delete image.
401 if ($img) {
402 imagedestroy($img);
403 }
404 if ($imgTarget) {
405 imagedestroy($imgTarget);
406 }
407
408 clearstatcache(); // File was replaced by a modified one, so we clear file caches.
409
410 return $imgTargetName;
411}
412
413
422function dolRotateImage($file_path)
423{
424 return correctExifImageOrientation($file_path, $file_path);
425}
426
427
436function correctExifImageOrientation($fileSource, $fileDest, $quality = 95)
437{
438 if (function_exists('exif_read_data')) {
439 $exif = @exif_read_data($fileSource);
440 if ($exif && isset($exif['Orientation'])) {
441 $infoImg = getimagesize($fileSource); // Get image infos
442
443 $orientation = $exif['Orientation'];
444 if ($orientation != 1) {
445 $img = imagecreatefromjpeg($fileSource);
446 $deg = 0;
447 switch ($orientation) {
448 case 3:
449 $deg = 180;
450 break;
451 case 6:
452 $deg = 270;
453 break;
454 case 8:
455 $deg = 90;
456 break;
457 }
458 if ($deg) {
459 if ($infoImg[2] === IMAGETYPE_PNG) { // In fact there is no exif on PNG but just in case
460 imagealphablending($img, false);
461 imagesavealpha($img, true);
462 $img = imagerotate($img, $deg, imagecolorallocatealpha($img, 0, 0, 0, 127));
463 imagealphablending($img, false);
464 imagesavealpha($img, true);
465 } else {
466 $img = imagerotate($img, $deg, 0);
467 }
468 }
469 // then rewrite the rotated image back to the disk as $fileDest
470 if ($fileDest === false) {
471 return $img;
472 } else {
473 // In fact there exif is only for JPG but just in case
474 // Create image on disk
475 $image = false;
476
477 switch ($infoImg[2]) {
478 case IMAGETYPE_GIF: // 1
479 $image = imagegif($img, $fileDest);
480 break;
481 case IMAGETYPE_JPEG: // 2
482 $image = imagejpeg($img, $fileDest, $quality);
483 break;
484 case IMAGETYPE_PNG: // 3
485 $image = imagepng($img, $fileDest, $quality);
486 break;
487 case IMAGETYPE_BMP: // 6
488 // Not supported by PHP GD
489 break;
490 case IMAGETYPE_WBMP: // 15
491 $image = imagewbmp($img, $fileDest);
492 break;
493 }
494
495 // Free up memory (imagedestroy does not delete files):
496 @imagedestroy($img);
497
498 return $image;
499 }
500 } // if there is some rotation necessary
501 } // if have the exif orientation info
502 } // if function exists
503
504 return false;
505}
506
520function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName = '_small', $quality = 50, $outdir = 'thumbs', $targetformat = 0)
521{
522 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
523
524 global $langs;
525
526 dol_syslog("vignette file=".$file." extName=".$extName." maxWidth=".$maxWidth." maxHeight=".$maxHeight." quality=".$quality." outdir=".$outdir." targetformat=".$targetformat);
527
528 // Clean parameters
529 $file = trim($file);
530
531 // Check parameters
532 if (!$file) {
533 // If the file has not been indicated
534 return 'ErrorBadParameters';
535 } elseif (!file_exists($file)) {
536 // If the file passed in parameter does not exist
537 dol_syslog($langs->trans("ErrorFileNotFound", $file), LOG_ERR);
538 return $langs->trans("ErrorFileNotFound", $file);
539 } elseif (image_format_supported($file) < 0) {
540 dol_syslog('This file '.$file.' does not seem to be an image format file name.', LOG_WARNING);
541 return 'ErrorBadImageFormat';
542 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
543 // If max width is incorrect (not numeric, empty, or less than 0)
544 dol_syslog('Wrong value for parameter maxWidth', LOG_ERR);
545 return 'Error: Wrong value for parameter maxWidth';
546 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
547 // If max height is incorrect (not numeric, empty, or less than 0)
548 dol_syslog('Wrong value for parameter maxHeight', LOG_ERR);
549 return 'Error: Wrong value for parameter maxHeight';
550 }
551
552 $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image
553
554 $infoImg = getimagesize($filetoread); // Recuperation des infos de l'image
555 $imgWidth = $infoImg[0]; // Largeur de l'image
556 $imgHeight = $infoImg[1]; // Hauteur de l'image
557
558 $ort = false;
559 if (function_exists('exif_read_data')) {
560 $exif = @exif_read_data($filetoread);
561 if ($exif && !empty($exif['Orientation'])) {
562 $ort = $exif['Orientation'];
563 }
564 }
565
566 if ($maxWidth == -1) {
567 $maxWidth = $infoImg[0]; // If size is -1, we keep unchanged
568 }
569 if ($maxHeight == -1) {
570 $maxHeight = $infoImg[1]; // If size is -1, we keep unchanged
571 }
572
573 // If the image is smaller than the maximum width and height, no thumbnail is created.
574 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
575 // On cree toujours les vignettes
576 dol_syslog("File size is smaller than thumb size", LOG_DEBUG);
577 //return 'Le fichier '.$file.' ne necessite pas de creation de vignette';
578 }
579
580 $imgfonction = '';
581 switch ($infoImg[2]) {
582 case IMAGETYPE_GIF: // 1
583 $imgfonction = 'imagecreatefromgif';
584 break;
585 case IMAGETYPE_JPEG: // 2
586 $imgfonction = 'imagecreatefromjpeg';
587 break;
588 case IMAGETYPE_PNG: // 3
589 $imgfonction = 'imagecreatefrompng';
590 break;
591 case IMAGETYPE_BMP: // 6
592 // Not supported by PHP GD
593 break;
594 case IMAGETYPE_WBMP: // 15
595 $imgfonction = 'imagecreatefromwbmp';
596 break;
597 case IMAGETYPE_WEBP: // 18
598 $imgfonction = 'imagecreatefromwebp';
599 break;
600 }
601 if ($imgfonction) {
602 if (!function_exists($imgfonction)) {
603 // Conversion functions not present in this PHP
604 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
605 }
606 }
607
608 // We create the directory containing the thumbnails
609 $dirthumb = dirname($file).($outdir ? '/'.$outdir : ''); // Path to thumbnail folder
610 dol_mkdir($dirthumb);
611
612 // Variable initialization according to image extension
613 $img = null;
614 switch ($infoImg[2]) {
615 case IMAGETYPE_GIF: // 1
616 $img = imagecreatefromgif($filetoread);
617 $extImg = '.gif';
618 break;
619 case IMAGETYPE_JPEG: // 2
620 $img = imagecreatefromjpeg($filetoread);
621 $extImg = (preg_match('/\.jpeg$/', $file) ? '.jpeg' : '.jpg');
622 break;
623 case IMAGETYPE_PNG: // 3
624 $img = imagecreatefrompng($filetoread);
625 $extImg = '.png';
626 break;
627 case IMAGETYPE_BMP: // 6
628 // Not supported by PHP GD
629 $extImg = '.bmp';
630 break;
631 case IMAGETYPE_WBMP: // 15
632 $img = imagecreatefromwbmp($filetoread);
633 $extImg = '.bmp';
634 break;
635 case IMAGETYPE_WEBP: // 18
636 $img = imagecreatefromwebp($filetoread);
637 $extImg = '.webp';
638 break;
639 }
640
641 // Before PHP8, img was a resource, With PHP8, it is a GdImage
642 // if (!is_resource($img) && class_exists('GdImage') && !($img instanceof GdImage)) {
643 if (is_null($img) || $img === false) {
644 dol_syslog('Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
645 return 0;
646 }
647
648 $exifAngle = false;
649 if ($ort && getDolGlobalString('MAIN_USE_EXIF_ROTATION')) {
650 switch ($ort) {
651 case 3: // 180 rotate left
652 $exifAngle = 180;
653 break;
654 case 6: // 90 rotate right
655 $exifAngle = -90;
656 // changing sizes
657 $trueImgWidth = $infoImg[1];
658 $trueImgHeight = $infoImg[0];
659 break;
660 case 8: // 90 rotate left
661 $exifAngle = 90;
662 // changing sizes
663 $trueImgWidth = $infoImg[1]; // Largeur de l'image
664 $trueImgHeight = $infoImg[0]; // Hauteur de l'image
665 break;
666 }
667 }
668
669 if ($exifAngle) {
670 $rotated = false;
671
672 if ($infoImg[2] === IMAGETYPE_PNG) { // In fact there is no exif on PNG but just in case
673 imagealphablending($img, false);
674 imagesavealpha($img, true);
675 $rotated = imagerotate($img, $exifAngle, imagecolorallocatealpha($img, 0, 0, 0, 127));
676 imagealphablending($rotated, false);
677 imagesavealpha($rotated, true);
678 } else {
679 $rotated = imagerotate($img, $exifAngle, 0);
680 }
681
682 // replace image with good orientation
683 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
684 $img = $rotated;
685 $imgWidth = $trueImgWidth;
686 $imgHeight = $trueImgHeight;
687 }
688 }
689
690 // Initialize thumbnail dimensions if larger than original
691 if ($maxWidth > $imgWidth) {
692 $maxWidth = $imgWidth;
693 }
694 if ($maxHeight > $imgHeight) {
695 $maxHeight = $imgHeight;
696 }
697
698 $whFact = $maxWidth / $maxHeight; // Width/height factor for maximum label dimensions
699 $imgWhFact = $imgWidth / $imgHeight; // Original width/height factor
700
701 // Set label dimensions
702 if ($whFact < $imgWhFact) {
703 // If determining width
704 $thumbWidth = $maxWidth;
705 $thumbHeight = $thumbWidth / $imgWhFact;
706 } else {
707 // If determining height
708 $thumbHeight = $maxHeight;
709 $thumbWidth = $thumbHeight * $imgWhFact;
710 }
711 $thumbHeight = (int) round($thumbHeight);
712 $thumbWidth = (int) round($thumbWidth);
713
714 // Define target format
715 if (empty($targetformat)) {
716 $targetformat = $infoImg[2];
717 }
718
719 // Create empty image
720 if ($targetformat == IMAGETYPE_GIF) {
721 // Compatibilite image GIF
722 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
723 } else {
724 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
725 }
726
727 // Activate antialiasing for better quality
728 if (function_exists('imageantialias')) {
729 imageantialias($imgThumb, true);
730 }
731
732 // This is to keep transparent alpha channel if exists (PHP >= 4.2)
733 if (function_exists('imagesavealpha')) {
734 imagesavealpha($imgThumb, true);
735 }
736
737 // Variable initialization according to image extension
738 // $targetformat is 0 by default, in such case, we keep original extension
739 $extImgTarget = null;
740 $trans_colour = false;
741 $newquality = null;
742 switch ($targetformat) {
743 case IMAGETYPE_GIF: // 1
744 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // The GIF format works differently
745 imagecolortransparent($imgThumb, $trans_colour);
746 $extImgTarget = '.gif';
747 $newquality = 'NU';
748 break;
749 case IMAGETYPE_JPEG: // 2
750 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
751 $extImgTarget = (preg_match('/\.jpeg$/i', $file) ? '.jpeg' : '.jpg');
752 $newquality = $quality;
753 break;
754 case IMAGETYPE_PNG: // 3
755 imagealphablending($imgThumb, false); // For compatibility on certain systems
756 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
757 $extImgTarget = '.png';
758 $newquality = $quality - 100;
759 $newquality = round(abs($quality - 100) * 9 / 100);
760 break;
761 case IMAGETYPE_BMP: // 6
762 // Not supported by PHP GD
763 $extImgTarget = '.bmp';
764 $newquality = 'NU';
765 break;
766 case IMAGETYPE_WBMP: // 15
767 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
768 $extImgTarget = '.bmp';
769 $newquality = 'NU';
770 break;
771 case IMAGETYPE_WEBP: // 18
772 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
773 $extImgTarget = '.webp';
774 $newquality = $quality;
775 break;
776 }
777 if (function_exists("imagefill") && $trans_colour !== false) {
778 imagefill($imgThumb, 0, 0, $trans_colour);
779 }
780
781 dol_syslog("vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
782 //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insert resized base image
783 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insert resized base image
784
785 $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i', '', $file); // We remove any extension box
786 $fileName = basename($fileName);
787 //$imgThumbName = $dirthumb.'/'.getImageFileNameForSize(basename($file), $extName, $extImgTarget); // Full path of thumb file
788 $imgThumbName = getImageFileNameForSize($file, $extName, $extImgTarget); // Full path of thumb file
789
790
791 // Check if permission are ok
792 //$fp = fopen($imgThumbName, "w");
793 //fclose($fp);
794
795 // Create image on disk
796 switch ($targetformat) {
797 case IMAGETYPE_GIF: // 1
798 imagegif($imgThumb, $imgThumbName);
799 break;
800 case IMAGETYPE_JPEG: // 2
801 imagejpeg($imgThumb, $imgThumbName, $newquality); // @phan-suppress-current-line PhanTypeMismatchArgumentNullableInternal,PhanPossiblyUndeclaredVariable
802 break;
803 case IMAGETYPE_PNG: // 3
804 imagepng($imgThumb, $imgThumbName, $newquality); // @phan-suppress-current-line PhanPossiblyUndeclaredVariable
805 break;
806 case IMAGETYPE_BMP: // 6
807 // Not supported by PHP GD
808 break;
809 case IMAGETYPE_WBMP: // 15
810 imagewbmp($imgThumb, $imgThumbName);
811 break;
812 case IMAGETYPE_WEBP: // 18
813 imagewebp($imgThumb, $imgThumbName, $newquality); // @phan-suppress-current-line PhanTypeMismatchArgumentNullableInternal,PhanPossiblyUndeclaredVariable
814 break;
815 }
816
817 // Set permissions on file
818 dolChmod($imgThumbName);
819
820 // Free memory. This does not delete image.
821 imagedestroy($img);
822 imagedestroy($imgThumb);
823
824 return $imgThumbName;
825}
826
827
836function imgAddEditDeleteButton($htmlid, $urledit, $urldelete)
837{
838 // TODO
839 return '';
840}
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.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.
if(!defined( 'IMAGETYPE_WEBP')) 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)
imgAddEditDeleteButton($htmlid, $urledit, $urldelete)
Beautify an image by adding a link edit and delete on image.
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.