dolibarr 19.0.4
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
33if (!defined('IMAGETYPE_WEBP')) {
34 define('IMAGETYPE_WEBP', 18);
35}
36
37
44{
45 $maxwidthsmall = 480;
46 $maxheightsmall = 270; // Near 16/9eme
47 $maxwidthmini = 128;
48 $maxheightmini = 72; // 16/9eme
49 $quality = 80;
50
51 return array(
52 'maxwidthsmall' => $maxwidthsmall,
53 'maxheightsmall' => $maxheightsmall,
54 'maxwidthmini' => $maxwidthmini,
55 'maxheightmini' => $maxheightmini,
56 'quality' => $quality
57 );
58}
59
66function getListOfPossibleImageExt($acceptsvg = 0)
67{
68 global $conf;
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 // Fonctions 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 parametre 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 // Fonctions 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 // Fonctions 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); // On procede autrement pour le format GIF
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); // Pour compatibilite sur certain systeme
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 $conf, $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 dol_syslog('Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
638 return 0;
639 }
640
641 $exifAngle = false;
642 if ($ort && getDolGlobalString('MAIN_USE_EXIF_ROTATION')) {
643 switch ($ort) {
644 case 3: // 180 rotate left
645 $exifAngle = 180;
646 break;
647 case 6: // 90 rotate right
648 $exifAngle = -90;
649 // changing sizes
650 $trueImgWidth = $infoImg[1];
651 $trueImgHeight = $infoImg[0];
652 break;
653 case 8: // 90 rotate left
654 $exifAngle = 90;
655 // changing sizes
656 $trueImgWidth = $infoImg[1]; // Largeur de l'image
657 $trueImgHeight = $infoImg[0]; // Hauteur de l'image
658 break;
659 }
660 }
661
662 if ($exifAngle) {
663 $rotated = false;
664
665 if ($infoImg[2] === 'IMAGETYPE_PNG') { // In fact there is no exif on PNG but just in case
666 imagealphablending($img, false);
667 imagesavealpha($img, true);
668 $rotated = imagerotate($img, $exifAngle, imageColorAllocateAlpha($img, 0, 0, 0, 127));
669 imagealphablending($rotated, false);
670 imagesavealpha($rotated, true);
671 } else {
672 $rotated = imagerotate($img, $exifAngle, 0);
673 }
674
675 // replace image with good orientation
676 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
677 $img = $rotated;
678 $imgWidth = $trueImgWidth;
679 $imgHeight = $trueImgHeight;
680 }
681 }
682
683 // Initialize thumbnail dimensions if larger than original
684 if ($maxWidth > $imgWidth) {
685 $maxWidth = $imgWidth;
686 }
687 if ($maxHeight > $imgHeight) {
688 $maxHeight = $imgHeight;
689 }
690
691 $whFact = $maxWidth / $maxHeight; // Width/height factor for maximum label dimensions
692 $imgWhFact = $imgWidth / $imgHeight; // Original width/height factor
693
694 // Set label dimensions
695 if ($whFact < $imgWhFact) {
696 // If determining width
697 $thumbWidth = $maxWidth;
698 $thumbHeight = $thumbWidth / $imgWhFact;
699 } else {
700 // If determining height
701 $thumbHeight = $maxHeight;
702 $thumbWidth = $thumbHeight * $imgWhFact;
703 }
704 $thumbHeight = round($thumbHeight);
705 $thumbWidth = round($thumbWidth);
706
707 // Define target format
708 if (empty($targetformat)) {
709 $targetformat = $infoImg[2];
710 }
711
712 // Create empty image
713 if ($targetformat == IMAGETYPE_GIF) {
714 // Compatibilite image GIF
715 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
716 } else {
717 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
718 }
719
720 // Activate antialiasing for better quality
721 if (function_exists('imageantialias')) {
722 imageantialias($imgThumb, true);
723 }
724
725 // This is to keep transparent alpha channel if exists (PHP >= 4.2)
726 if (function_exists('imagesavealpha')) {
727 imagesavealpha($imgThumb, true);
728 }
729
730 // Variable initialization according to image extension
731 // $targetformat is 0 by default, in such case, we keep original extension
732 switch ($targetformat) {
733 case IMAGETYPE_GIF: // 1
734 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // The GIF format works differently
735 imagecolortransparent($imgThumb, $trans_colour);
736 $extImgTarget = '.gif';
737 $newquality = 'NU';
738 break;
739 case IMAGETYPE_JPEG: // 2
740 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
741 $extImgTarget = (preg_match('/\.jpeg$/i', $file) ? '.jpeg' : '.jpg');
742 $newquality = $quality;
743 break;
744 case IMAGETYPE_PNG: // 3
745 imagealphablending($imgThumb, false); // For compatibility on certain systems
746 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
747 $extImgTarget = '.png';
748 $newquality = $quality - 100;
749 $newquality = round(abs($quality - 100) * 9 / 100);
750 break;
751 case IMAGETYPE_BMP: // 6
752 // Not supported by PHP GD
753 $extImgTarget = '.bmp';
754 $newquality = 'NU';
755 break;
756 case IMAGETYPE_WBMP: // 15
757 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
758 $extImgTarget = '.bmp';
759 $newquality = 'NU';
760 break;
761 case IMAGETYPE_WEBP: // 18
762 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
763 $extImgTarget = '.webp';
764 $newquality = $quality;
765 break;
766 }
767 if (function_exists("imagefill")) {
768 imagefill($imgThumb, 0, 0, $trans_colour);
769 }
770
771 dol_syslog("vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
772 //imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insert resized base image
773 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insert resized base image
774
775 $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i', '', $file); // We remove any extension box
776 $fileName = basename($fileName);
777 //$imgThumbName = $dirthumb.'/'.getImageFileNameForSize(basename($file), $extName, $extImgTarget); // Full path of thumb file
778 $imgThumbName = getImageFileNameForSize($file, $extName, $extImgTarget); // Full path of thumb file
779
780
781 // Check if permission are ok
782 //$fp = fopen($imgThumbName, "w");
783 //fclose($fp);
784
785 // Create image on disk
786 switch ($targetformat) {
787 case IMAGETYPE_GIF: // 1
788 imagegif($imgThumb, $imgThumbName);
789 break;
790 case IMAGETYPE_JPEG: // 2
791 imagejpeg($imgThumb, $imgThumbName, $newquality);
792 break;
793 case IMAGETYPE_PNG: // 3
794 imagepng($imgThumb, $imgThumbName, $newquality);
795 break;
796 case IMAGETYPE_BMP: // 6
797 // Not supported by PHP GD
798 break;
799 case IMAGETYPE_WBMP: // 15
800 imagewbmp($imgThumb, $imgThumbName);
801 break;
802 case IMAGETYPE_WEBP: // 18
803 imagewebp($imgThumb, $imgThumbName, $newquality);
804 break;
805 }
806
807 // Set permissions on file
808 dolChmod($imgThumbName);
809
810 // Free memory. This does not delete image.
811 imagedestroy($img);
812 imagedestroy($imgThumb);
813
814 return $imgThumbName;
815}
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)
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.