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);
133 $ret[
'width'] = $infoImg[0];
134 $ret[
'height'] = $infoImg[1];
155 function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite =
'', $newquality = 0)
157 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
159 global $conf, $langs;
161 dol_syslog(
"dol_imageResizeOrCrop file=".$file.
" mode=".$mode.
" newWidth=".$newWidth.
" newHeight=".$newHeight.
" src_x=".$src_x.
" src_y=".$src_y);
169 return 'Bad parameter file';
170 } elseif (!file_exists($file)) {
172 return $langs->trans(
"ErrorFileNotFound", $file);
174 return 'This filename '.$file.
' does not seem to be an image filename.';
175 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
176 return 'Wrong value for parameter newWidth or newHeight';
177 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
178 return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
179 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
180 return 'Both newHeight or newWidth must be defined for croping';
185 $infoImg = getimagesize($filetoread);
186 $imgWidth = $infoImg[0];
187 $imgHeight = $infoImg[1];
189 $imgTargetName = ($filetowrite ? $filetowrite : $file);
190 $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
193 if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
194 $newWidth = $imgWidth;
195 $newHeight = $imgHeight;
198 if ($newWidth <= 0) {
199 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth);
201 if ($newHeight <= 0) {
202 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight);
208 switch ($infoImg[2]) {
210 $imgfonction =
'imagecreatefromgif';
213 $imgfonction =
'imagecreatefromjpeg';
216 $imgfonction =
'imagecreatefrompng';
219 $imgfonction =
'imagecreatefromwbmp';
222 $imgfonction =
'imagecreatefromwebp';
226 if (!function_exists($imgfonction)) {
228 return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
237 $imgfonction =
'imagecreatefromgif';
241 $imgfonction =
'imagecreatefromjpeg';
244 $imgfonction =
'imagecreatefrompng';
247 $imgfonction =
'imagecreatefromwbmp';
250 $imgfonction =
'imagecreatefromwebp';
254 if (!function_exists($imgfonction)) {
256 return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
262 switch ($infoImg[2]) {
264 $img = imagecreatefromgif($filetoread);
268 $img = imagecreatefromjpeg($filetoread);
272 $img = imagecreatefrompng($filetoread);
276 $img = imagecreatefromwbmp($filetoread);
280 $img = imagecreatefromwebp($filetoread);
286 if ($newExt ==
'gif') {
288 $imgTarget = imagecreate($newWidth, $newHeight);
290 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
294 if (function_exists(
'imageantialias')) {
295 imageantialias($imgTarget,
true);
299 if (function_exists(
'imagesavealpha')) {
300 imagesavealpha($imgTarget,
true);
307 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
308 imagecolortransparent($imgTarget, $trans_colour);
312 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
315 imagealphablending($imgTarget,
false);
316 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
319 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
322 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
325 if (function_exists(
"imagefill") && $trans_colour > 0) {
326 imagefill($imgTarget, 0, 0, $trans_colour);
329 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
331 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
341 imagegif($imgTarget, $imgTargetName);
345 $newquality = ($newquality ? $newquality :
'100');
346 imagejpeg($imgTarget, $imgTargetName, $newquality);
350 imagepng($imgTarget, $imgTargetName, $newquality);
354 imagewbmp($imgTarget, $imgTargetName);
357 $newquality = ($newquality ? $newquality :
'100');
358 imagewebp($imgTarget, $imgTargetName, $newquality);
361 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
365 if (!empty($conf->global->MAIN_UMASK)) {
366 @chmod($imgTargetName, octdec($conf->global->MAIN_UMASK));
371 imagedestroy($imgTarget);
375 return $imgTargetName;
403 if (function_exists(
'exif_read_data')) {
404 $exif = @exif_read_data($fileSource);
405 if ($exif && isset($exif[
'Orientation'])) {
406 $infoImg = getimagesize($fileSource);
408 $orientation = $exif[
'Orientation'];
409 if ($orientation != 1) {
410 $img = imagecreatefromjpeg($fileSource);
412 switch ($orientation) {
424 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
425 imagealphablending($img,
false);
426 imagesavealpha($img,
true);
427 $img = imagerotate($img, $deg, imageColorAllocateAlpha($img, 0, 0, 0, 127));
428 imagealphablending($img,
false);
429 imagesavealpha($img,
true);
431 $img = imagerotate($img, $deg, 0);
435 if ($fileDest ===
false) {
442 switch ($infoImg[2]) {
444 $image = imagegif($img, $fileDest);
447 $image = imagejpeg($img, $fileDest, $quality);
450 $image = imagepng($img, $fileDest, $quality);
456 $image = imagewbmp($img, $fileDest);
485 function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
487 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
489 global $conf, $langs;
491 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
499 return 'ErrorBadParameters';
500 } elseif (!file_exists($file)) {
502 dol_syslog($langs->trans(
"ErrorFileNotFound", $file), LOG_ERR);
503 return $langs->trans(
"ErrorFileNotFound", $file);
505 dol_syslog(
'This file '.$file.
' does not seem to be an image format file name.', LOG_WARNING);
506 return 'ErrorBadImageFormat';
507 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
509 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
510 return 'Error: Wrong value for parameter maxWidth';
511 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
513 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
514 return 'Error: Wrong value for parameter maxHeight';
519 $infoImg = getimagesize($filetoread);
520 $imgWidth = $infoImg[0];
521 $imgHeight = $infoImg[1];
524 if (function_exists(
'exif_read_data')) {
525 $exif = @exif_read_data($filetoread);
526 if ($exif && !empty($exif[
'Orientation'])) {
527 $ort = $exif[
'Orientation'];
531 if ($maxWidth == -1) {
532 $maxWidth = $infoImg[0];
534 if ($maxHeight == -1) {
535 $maxHeight = $infoImg[1];
539 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
541 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
546 switch ($infoImg[2]) {
548 $imgfonction =
'imagecreatefromgif';
551 $imgfonction =
'imagecreatefromjpeg';
554 $imgfonction =
'imagecreatefrompng';
560 $imgfonction =
'imagecreatefromwbmp';
564 if (!function_exists($imgfonction)) {
566 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
571 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
576 switch ($infoImg[2]) {
578 $img = imagecreatefromgif($filetoread);
582 $img = imagecreatefromjpeg($filetoread);
583 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
586 $img = imagecreatefrompng($filetoread);
594 $img = imagecreatefromwbmp($filetoread);
600 if (!is_resource($img) && !($img instanceof \GdImage)) {
601 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
606 if ($ort && !empty($conf->global->MAIN_USE_EXIF_ROTATION)) {
614 $trueImgWidth = $infoImg[1];
615 $trueImgHeight = $infoImg[0];
620 $trueImgWidth = $infoImg[1];
621 $trueImgHeight = $infoImg[0];
629 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
630 imagealphablending($img,
false);
631 imagesavealpha($img,
true);
632 $rotated = imagerotate($img, $exifAngle, imageColorAllocateAlpha($img, 0, 0, 0, 127));
633 imagealphablending($rotated,
false);
634 imagesavealpha($rotated,
true);
636 $rotated = imagerotate($img, $exifAngle, 0);
640 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
642 $imgWidth = $trueImgWidth;
643 $imgHeight = $trueImgHeight;
648 if ($maxWidth > $imgWidth) {
649 $maxWidth = $imgWidth;
651 if ($maxHeight > $imgHeight) {
652 $maxHeight = $imgHeight;
655 $whFact = $maxWidth / $maxHeight;
656 $imgWhFact = $imgWidth / $imgHeight;
659 if ($whFact < $imgWhFact) {
661 $thumbWidth = $maxWidth;
662 $thumbHeight = $thumbWidth / $imgWhFact;
665 $thumbHeight = $maxHeight;
666 $thumbWidth = $thumbHeight * $imgWhFact;
668 $thumbHeight = round($thumbHeight);
669 $thumbWidth = round($thumbWidth);
672 if (empty($targetformat)) {
673 $targetformat = $infoImg[2];
677 if ($targetformat == IMAGETYPE_GIF) {
679 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
681 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
685 if (function_exists(
'imageantialias')) {
686 imageantialias($imgThumb,
true);
690 if (function_exists(
'imagesavealpha')) {
691 imagesavealpha($imgThumb,
true);
696 switch ($targetformat) {
698 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
699 imagecolortransparent($imgThumb, $trans_colour);
700 $extImgTarget =
'.gif';
704 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
705 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
706 $newquality = $quality;
709 imagealphablending($imgThumb,
false);
710 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
711 $extImgTarget =
'.png';
712 $newquality = $quality - 100;
713 $newquality = round(abs($quality - 100) * 9 / 100);
717 $extImgTarget =
'.bmp';
721 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
722 $extImgTarget =
'.bmp';
726 if (function_exists(
"imagefill")) {
727 imagefill($imgThumb, 0, 0, $trans_colour);
730 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
732 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
734 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
735 $fileName = basename($fileName);
745 switch ($targetformat) {
747 imagegif($imgThumb, $imgThumbName);
750 imagejpeg($imgThumb, $imgThumbName, $newquality);
753 imagepng($imgThumb, $imgThumbName, $newquality);
759 imagewbmp($imgThumb, $imgThumbName);
764 if (!empty($conf->global->MAIN_UMASK)) {
765 @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
770 imagedestroy($imgThumb);
772 return $imgThumbName;
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
getImageFileNameForSize($file, $extName, $extImgTarget='')
Return the filename of file to get the thumbs.
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.
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.