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);
156 $ret[
'width'] = $infoImg[0];
157 $ret[
'height'] = $infoImg[1];
159 $ret[
'width'] = $ret[
'height'] =
'';
181 function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite =
'', $newquality = 0)
183 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
185 global $conf, $langs;
187 dol_syslog(
"dol_imageResizeOrCrop file=".$file.
" mode=".$mode.
" newWidth=".$newWidth.
" newHeight=".$newHeight.
" src_x=".$src_x.
" src_y=".$src_y);
195 return 'Bad parameter file';
196 } elseif (!file_exists($file)) {
198 return $langs->trans(
"ErrorFileNotFound", $file);
200 return 'This filename '.$file.
' does not seem to be an image filename.';
201 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
202 return 'Wrong value for parameter newWidth or newHeight';
203 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
204 return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
205 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
206 return 'Both newHeight or newWidth must be defined for croping';
211 $infoImg = getimagesize($filetoread);
212 $imgWidth = $infoImg[0];
213 $imgHeight = $infoImg[1];
215 $imgTargetName = ($filetowrite ? $filetowrite : $file);
216 $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
219 if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
220 $newWidth = $imgWidth;
221 $newHeight = $imgHeight;
224 if ($newWidth <= 0) {
225 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth);
227 if ($newHeight <= 0) {
228 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight);
234 switch ($infoImg[2]) {
236 $imgfonction =
'imagecreatefromgif';
239 $imgfonction =
'imagecreatefromjpeg';
242 $imgfonction =
'imagecreatefrompng';
245 $imgfonction =
'imagecreatefromwbmp';
248 $imgfonction =
'imagecreatefromwebp';
252 if (!function_exists($imgfonction)) {
254 return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
263 $imgfonction =
'imagecreatefromgif';
267 $imgfonction =
'imagecreatefromjpeg';
270 $imgfonction =
'imagecreatefrompng';
273 $imgfonction =
'imagecreatefromwbmp';
276 $imgfonction =
'imagecreatefromwebp';
280 if (!function_exists($imgfonction)) {
282 return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
288 switch ($infoImg[2]) {
290 $img = imagecreatefromgif($filetoread);
294 $img = imagecreatefromjpeg($filetoread);
298 $img = imagecreatefrompng($filetoread);
302 $img = imagecreatefromwbmp($filetoread);
306 $img = imagecreatefromwebp($filetoread);
312 if ($newExt ==
'gif') {
314 $imgTarget = imagecreate($newWidth, $newHeight);
316 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
320 if (function_exists(
'imageantialias')) {
321 imageantialias($imgTarget,
true);
325 if (function_exists(
'imagesavealpha')) {
326 imagesavealpha($imgTarget,
true);
333 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
334 imagecolortransparent($imgTarget, $trans_colour);
338 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
341 imagealphablending($imgTarget,
false);
342 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
345 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
348 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
351 if (function_exists(
"imagefill") && $trans_colour > 0) {
352 imagefill($imgTarget, 0, 0, $trans_colour);
355 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
357 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
367 imagegif($imgTarget, $imgTargetName);
371 $newquality = ($newquality ? $newquality :
'100');
372 imagejpeg($imgTarget, $imgTargetName, $newquality);
376 imagepng($imgTarget, $imgTargetName, $newquality);
380 imagewbmp($imgTarget, $imgTargetName);
383 $newquality = ($newquality ? $newquality :
'100');
384 imagewebp($imgTarget, $imgTargetName, $newquality);
387 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
391 if (!empty($conf->global->MAIN_UMASK)) {
392 @chmod($imgTargetName, octdec($conf->global->MAIN_UMASK));
397 imagedestroy($imgTarget);
401 return $imgTargetName;
429 if (function_exists(
'exif_read_data')) {
430 $exif = @exif_read_data($fileSource);
431 if ($exif && isset($exif[
'Orientation'])) {
432 $infoImg = getimagesize($fileSource);
434 $orientation = $exif[
'Orientation'];
435 if ($orientation != 1) {
436 $img = imagecreatefromjpeg($fileSource);
438 switch ($orientation) {
450 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
451 imagealphablending($img,
false);
452 imagesavealpha($img,
true);
453 $img = imagerotate($img, $deg, imageColorAllocateAlpha($img, 0, 0, 0, 127));
454 imagealphablending($img,
false);
455 imagesavealpha($img,
true);
457 $img = imagerotate($img, $deg, 0);
461 if ($fileDest ===
false) {
468 switch ($infoImg[2]) {
470 $image = imagegif($img, $fileDest);
473 $image = imagejpeg($img, $fileDest, $quality);
476 $image = imagepng($img, $fileDest, $quality);
482 $image = imagewbmp($img, $fileDest);
511 function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
513 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
515 global $conf, $langs;
517 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
525 return 'ErrorBadParameters';
526 } elseif (!file_exists($file)) {
528 dol_syslog($langs->trans(
"ErrorFileNotFound", $file), LOG_ERR);
529 return $langs->trans(
"ErrorFileNotFound", $file);
531 dol_syslog(
'This file '.$file.
' does not seem to be an image format file name.', LOG_WARNING);
532 return 'ErrorBadImageFormat';
533 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
535 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
536 return 'Error: Wrong value for parameter maxWidth';
537 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
539 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
540 return 'Error: Wrong value for parameter maxHeight';
545 $infoImg = getimagesize($filetoread);
546 $imgWidth = $infoImg[0];
547 $imgHeight = $infoImg[1];
550 if (function_exists(
'exif_read_data')) {
551 $exif = @exif_read_data($filetoread);
552 if ($exif && !empty($exif[
'Orientation'])) {
553 $ort = $exif[
'Orientation'];
557 if ($maxWidth == -1) {
558 $maxWidth = $infoImg[0];
560 if ($maxHeight == -1) {
561 $maxHeight = $infoImg[1];
565 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
567 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
572 switch ($infoImg[2]) {
574 $imgfonction =
'imagecreatefromgif';
577 $imgfonction =
'imagecreatefromjpeg';
580 $imgfonction =
'imagecreatefrompng';
586 $imgfonction =
'imagecreatefromwbmp';
590 if (!function_exists($imgfonction)) {
592 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
597 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
602 switch ($infoImg[2]) {
604 $img = imagecreatefromgif($filetoread);
608 $img = imagecreatefromjpeg($filetoread);
609 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
612 $img = imagecreatefrompng($filetoread);
620 $img = imagecreatefromwbmp($filetoread);
626 if (!is_resource($img) && !($img instanceof \GdImage)) {
627 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
632 if ($ort && !empty($conf->global->MAIN_USE_EXIF_ROTATION)) {
640 $trueImgWidth = $infoImg[1];
641 $trueImgHeight = $infoImg[0];
646 $trueImgWidth = $infoImg[1];
647 $trueImgHeight = $infoImg[0];
655 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
656 imagealphablending($img,
false);
657 imagesavealpha($img,
true);
658 $rotated = imagerotate($img, $exifAngle, imageColorAllocateAlpha($img, 0, 0, 0, 127));
659 imagealphablending($rotated,
false);
660 imagesavealpha($rotated,
true);
662 $rotated = imagerotate($img, $exifAngle, 0);
666 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
668 $imgWidth = $trueImgWidth;
669 $imgHeight = $trueImgHeight;
674 if ($maxWidth > $imgWidth) {
675 $maxWidth = $imgWidth;
677 if ($maxHeight > $imgHeight) {
678 $maxHeight = $imgHeight;
681 $whFact = $maxWidth / $maxHeight;
682 $imgWhFact = $imgWidth / $imgHeight;
685 if ($whFact < $imgWhFact) {
687 $thumbWidth = $maxWidth;
688 $thumbHeight = $thumbWidth / $imgWhFact;
691 $thumbHeight = $maxHeight;
692 $thumbWidth = $thumbHeight * $imgWhFact;
694 $thumbHeight = round($thumbHeight);
695 $thumbWidth = round($thumbWidth);
698 if (empty($targetformat)) {
699 $targetformat = $infoImg[2];
703 if ($targetformat == IMAGETYPE_GIF) {
705 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
707 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
711 if (function_exists(
'imageantialias')) {
712 imageantialias($imgThumb,
true);
716 if (function_exists(
'imagesavealpha')) {
717 imagesavealpha($imgThumb,
true);
722 switch ($targetformat) {
724 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
725 imagecolortransparent($imgThumb, $trans_colour);
726 $extImgTarget =
'.gif';
730 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
731 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
732 $newquality = $quality;
735 imagealphablending($imgThumb,
false);
736 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
737 $extImgTarget =
'.png';
738 $newquality = $quality - 100;
739 $newquality = round(abs($quality - 100) * 9 / 100);
743 $extImgTarget =
'.bmp';
747 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
748 $extImgTarget =
'.bmp';
752 if (function_exists(
"imagefill")) {
753 imagefill($imgThumb, 0, 0, $trans_colour);
756 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
758 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
760 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
761 $fileName = basename($fileName);
771 switch ($targetformat) {
773 imagegif($imgThumb, $imgThumbName);
776 imagejpeg($imgThumb, $imgThumbName, $newquality);
779 imagepng($imgThumb, $imgThumbName, $newquality);
785 imagewbmp($imgThumb, $imgThumbName);
790 if (!empty($conf->global->MAIN_UMASK)) {
791 @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
796 imagedestroy($imgThumb);
798 return $imgThumbName;