dolibarr 21.0.0-alpha
images.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2007 Regis Houssin <regis.houssin@inodbox.com>
4 * 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 $conf, $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 switch ($infoImg[2]) {
294 case 1: // Gif
295 $img = imagecreatefromgif($filetoread);
296 $extImg = '.gif'; // File name extension of image
297 break;
298 case 2: // Jpg
299 $img = imagecreatefromjpeg($filetoread);
300 $extImg = '.jpg';
301 break;
302 case 3: // Png
303 $img = imagecreatefrompng($filetoread);
304 $extImg = '.png';
305 break;
306 case 4: // Bmp
307 $img = imagecreatefromwbmp($filetoread);
308 $extImg = '.bmp';
309 break;
310 case 18: // Webp
311 $img = imagecreatefromwebp($filetoread);
312 $extImg = '.webp';
313 break;
314 }
315
316 // Create empty image for target
317 if ($newExt == 'gif') {
318 // Compatibility image GIF
319 $imgTarget = imagecreate($newWidth, $newHeight);
320 } else {
321 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
322 }
323
324 // Activate antialiasing for better quality
325 if (function_exists('imageantialias')) {
326 imageantialias($imgTarget, true);
327 }
328
329 // This is to keep transparent alpha channel if exists (PHP >= 4.2)
330 if (function_exists('imagesavealpha')) {
331 imagesavealpha($imgTarget, true);
332 }
333
334 // Set transparent color according to image extension
335 $trans_colour = -1; // By default, undefined
336 switch ($newExt) {
337 case 'gif': // Gif
338 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255); // The method is different for the GIF format
339 imagecolortransparent($imgTarget, $trans_colour);
340 break;
341 case 'jpg': // Jpg
342 case 'jpeg': // Jpeg
343 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
344 break;
345 case 'png': // Png
346 imagealphablending($imgTarget, false); // For compatibility with certain systems
347 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127); // Keep transparent channel
348 break;
349 case 'bmp': // Bmp
350 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
351 break;
352 case 'webp': // Webp
353 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
354 break;
355 }
356 if (function_exists("imagefill") && $trans_colour > 0) {
357 imagefill($imgTarget, 0, 0, $trans_colour);
358 }
359
360 dol_syslog("dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
361 //imagecopyresized($imgTarget, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
362 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
363
364 // Check if permission are ok
365 //$fp = fopen($imgTargetName, "w");
366 //fclose($fp);
367
368 // Create image on disk (overwrite file if exists)
369 switch ($newExt) {
370 case 'gif': // Gif
371 $newquality = 'NU'; // Quality is not used for this format
372 imagegif($imgTarget, $imgTargetName);
373 break;
374 case 'jpg': // Jpg
375 case 'jpeg': // Jpeg
376 $newquality = ($newquality ? $newquality : '100'); // % quality maximum
377 imagejpeg($imgTarget, $imgTargetName, $newquality);
378 break;
379 case 'png': // Png
380 $newquality = 0; // No compression (0-9)
381 imagepng($imgTarget, $imgTargetName, $newquality);
382 break;
383 case 'bmp': // Bmp
384 $newquality = 'NU'; // Quality is not used for this format
385 imagewbmp($imgTarget, $imgTargetName);
386 break;
387 case 'webp': // Webp
388 $newquality = ($newquality ? $newquality : '100'); // % quality maximum
389 imagewebp($imgTarget, $imgTargetName, $newquality);
390 break;
391 default:
392 dol_syslog("images.lib.php::imageResizeOrCrop() Format ".$newExt." is not supported", LOG_WARNING);
393 }
394
395 // Set permissions on file
396 dolChmod($imgTargetName);
397
398 // Free memory. This does not delete image.
399 imagedestroy($img);
400 imagedestroy($imgTarget);
401
402 clearstatcache(); // File was replaced by a modified one, so we clear file caches.
403
404 return $imgTargetName;
405}
406
407
416function dolRotateImage($file_path)
417{
418 return correctExifImageOrientation($file_path, $file_path);
419}
420
421
430function correctExifImageOrientation($fileSource, $fileDest, $quality = 95)
431{
432 if (function_exists('exif_read_data')) {
433 $exif = @exif_read_data($fileSource);
434 if ($exif && isset($exif['Orientation'])) {
435 $infoImg = getimagesize($fileSource); // Get image infos
436
437 $orientation = $exif['Orientation'];
438 if ($orientation != 1) {
439 $img = imagecreatefromjpeg($fileSource);
440 $deg = 0;
441 switch ($orientation) {
442 case 3:
443 $deg = 180;
444 break;
445 case 6:
446 $deg = 270;
447 break;
448 case 8:
449 $deg = 90;
450 break;
451 }
452 if ($deg) {
453 if ($infoImg[2] === IMAGETYPE_PNG) { // In fact there is no exif on PNG but just in case
454 imagealphablending($img, false);
455 imagesavealpha($img, true);
456 $img = imagerotate($img, $deg, imagecolorallocatealpha($img, 0, 0, 0, 127));
457 imagealphablending($img, false);
458 imagesavealpha($img, true);
459 } else {
460 $img = imagerotate($img, $deg, 0);
461 }
462 }
463 // then rewrite the rotated image back to the disk as $fileDest
464 if ($fileDest === false) {
465 return $img;
466 } else {
467 // In fact there exif is only for JPG but just in case
468 // Create image on disk
469 $image = false;
470
471 switch ($infoImg[2]) {
472 case IMAGETYPE_GIF: // 1
473 $image = imagegif($img, $fileDest);
474 break;
475 case IMAGETYPE_JPEG: // 2
476 $image = imagejpeg($img, $fileDest, $quality);
477 break;
478 case IMAGETYPE_PNG: // 3
479 $image = imagepng($img, $fileDest, $quality);
480 break;
481 case IMAGETYPE_BMP: // 6
482 // Not supported by PHP GD
483 break;
484 case IMAGETYPE_WBMP: // 15
485 $image = imagewbmp($img, $fileDest);
486 break;
487 }
488
489 // Free up memory (imagedestroy does not delete files):
490 @imagedestroy($img);
491
492 return $image;
493 }
494 } // if there is some rotation necessary
495 } // if have the exif orientation info
496 } // if function exists
497
498 return false;
499}
500
514function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName = '_small', $quality = 50, $outdir = 'thumbs', $targetformat = 0)
515{
516 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
517
518 global $langs;
519
520 dol_syslog("vignette file=".$file." extName=".$extName." maxWidth=".$maxWidth." maxHeight=".$maxHeight." quality=".$quality." outdir=".$outdir." targetformat=".$targetformat);
521
522 // Clean parameters
523 $file = trim($file);
524
525 // Check parameters
526 if (!$file) {
527 // If the file has not been indicated
528 return 'ErrorBadParameters';
529 } elseif (!file_exists($file)) {
530 // If the file passed in parameter does not exist
531 dol_syslog($langs->trans("ErrorFileNotFound", $file), LOG_ERR);
532 return $langs->trans("ErrorFileNotFound", $file);
533 } elseif (image_format_supported($file) < 0) {
534 dol_syslog('This file '.$file.' does not seem to be an image format file name.', LOG_WARNING);
535 return 'ErrorBadImageFormat';
536 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
537 // If max width is incorrect (not numeric, empty, or less than 0)
538 dol_syslog('Wrong value for parameter maxWidth', LOG_ERR);
539 return 'Error: Wrong value for parameter maxWidth';
540 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
541 // If max height is incorrect (not numeric, empty, or less than 0)
542 dol_syslog('Wrong value for parameter maxHeight', LOG_ERR);
543 return 'Error: Wrong value for parameter maxHeight';
544 }
545
546 $filetoread = realpath(dol_osencode($file)); // Chemin canonique absolu de l'image
547
548 $infoImg = getimagesize($filetoread); // Recuperation des infos de l'image
549 $imgWidth = $infoImg[0]; // Largeur de l'image
550 $imgHeight = $infoImg[1]; // Hauteur de l'image
551
552 $ort = false;
553 if (function_exists('exif_read_data')) {
554 $exif = @exif_read_data($filetoread);
555 if ($exif && !empty($exif['Orientation'])) {
556 $ort = $exif['Orientation'];
557 }
558 }
559
560 if ($maxWidth == -1) {
561 $maxWidth = $infoImg[0]; // If size is -1, we keep unchanged
562 }
563 if ($maxHeight == -1) {
564 $maxHeight = $infoImg[1]; // If size is -1, we keep unchanged
565 }
566
567 // If the image is smaller than the maximum width and height, no thumbnail is created.
568 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
569 // On cree toujours les vignettes
570 dol_syslog("File size is smaller than thumb size", LOG_DEBUG);
571 //return 'Le fichier '.$file.' ne necessite pas de creation de vignette';
572 }
573
574 $imgfonction = '';
575 switch ($infoImg[2]) {
576 case IMAGETYPE_GIF: // 1
577 $imgfonction = 'imagecreatefromgif';
578 break;
579 case IMAGETYPE_JPEG: // 2
580 $imgfonction = 'imagecreatefromjpeg';
581 break;
582 case IMAGETYPE_PNG: // 3
583 $imgfonction = 'imagecreatefrompng';
584 break;
585 case IMAGETYPE_BMP: // 6
586 // Not supported by PHP GD
587 break;
588 case IMAGETYPE_WBMP: // 15
589 $imgfonction = 'imagecreatefromwbmp';
590 break;
591 case IMAGETYPE_WEBP: // 18
592 $imgfonction = 'imagecreatefromwebp';
593 break;
594 }
595 if ($imgfonction) {
596 if (!function_exists($imgfonction)) {
597 // Conversion functions not present in this PHP
598 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
599 }
600 }
601
602 // We create the directory containing the thumbnails
603 $dirthumb = dirname($file).($outdir ? '/'.$outdir : ''); // Path to thumbnail folder
604 dol_mkdir($dirthumb);
605
606 // Variable initialization according to image extension
607 $img = null;
608 switch ($infoImg[2]) {
609 case IMAGETYPE_GIF: // 1
610 $img = imagecreatefromgif($filetoread);
611 $extImg = '.gif';
612 break;
613 case IMAGETYPE_JPEG: // 2
614 $img = imagecreatefromjpeg($filetoread);
615 $extImg = (preg_match('/\.jpeg$/', $file) ? '.jpeg' : '.jpg');
616 break;
617 case IMAGETYPE_PNG: // 3
618 $img = imagecreatefrompng($filetoread);
619 $extImg = '.png';
620 break;
621 case IMAGETYPE_BMP: // 6
622 // Not supported by PHP GD
623 $extImg = '.bmp';
624 break;
625 case IMAGETYPE_WBMP: // 15
626 $img = imagecreatefromwbmp($filetoread);
627 $extImg = '.bmp';
628 break;
629 case IMAGETYPE_WEBP: // 18
630 $img = imagecreatefromwebp($filetoread);
631 $extImg = '.webp';
632 break;
633 }
634
635 // Before PHP8, img was a resource, With PHP8, it is a GdImage
636 // if (!is_resource($img) && class_exists('GdImage') && !($img instanceof GdImage)) {
637 if (is_null($img) || $img === false) {
638 dol_syslog('Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
639 return 0;
640 }
641
642 $exifAngle = false;
643 if ($ort && getDolGlobalString('MAIN_USE_EXIF_ROTATION')) {
644 switch ($ort) {
645 case 3: // 180 rotate left
646 $exifAngle = 180;
647 break;
648 case 6: // 90 rotate right
649 $exifAngle = -90;
650 // changing sizes
651 $trueImgWidth = $infoImg[1];
652 $trueImgHeight = $infoImg[0];
653 break;
654 case 8: // 90 rotate left
655 $exifAngle = 90;
656 // changing sizes
657 $trueImgWidth = $infoImg[1]; // Largeur de l'image
658 $trueImgHeight = $infoImg[0]; // Hauteur de l'image
659 break;
660 }
661 }
662
663 if ($exifAngle) {
664 $rotated = false;
665
666 if ($infoImg[2] === IMAGETYPE_PNG) { // In fact there is no exif on PNG but just in case
667 imagealphablending($img, false);
668 imagesavealpha($img, true);
669 $rotated = imagerotate($img, $exifAngle, imagecolorallocatealpha($img, 0, 0, 0, 127));
670 imagealphablending($rotated, false);
671 imagesavealpha($rotated, true);
672 } else {
673 $rotated = imagerotate($img, $exifAngle, 0);
674 }
675
676 // replace image with good orientation
677 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
678 $img = $rotated;
679 $imgWidth = $trueImgWidth;
680 $imgHeight = $trueImgHeight;
681 }
682 }
683
684 // Initialize thumbnail dimensions if larger than original
685 if ($maxWidth > $imgWidth) {
686 $maxWidth = $imgWidth;
687 }
688 if ($maxHeight > $imgHeight) {
689 $maxHeight = $imgHeight;
690 }
691
692 $whFact = $maxWidth / $maxHeight; // Width/height factor for maximum label dimensions
693 $imgWhFact = $imgWidth / $imgHeight; // Original width/height factor
694
695 // Set label dimensions
696 if ($whFact < $imgWhFact) {
697 // If determining width
698 $thumbWidth = $maxWidth;
699 $thumbHeight = $thumbWidth / $imgWhFact;
700 } else {
701 // If determining height
702 $thumbHeight = $maxHeight;
703 $thumbWidth = $thumbHeight * $imgWhFact;
704 }
705 $thumbHeight = (int) round($thumbHeight);
706 $thumbWidth = (int) round($thumbWidth);
707
708 // Define target format
709 if (empty($targetformat)) {
710 $targetformat = $infoImg[2];
711 }
712
713 // Create empty image
714 if ($targetformat == IMAGETYPE_GIF) {
715 // Compatibilite image GIF
716 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
717 } else {
718 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
719 }
720
721 // Activate antialiasing for better quality
722 if (function_exists('imageantialias')) {
723 imageantialias($imgThumb, true);
724 }
725
726 // This is to keep transparent alpha channel if exists (PHP >= 4.2)
727 if (function_exists('imagesavealpha')) {
728 imagesavealpha($imgThumb, true);
729 }
730
731 // Variable initialization according to image extension
732 // $targetformat is 0 by default, in such case, we keep original extension
733 switch ($targetformat) {
734 case IMAGETYPE_GIF: // 1
735 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // The GIF format works differently
736 imagecolortransparent($imgThumb, $trans_colour);
737 $extImgTarget = '.gif';
738 $newquality = 'NU';
739 break;
740 case IMAGETYPE_JPEG: // 2
741 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
742 $extImgTarget = (preg_match('/\.jpeg$/i', $file) ? '.jpeg' : '.jpg');
743 $newquality = $quality;
744 break;
745 case IMAGETYPE_PNG: // 3
746 imagealphablending($imgThumb, false); // For compatibility on certain systems
747 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
748 $extImgTarget = '.png';
749 $newquality = $quality - 100;
750 $newquality = round(abs($quality - 100) * 9 / 100);
751 break;
752 case IMAGETYPE_BMP: // 6
753 // Not supported by PHP GD
754 $extImgTarget = '.bmp';
755 $newquality = 'NU';
756 break;
757 case IMAGETYPE_WBMP: // 15
758 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
759 $extImgTarget = '.bmp';
760 $newquality = 'NU';
761 break;
762 case IMAGETYPE_WEBP: // 18
763 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
764 $extImgTarget = '.webp';
765 $newquality = $quality;
766 break;
767 }
768 if (function_exists("imagefill")) {
769 imagefill($imgThumb, 0, 0, $trans_colour);
770 }
771
772 dol_syslog("vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
773 //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insert resized base image
774 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insert resized base image
775
776 $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i', '', $file); // We remove any extension box
777 $fileName = basename($fileName);
778 //$imgThumbName = $dirthumb.'/'.getImageFileNameForSize(basename($file), $extName, $extImgTarget); // Full path of thumb file
779 $imgThumbName = getImageFileNameForSize($file, $extName, $extImgTarget); // Full path of thumb file
780
781
782 // Check if permission are ok
783 //$fp = fopen($imgThumbName, "w");
784 //fclose($fp);
785
786 // Create image on disk
787 switch ($targetformat) {
788 case IMAGETYPE_GIF: // 1
789 imagegif($imgThumb, $imgThumbName);
790 break;
791 case IMAGETYPE_JPEG: // 2
792 imagejpeg($imgThumb, $imgThumbName, $newquality);
793 break;
794 case IMAGETYPE_PNG: // 3
795 imagepng($imgThumb, $imgThumbName, $newquality);
796 break;
797 case IMAGETYPE_BMP: // 6
798 // Not supported by PHP GD
799 break;
800 case IMAGETYPE_WBMP: // 15
801 imagewbmp($imgThumb, $imgThumbName);
802 break;
803 case IMAGETYPE_WEBP: // 18
804 imagewebp($imgThumb, $imgThumbName, $newquality);
805 break;
806 }
807
808 // Set permissions on file
809 dolChmod($imgThumbName);
810
811 // Free memory. This does not delete image.
812 imagedestroy($img);
813 imagedestroy($imgThumb);
814
815 return $imgThumbName;
816}
817
818
827function imgAddEditDeleteButton($htmlid, $urledit, $urldelete)
828{
829 // TODO
830 return '';
831}
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 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.