dolibarr 21.0.3
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
521function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName = '_small', $quality = 50, $outdir = 'thumbs', $targetformat = 0)
522{
523 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
524
525 global $langs;
526
527 dol_syslog("vignette file=".$file." extName=".$extName." maxWidth=".$maxWidth." maxHeight=".$maxHeight." quality=".$quality." outdir=".$outdir." targetformat=".$targetformat);
528
529 // Clean parameters
530 $file = dol_sanitizePathName(trim($file));
531
532 // Check parameters
533 if (!$file) {
534 // If the file has not been indicated
535 return 'ErrorBadParameters';
536 } elseif (image_format_supported($file) < 0) {
537 dol_syslog('This file '.$file.' does not seem to be a supported image file name (bad extension).', LOG_WARNING);
538 return 'ErrorBadImageFormat';
539 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
540 // If max width is incorrect (not numeric, empty, or less than 0)
541 dol_syslog('Wrong value for parameter maxWidth', LOG_ERR);
542 return 'Error: Wrong value for parameter maxWidth';
543 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
544 // If max height is incorrect (not numeric, empty, or less than 0)
545 dol_syslog('Wrong value for parameter maxHeight', LOG_ERR);
546 return 'Error: Wrong value for parameter maxHeight';
547 }
548
549 $filetoread = realpath(dol_osencode($file)); // Absolute canonical path of image
550
551 if (!file_exists($filetoread)) {
552 // If the file passed in parameter does not exist
553 dol_syslog($langs->trans("ErrorFileNotFound", $filetoread), LOG_ERR);
554 return $langs->trans("ErrorFileNotFound", $filetoread);
555 }
556
557 $infoImg = getimagesize($filetoread); // Get information like size and real format of image. Warning real format may be png when extension is .jpg
558 $imgWidth = $infoImg[0]; // Width of image
559 $imgHeight = $infoImg[1]; // Height of image
560
561 $ort = false;
562 if (function_exists('exif_read_data')) {
563 $exif = @exif_read_data($filetoread);
564 if ($exif && !empty($exif['Orientation'])) {
565 $ort = $exif['Orientation'];
566 }
567 }
568
569 if ($maxWidth == -1) {
570 $maxWidth = $infoImg[0]; // If size is -1, we keep unchanged
571 }
572 if ($maxHeight == -1) {
573 $maxHeight = $infoImg[1]; // If size is -1, we keep unchanged
574 }
575
576 // If the image is smaller than the maximum width and height, no thumbnail is created.
577 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
578 // On cree toujours les vignettes
579 dol_syslog("File size is smaller than thumb size", LOG_DEBUG);
580 //return 'Le fichier '.$file.' ne necessite pas de creation de vignette';
581 }
582
583 $imgfonction = '';
584 switch ($infoImg[2]) {
585 case IMAGETYPE_GIF: // 1
586 $imgfonction = 'imagecreatefromgif';
587 break;
588 case IMAGETYPE_JPEG: // 2
589 $imgfonction = 'imagecreatefromjpeg';
590 break;
591 case IMAGETYPE_PNG: // 3
592 $imgfonction = 'imagecreatefrompng';
593 break;
594 case IMAGETYPE_BMP: // 6
595 // Not supported by PHP GD
596 break;
597 case IMAGETYPE_WBMP: // 15
598 $imgfonction = 'imagecreatefromwbmp';
599 break;
600 case IMAGETYPE_WEBP: // 18
601 $imgfonction = 'imagecreatefromwebp';
602 break;
603 }
604 if ($imgfonction) {
605 if (!function_exists($imgfonction)) {
606 // Conversion functions not present in this PHP
607 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
608 }
609 }
610
611 // We create the directory containing the thumbnails
612 $dirthumb = dirname($file).($outdir ? '/'.$outdir : ''); // Path to thumbnail folder
613 dol_mkdir($dirthumb);
614
615 // Variable initialization according to image extension
616 $img = null;
617 switch ($infoImg[2]) {
618 case IMAGETYPE_GIF: // 1
619 $img = imagecreatefromgif($filetoread);
620 $extImg = '.gif';
621 break;
622 case IMAGETYPE_JPEG: // 2
623 $img = imagecreatefromjpeg($filetoread);
624 $extImg = (preg_match('/\.jpeg$/', $file) ? '.jpeg' : '.jpg');
625 break;
626 case IMAGETYPE_PNG: // 3
627 $img = imagecreatefrompng($filetoread);
628 $extImg = '.png';
629 break;
630 case IMAGETYPE_BMP: // 6
631 // Not supported by PHP GD
632 $extImg = '.bmp';
633 break;
634 case IMAGETYPE_WBMP: // 15
635 $img = imagecreatefromwbmp($filetoread);
636 $extImg = '.bmp';
637 break;
638 case IMAGETYPE_WEBP: // 18
639 $img = imagecreatefromwebp($filetoread);
640 $extImg = '.webp';
641 break;
642 }
643
644 // Before PHP8, img was a resource, With PHP8, it is a GdImage
645 // if (!is_resource($img) && class_exists('GdImage') && !($img instanceof GdImage)) {
646 if (is_null($img) || $img === false) {
647 dol_syslog('Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
648 return 0;
649 }
650
651 $exifAngle = false;
652 if ($ort && getDolGlobalString('MAIN_USE_EXIF_ROTATION')) {
653 switch ($ort) {
654 case 3: // 180 rotate left
655 $exifAngle = 180;
656 break;
657 case 6: // 90 rotate right
658 $exifAngle = -90;
659 // changing sizes
660 $trueImgWidth = $infoImg[1];
661 $trueImgHeight = $infoImg[0];
662 break;
663 case 8: // 90 rotate left
664 $exifAngle = 90;
665 // changing sizes
666 $trueImgWidth = $infoImg[1]; // Largeur de l'image
667 $trueImgHeight = $infoImg[0]; // Hauteur de l'image
668 break;
669 }
670 }
671
672 if ($exifAngle) {
673 $rotated = false;
674
675 if ($infoImg[2] === IMAGETYPE_PNG) { // In fact there is no exif on PNG but just in case
676 imagealphablending($img, false);
677 imagesavealpha($img, true);
678 $rotated = imagerotate($img, $exifAngle, imagecolorallocatealpha($img, 0, 0, 0, 127));
679 imagealphablending($rotated, false);
680 imagesavealpha($rotated, true);
681 } else {
682 $rotated = imagerotate($img, $exifAngle, 0);
683 }
684
685 // replace image with good orientation
686 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
687 $img = $rotated;
688 $imgWidth = $trueImgWidth;
689 $imgHeight = $trueImgHeight;
690 }
691 }
692
693 // Initialize thumbnail dimensions if larger than original
694 if ($maxWidth > $imgWidth) {
695 $maxWidth = $imgWidth;
696 }
697 if ($maxHeight > $imgHeight) {
698 $maxHeight = $imgHeight;
699 }
700
701 $whFact = $maxWidth / $maxHeight; // Width/height factor for maximum label dimensions
702 $imgWhFact = $imgWidth / $imgHeight; // Original width/height factor
703
704 // Set label dimensions
705 if ($whFact < $imgWhFact) {
706 // If determining width
707 $thumbWidth = $maxWidth;
708 $thumbHeight = $thumbWidth / $imgWhFact;
709 } else {
710 // If determining height
711 $thumbHeight = $maxHeight;
712 $thumbWidth = $thumbHeight * $imgWhFact;
713 }
714 $thumbHeight = (int) round($thumbHeight);
715 $thumbWidth = (int) round($thumbWidth);
716
717 // Define target format
718 if (empty($targetformat)) {
719 $targetformat = $infoImg[2];
720 }
721
722 // Create empty image
723 if ($targetformat == IMAGETYPE_GIF) {
724 // Compatibilite image GIF
725 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
726 } else {
727 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
728 }
729
730 // Activate antialiasing for better quality
731 if (function_exists('imageantialias')) {
732 imageantialias($imgThumb, true);
733 }
734
735 // This is to keep transparent alpha channel if exists (PHP >= 4.2)
736 if (function_exists('imagesavealpha')) {
737 imagesavealpha($imgThumb, true);
738 }
739
740 // Variable initialization according to image extension
741 // $targetformat is 0 by default, in such case, we keep original extension
742 $extImgTarget = null;
743 $trans_colour = false;
744 $newquality = null;
745 switch ($targetformat) {
746 case IMAGETYPE_GIF: // 1
747 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // The GIF format works differently
748 imagecolortransparent($imgThumb, $trans_colour);
749 $extImgTarget = '.gif';
750 $newquality = 'NU';
751 break;
752 case IMAGETYPE_JPEG: // 2
753 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
754 $extImgTarget = (preg_match('/\.jpeg$/i', $file) ? '.jpeg' : '.jpg');
755 $newquality = $quality;
756 break;
757 case IMAGETYPE_PNG: // 3
758 imagealphablending($imgThumb, false); // For compatibility on certain systems
759 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
760 $extImgTarget = '.png';
761 $newquality = round(abs($quality - 100) * 9 / 100);
762 break;
763 case IMAGETYPE_BMP: // 6
764 // Not supported by PHP GD
765 $extImgTarget = '.bmp';
766 $newquality = 'NU';
767 break;
768 case IMAGETYPE_WBMP: // 15
769 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
770 $extImgTarget = '.bmp';
771 $newquality = 'NU';
772 break;
773 case IMAGETYPE_WEBP: // 18
774 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
775 $extImgTarget = '.webp';
776 $newquality = $quality;
777 break;
778 }
779 if (function_exists("imagefill") && $trans_colour !== false) {
780 imagefill($imgThumb, 0, 0, $trans_colour);
781 }
782
783 dol_syslog("vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
784 //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insert resized base image
785 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insert resized base image
786
787 $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i', '', $file); // We remove any extension box
788 $fileName = basename($fileName);
789 //$imgThumbName = $dirthumb.'/'.getImageFileNameForSize(basename($file), $extName, $extImgTarget); // Full path of thumb file
790 $imgThumbName = getImageFileNameForSize($file, $extName, $extImgTarget); // Full path of thumb file
791
792
793 // Check if permission are ok
794 //$fp = fopen($imgThumbName, "w");
795 //fclose($fp);
796
797 // Create image on disk
798 switch ($targetformat) {
799 case IMAGETYPE_GIF: // 1
800 imagegif($imgThumb, $imgThumbName);
801 break;
802 case IMAGETYPE_JPEG: // 2
803 imagejpeg($imgThumb, $imgThumbName, $newquality); // @phan-suppress-current-line PhanTypeMismatchArgumentNullableInternal,PhanPossiblyUndeclaredVariable
804 break;
805 case IMAGETYPE_PNG: // 3
806 imagepng($imgThumb, $imgThumbName, $newquality); // @phan-suppress-current-line PhanPossiblyUndeclaredVariable
807 break;
808 case IMAGETYPE_BMP: // 6
809 // Not supported by PHP GD
810 break;
811 case IMAGETYPE_WBMP: // 15
812 imagewbmp($imgThumb, $imgThumbName);
813 break;
814 case IMAGETYPE_WEBP: // 18
815 imagewebp($imgThumb, $imgThumbName, $newquality); // @phan-suppress-current-line PhanTypeMismatchArgumentNullableInternal,PhanPossiblyUndeclaredVariable
816 break;
817 }
818
819 // Set permissions on file
820 dolChmod($imgThumbName);
821
822 // Free memory. This does not delete image.
823 imagedestroy($img);
824 imagedestroy($imgThumb);
825
826 return $imgThumbName;
827}
828
829
838function imgAddEditDeleteButton($htmlid, $urledit, $urldelete)
839{
840 // TODO
841 return '';
842}
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_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
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.