28 $maxheightsmall = 270;
41 $maxheightsmall = 270;
47 'maxwidthsmall' => $maxwidthsmall,
48 'maxheightsmall' => $maxheightsmall,
49 'maxwidthmini' => $maxwidthmini,
50 'maxheightmini' => $maxheightmini,
65 $regeximgext =
'\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm';
66 if ($acceptsvg || !empty($conf->global->MAIN_ALLOW_SVG_FILES_AS_IMAGES)) {
67 $regeximgext .=
'|\.svg';
86 if (!preg_match(
'/('.$regeximgext.
')$/i', $file, $reg)) {
92 if (strtolower($reg[1]) ==
'.gif') {
93 $imgfonction =
'imagecreatefromgif';
95 if (strtolower($reg[1]) ==
'.jpg') {
96 $imgfonction =
'imagecreatefromjpeg';
98 if (strtolower($reg[1]) ==
'.jpeg') {
99 $imgfonction =
'imagecreatefromjpeg';
101 if (strtolower($reg[1]) ==
'.png') {
102 $imgfonction =
'imagecreatefrompng';
104 if (strtolower($reg[1]) ==
'.bmp') {
105 $imgfonction =
'imagecreatefromwbmp';
107 if (strtolower($reg[1]) ==
'.webp') {
108 $imgfonction =
'imagecreatefromwebp';
110 if (strtolower($reg[1]) ==
'.xpm') {
111 $imgfonction =
'imagecreatefromxpm';
113 if (strtolower($reg[1]) ==
'.xbm') {
114 $imgfonction =
'imagecreatefromxbm';
116 if (strtolower($reg[1]) ==
'.svg') {
117 $imgfonction =
'imagecreatefromsvg';
120 if (!function_exists($imgfonction)) {
154 $infoImg = getimagesize($filetoread);
155 $ret[
'width'] = $infoImg[0];
156 $ret[
'height'] = $infoImg[1];
177 function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite =
'', $newquality = 0)
179 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
181 global $conf, $langs;
183 dol_syslog(
"dol_imageResizeOrCrop file=".$file.
" mode=".$mode.
" newWidth=".$newWidth.
" newHeight=".$newHeight.
" src_x=".$src_x.
" src_y=".$src_y);
191 return 'Bad parameter file';
192 } elseif (!file_exists($file)) {
194 return $langs->trans(
"ErrorFileNotFound", $file);
196 return 'This filename '.$file.
' does not seem to be an image filename.';
197 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
198 return 'Wrong value for parameter newWidth or newHeight';
199 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
200 return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
201 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
202 return 'Both newHeight or newWidth must be defined for croping';
207 $infoImg = getimagesize($filetoread);
208 $imgWidth = $infoImg[0];
209 $imgHeight = $infoImg[1];
211 $imgTargetName = ($filetowrite ? $filetowrite : $file);
212 $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
215 if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
216 $newWidth = $imgWidth;
217 $newHeight = $imgHeight;
220 if ($newWidth <= 0) {
221 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth);
223 if ($newHeight <= 0) {
224 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight);
230 switch ($infoImg[2]) {
232 $imgfonction =
'imagecreatefromgif';
235 $imgfonction =
'imagecreatefromjpeg';
238 $imgfonction =
'imagecreatefrompng';
241 $imgfonction =
'imagecreatefromwbmp';
244 $imgfonction =
'imagecreatefromwebp';
248 if (!function_exists($imgfonction)) {
250 return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
259 $imgfonction =
'imagecreatefromgif';
263 $imgfonction =
'imagecreatefromjpeg';
266 $imgfonction =
'imagecreatefrompng';
269 $imgfonction =
'imagecreatefromwbmp';
272 $imgfonction =
'imagecreatefromwebp';
276 if (!function_exists($imgfonction)) {
278 return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
284 switch ($infoImg[2]) {
286 $img = imagecreatefromgif($filetoread);
290 $img = imagecreatefromjpeg($filetoread);
294 $img = imagecreatefrompng($filetoread);
298 $img = imagecreatefromwbmp($filetoread);
302 $img = imagecreatefromwebp($filetoread);
308 if ($newExt ==
'gif') {
310 $imgTarget = imagecreate($newWidth, $newHeight);
312 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
316 if (function_exists(
'imageantialias')) {
317 imageantialias($imgTarget,
true);
321 if (function_exists(
'imagesavealpha')) {
322 imagesavealpha($imgTarget,
true);
329 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
330 imagecolortransparent($imgTarget, $trans_colour);
334 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
337 imagealphablending($imgTarget,
false);
338 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
341 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
344 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
347 if (function_exists(
"imagefill") && $trans_colour > 0) {
348 imagefill($imgTarget, 0, 0, $trans_colour);
351 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
353 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
363 imagegif($imgTarget, $imgTargetName);
367 $newquality = ($newquality ? $newquality :
'100');
368 imagejpeg($imgTarget, $imgTargetName, $newquality);
372 imagepng($imgTarget, $imgTargetName, $newquality);
376 imagewbmp($imgTarget, $imgTargetName);
379 $newquality = ($newquality ? $newquality :
'100');
380 imagewebp($imgTarget, $imgTargetName, $newquality);
383 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
387 if (!empty($conf->global->MAIN_UMASK)) {
388 @chmod($imgTargetName, octdec($conf->global->MAIN_UMASK));
393 imagedestroy($imgTarget);
397 return $imgTargetName;
425 if (function_exists(
'exif_read_data')) {
426 $exif = @exif_read_data($fileSource);
427 if ($exif && isset($exif[
'Orientation'])) {
428 $infoImg = getimagesize($fileSource);
430 $orientation = $exif[
'Orientation'];
431 if ($orientation != 1) {
432 $img = imagecreatefromjpeg($fileSource);
434 switch ($orientation) {
446 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
447 imagealphablending($img,
false);
448 imagesavealpha($img,
true);
449 $img = imagerotate($img, $deg, imageColorAllocateAlpha($img, 0, 0, 0, 127));
450 imagealphablending($img,
false);
451 imagesavealpha($img,
true);
453 $img = imagerotate($img, $deg, 0);
457 if ($fileDest ===
false) {
464 switch ($infoImg[2]) {
466 $image = imagegif($img, $fileDest);
469 $image = imagejpeg($img, $fileDest, $quality);
472 $image = imagepng($img, $fileDest, $quality);
478 $image = imagewbmp($img, $fileDest);
507 function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
509 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
511 global $conf, $langs;
513 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
521 return 'ErrorBadParameters';
522 } elseif (!file_exists($file)) {
524 dol_syslog($langs->trans(
"ErrorFileNotFound", $file), LOG_ERR);
525 return $langs->trans(
"ErrorFileNotFound", $file);
527 dol_syslog(
'This file '.$file.
' does not seem to be an image format file name.', LOG_WARNING);
528 return 'ErrorBadImageFormat';
529 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
531 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
532 return 'Error: Wrong value for parameter maxWidth';
533 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
535 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
536 return 'Error: Wrong value for parameter maxHeight';
541 $infoImg = getimagesize($filetoread);
542 $imgWidth = $infoImg[0];
543 $imgHeight = $infoImg[1];
546 if (function_exists(
'exif_read_data')) {
547 $exif = @exif_read_data($filetoread);
548 if ($exif && !empty($exif[
'Orientation'])) {
549 $ort = $exif[
'Orientation'];
553 if ($maxWidth == -1) {
554 $maxWidth = $infoImg[0];
556 if ($maxHeight == -1) {
557 $maxHeight = $infoImg[1];
561 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
563 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
568 switch ($infoImg[2]) {
570 $imgfonction =
'imagecreatefromgif';
573 $imgfonction =
'imagecreatefromjpeg';
576 $imgfonction =
'imagecreatefrompng';
582 $imgfonction =
'imagecreatefromwbmp';
586 if (!function_exists($imgfonction)) {
588 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
593 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
598 switch ($infoImg[2]) {
600 $img = imagecreatefromgif($filetoread);
604 $img = imagecreatefromjpeg($filetoread);
605 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
608 $img = imagecreatefrompng($filetoread);
616 $img = imagecreatefromwbmp($filetoread);
622 if (!is_resource($img) && !($img instanceof \GdImage)) {
623 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
628 if ($ort && !empty($conf->global->MAIN_USE_EXIF_ROTATION)) {
636 $trueImgWidth = $infoImg[1];
637 $trueImgHeight = $infoImg[0];
642 $trueImgWidth = $infoImg[1];
643 $trueImgHeight = $infoImg[0];
651 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
652 imagealphablending($img,
false);
653 imagesavealpha($img,
true);
654 $rotated = imagerotate($img, $exifAngle, imageColorAllocateAlpha($img, 0, 0, 0, 127));
655 imagealphablending($rotated,
false);
656 imagesavealpha($rotated,
true);
658 $rotated = imagerotate($img, $exifAngle, 0);
662 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
664 $imgWidth = $trueImgWidth;
665 $imgHeight = $trueImgHeight;
670 if ($maxWidth > $imgWidth) {
671 $maxWidth = $imgWidth;
673 if ($maxHeight > $imgHeight) {
674 $maxHeight = $imgHeight;
677 $whFact = $maxWidth / $maxHeight;
678 $imgWhFact = $imgWidth / $imgHeight;
681 if ($whFact < $imgWhFact) {
683 $thumbWidth = $maxWidth;
684 $thumbHeight = $thumbWidth / $imgWhFact;
687 $thumbHeight = $maxHeight;
688 $thumbWidth = $thumbHeight * $imgWhFact;
690 $thumbHeight = round($thumbHeight);
691 $thumbWidth = round($thumbWidth);
694 if (empty($targetformat)) {
695 $targetformat = $infoImg[2];
699 if ($targetformat == IMAGETYPE_GIF) {
701 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
703 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
707 if (function_exists(
'imageantialias')) {
708 imageantialias($imgThumb,
true);
712 if (function_exists(
'imagesavealpha')) {
713 imagesavealpha($imgThumb,
true);
718 switch ($targetformat) {
720 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
721 imagecolortransparent($imgThumb, $trans_colour);
722 $extImgTarget =
'.gif';
726 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
727 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
728 $newquality = $quality;
731 imagealphablending($imgThumb,
false);
732 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
733 $extImgTarget =
'.png';
734 $newquality = $quality - 100;
735 $newquality = round(abs($quality - 100) * 9 / 100);
739 $extImgTarget =
'.bmp';
743 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
744 $extImgTarget =
'.bmp';
748 if (function_exists(
"imagefill")) {
749 imagefill($imgThumb, 0, 0, $trans_colour);
752 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
754 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
756 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
757 $fileName = basename($fileName);
767 switch ($targetformat) {
769 imagegif($imgThumb, $imgThumbName);
772 imagejpeg($imgThumb, $imgThumbName, $newquality);
775 imagepng($imgThumb, $imgThumbName, $newquality);
781 imagewbmp($imgThumb, $imgThumbName);
786 if (!empty($conf->global->MAIN_UMASK)) {
787 @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
792 imagedestroy($imgThumb);
794 return $imgThumbName;