27 $maxheightsmall = 270;
43 $regeximgext =
'\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm';
44 if ($acceptsvg || !empty($conf->global->MAIN_ALLOW_SVG_FILES_AS_IMAGES)) {
45 $regeximgext .=
'|\.svg';
64 if (!preg_match(
'/('.$regeximgext.
')$/i', $file, $reg)) {
70 if (strtolower($reg[1]) ==
'.gif') {
71 $imgfonction =
'imagecreatefromgif';
73 if (strtolower($reg[1]) ==
'.jpg') {
74 $imgfonction =
'imagecreatefromjpeg';
76 if (strtolower($reg[1]) ==
'.jpeg') {
77 $imgfonction =
'imagecreatefromjpeg';
79 if (strtolower($reg[1]) ==
'.png') {
80 $imgfonction =
'imagecreatefrompng';
82 if (strtolower($reg[1]) ==
'.bmp') {
83 $imgfonction =
'imagecreatefromwbmp';
85 if (strtolower($reg[1]) ==
'.webp') {
86 $imgfonction =
'imagecreatefromwebp';
88 if (strtolower($reg[1]) ==
'.xpm') {
89 $imgfonction =
'imagecreatefromxpm';
91 if (strtolower($reg[1]) ==
'.xbm') {
92 $imgfonction =
'imagecreatefromxbm';
94 if (strtolower($reg[1]) ==
'.svg') {
95 $imgfonction =
'imagecreatefromsvg';
98 if (!function_exists($imgfonction)) {
132 $infoImg = getimagesize($filetoread);
134 $ret[
'width'] = $infoImg[0];
135 $ret[
'height'] = $infoImg[1];
137 $ret[
'width'] = $ret[
'height'] =
'';
159 function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite =
'', $newquality = 0)
161 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
163 global $conf, $langs;
165 dol_syslog(
"dol_imageResizeOrCrop file=".$file.
" mode=".$mode.
" newWidth=".$newWidth.
" newHeight=".$newHeight.
" src_x=".$src_x.
" src_y=".$src_y);
173 return 'Bad parameter file';
174 } elseif (!file_exists($file)) {
176 return $langs->trans(
"ErrorFileNotFound", $file);
178 return 'This filename '.$file.
' does not seem to be an image filename.';
179 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
180 return 'Wrong value for parameter newWidth or newHeight';
181 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
182 return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
183 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
184 return 'Both newHeight or newWidth must be defined for croping';
189 $infoImg = getimagesize($filetoread);
190 $imgWidth = $infoImg[0];
191 $imgHeight = $infoImg[1];
193 $imgTargetName = ($filetowrite ? $filetowrite : $file);
194 $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
197 if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
198 $newWidth = $imgWidth;
199 $newHeight = $imgHeight;
202 if ($newWidth <= 0) {
203 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth);
205 if ($newHeight <= 0) {
206 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight);
212 switch ($infoImg[2]) {
214 $imgfonction =
'imagecreatefromgif';
217 $imgfonction =
'imagecreatefromjpeg';
220 $imgfonction =
'imagecreatefrompng';
223 $imgfonction =
'imagecreatefromwbmp';
226 $imgfonction =
'imagecreatefromwebp';
230 if (!function_exists($imgfonction)) {
232 return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
241 $imgfonction =
'imagecreatefromgif';
245 $imgfonction =
'imagecreatefromjpeg';
248 $imgfonction =
'imagecreatefrompng';
251 $imgfonction =
'imagecreatefromwbmp';
254 $imgfonction =
'imagecreatefromwebp';
258 if (!function_exists($imgfonction)) {
260 return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
266 switch ($infoImg[2]) {
268 $img = imagecreatefromgif($filetoread);
272 $img = imagecreatefromjpeg($filetoread);
276 $img = imagecreatefrompng($filetoread);
280 $img = imagecreatefromwbmp($filetoread);
284 $img = imagecreatefromwebp($filetoread);
290 if ($newExt ==
'gif') {
292 $imgTarget = imagecreate($newWidth, $newHeight);
294 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
298 if (function_exists(
'imageantialias')) {
299 imageantialias($imgTarget,
true);
303 if (function_exists(
'imagesavealpha')) {
304 imagesavealpha($imgTarget,
true);
311 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
312 imagecolortransparent($imgTarget, $trans_colour);
316 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
319 imagealphablending($imgTarget,
false);
320 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
323 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
326 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
329 if (function_exists(
"imagefill") && $trans_colour > 0) {
330 imagefill($imgTarget, 0, 0, $trans_colour);
333 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
335 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
345 imagegif($imgTarget, $imgTargetName);
349 $newquality = ($newquality ? $newquality :
'100');
350 imagejpeg($imgTarget, $imgTargetName, $newquality);
354 imagepng($imgTarget, $imgTargetName, $newquality);
358 imagewbmp($imgTarget, $imgTargetName);
361 $newquality = ($newquality ? $newquality :
'100');
362 imagewebp($imgTarget, $imgTargetName, $newquality);
365 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
373 imagedestroy($imgTarget);
377 return $imgTargetName;
405 if (function_exists(
'exif_read_data')) {
406 $exif = @exif_read_data($fileSource);
407 if ($exif && isset($exif[
'Orientation'])) {
408 $infoImg = getimagesize($fileSource);
410 $orientation = $exif[
'Orientation'];
411 if ($orientation != 1) {
412 $img = imagecreatefromjpeg($fileSource);
414 switch ($orientation) {
426 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
427 imagealphablending($img,
false);
428 imagesavealpha($img,
true);
429 $img = imagerotate($img, $deg, imageColorAllocateAlpha($img, 0, 0, 0, 127));
430 imagealphablending($img,
false);
431 imagesavealpha($img,
true);
433 $img = imagerotate($img, $deg, 0);
437 if ($fileDest ===
false) {
444 switch ($infoImg[2]) {
446 $image = imagegif($img, $fileDest);
449 $image = imagejpeg($img, $fileDest, $quality);
452 $image = imagepng($img, $fileDest, $quality);
458 $image = imagewbmp($img, $fileDest);
487 function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
489 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
491 global $conf, $langs;
493 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
501 return 'ErrorBadParameters';
502 } elseif (!file_exists($file)) {
504 dol_syslog($langs->trans(
"ErrorFileNotFound", $file), LOG_ERR);
505 return $langs->trans(
"ErrorFileNotFound", $file);
507 dol_syslog(
'This file '.$file.
' does not seem to be an image format file name.', LOG_WARNING);
508 return 'ErrorBadImageFormat';
509 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
511 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
512 return 'Error: Wrong value for parameter maxWidth';
513 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
515 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
516 return 'Error: Wrong value for parameter maxHeight';
521 $infoImg = getimagesize($filetoread);
522 $imgWidth = $infoImg[0];
523 $imgHeight = $infoImg[1];
526 if (function_exists(
'exif_read_data')) {
527 $exif = @exif_read_data($filetoread);
528 if ($exif && !empty($exif[
'Orientation'])) {
529 $ort = $exif[
'Orientation'];
533 if ($maxWidth == -1) {
534 $maxWidth = $infoImg[0];
536 if ($maxHeight == -1) {
537 $maxHeight = $infoImg[1];
541 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
543 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
548 switch ($infoImg[2]) {
550 $imgfonction =
'imagecreatefromgif';
553 $imgfonction =
'imagecreatefromjpeg';
556 $imgfonction =
'imagecreatefrompng';
562 $imgfonction =
'imagecreatefromwbmp';
566 if (!function_exists($imgfonction)) {
568 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
573 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
578 switch ($infoImg[2]) {
580 $img = imagecreatefromgif($filetoread);
584 $img = imagecreatefromjpeg($filetoread);
585 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
588 $img = imagecreatefrompng($filetoread);
596 $img = imagecreatefromwbmp($filetoread);
602 if (!is_resource($img) && !($img instanceof \GdImage)) {
603 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
608 if ($ort && !empty($conf->global->MAIN_USE_EXIF_ROTATION)) {
616 $trueImgWidth = $infoImg[1];
617 $trueImgHeight = $infoImg[0];
622 $trueImgWidth = $infoImg[1];
623 $trueImgHeight = $infoImg[0];
631 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
632 imagealphablending($img,
false);
633 imagesavealpha($img,
true);
634 $rotated = imagerotate($img, $exifAngle, imageColorAllocateAlpha($img, 0, 0, 0, 127));
635 imagealphablending($rotated,
false);
636 imagesavealpha($rotated,
true);
638 $rotated = imagerotate($img, $exifAngle, 0);
642 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
644 $imgWidth = $trueImgWidth;
645 $imgHeight = $trueImgHeight;
650 if ($maxWidth > $imgWidth) {
651 $maxWidth = $imgWidth;
653 if ($maxHeight > $imgHeight) {
654 $maxHeight = $imgHeight;
657 $whFact = $maxWidth / $maxHeight;
658 $imgWhFact = $imgWidth / $imgHeight;
661 if ($whFact < $imgWhFact) {
663 $thumbWidth = $maxWidth;
664 $thumbHeight = $thumbWidth / $imgWhFact;
667 $thumbHeight = $maxHeight;
668 $thumbWidth = $thumbHeight * $imgWhFact;
670 $thumbHeight = round($thumbHeight);
671 $thumbWidth = round($thumbWidth);
674 if (empty($targetformat)) {
675 $targetformat = $infoImg[2];
679 if ($targetformat == IMAGETYPE_GIF) {
681 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
683 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
687 if (function_exists(
'imageantialias')) {
688 imageantialias($imgThumb,
true);
692 if (function_exists(
'imagesavealpha')) {
693 imagesavealpha($imgThumb,
true);
698 switch ($targetformat) {
700 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
701 imagecolortransparent($imgThumb, $trans_colour);
702 $extImgTarget =
'.gif';
706 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
707 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
708 $newquality = $quality;
711 imagealphablending($imgThumb,
false);
712 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
713 $extImgTarget =
'.png';
714 $newquality = $quality - 100;
715 $newquality = round(abs($quality - 100) * 9 / 100);
719 $extImgTarget =
'.bmp';
723 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
724 $extImgTarget =
'.bmp';
728 if (function_exists(
"imagefill")) {
729 imagefill($imgThumb, 0, 0, $trans_colour);
732 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
734 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
736 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
737 $fileName = basename($fileName);
747 switch ($targetformat) {
749 imagegif($imgThumb, $imgThumbName);
752 imagejpeg($imgThumb, $imgThumbName, $newquality);
755 imagepng($imgThumb, $imgThumbName, $newquality);
761 imagewbmp($imgThumb, $imgThumbName);
770 imagedestroy($imgThumb);
772 return $imgThumbName;