29 $maxheightsmall = 270;
34 if (!defined(
'IMAGETYPE_WEBP')) {
35 define(
'IMAGETYPE_WEBP', 18);
47 $maxheightsmall = 270;
53 'maxwidthsmall' => $maxwidthsmall,
54 'maxheightsmall' => $maxheightsmall,
55 'maxwidthmini' => $maxwidthmini,
56 'maxheightmini' => $maxheightmini,
69 $regeximgext =
'\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm';
71 $regeximgext .=
'|\.svg';
90 if (!preg_match(
'/('.$regeximgext.
')$/i', $file, $reg)) {
96 if (strtolower($reg[1]) ==
'.gif') {
97 $imgfonction =
'imagecreatefromgif';
99 if (strtolower($reg[1]) ==
'.jpg') {
100 $imgfonction =
'imagecreatefromjpeg';
102 if (strtolower($reg[1]) ==
'.jpeg') {
103 $imgfonction =
'imagecreatefromjpeg';
105 if (strtolower($reg[1]) ==
'.png') {
106 $imgfonction =
'imagecreatefrompng';
108 if (strtolower($reg[1]) ==
'.bmp') {
109 $imgfonction =
'imagecreatefromwbmp';
111 if (strtolower($reg[1]) ==
'.webp') {
112 $imgfonction =
'imagecreatefromwebp';
114 if (strtolower($reg[1]) ==
'.xpm') {
115 $imgfonction =
'imagecreatefromxpm';
117 if (strtolower($reg[1]) ==
'.xbm') {
118 $imgfonction =
'imagecreatefromxbm';
120 if (strtolower($reg[1]) ==
'.svg') {
121 $imgfonction =
'imagecreatefromsvg';
124 if (!function_exists($imgfonction)) {
158 $infoImg = getimagesize($filetoread);
160 $ret[
'width'] = $infoImg[0];
161 $ret[
'height'] = $infoImg[1];
163 $ret[
'width'] = $ret[
'height'] =
'';
185 function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite =
'', $newquality = 0)
187 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
189 global $conf, $langs;
191 dol_syslog(
"dol_imageResizeOrCrop file=".$file.
" mode=".$mode.
" newWidth=".$newWidth.
" newHeight=".$newHeight.
" src_x=".$src_x.
" src_y=".$src_y);
199 return 'Bad parameter file';
200 } elseif (!file_exists($file)) {
202 return $langs->trans(
"ErrorFileNotFound", $file);
204 return 'This filename '.$file.
' does not seem to be an image filename.';
205 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
206 return 'Wrong value for parameter newWidth or newHeight';
207 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
208 return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
209 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
210 return 'Both newHeight or newWidth must be defined for croping';
215 $infoImg = getimagesize($filetoread);
216 $imgWidth = $infoImg[0];
217 $imgHeight = $infoImg[1];
219 $imgTargetName = ($filetowrite ? $filetowrite : $file);
220 $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
223 if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
224 $newWidth = $imgWidth;
225 $newHeight = $imgHeight;
228 if ($newWidth <= 0) {
229 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth);
231 if ($newHeight <= 0) {
232 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight);
238 switch ($infoImg[2]) {
240 $imgfonction =
'imagecreatefromgif';
243 $imgfonction =
'imagecreatefromjpeg';
246 $imgfonction =
'imagecreatefrompng';
249 $imgfonction =
'imagecreatefromwbmp';
252 $imgfonction =
'imagecreatefromwebp';
256 if (!function_exists($imgfonction)) {
258 return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
267 $imgfonction =
'imagecreatefromgif';
271 $imgfonction =
'imagecreatefromjpeg';
274 $imgfonction =
'imagecreatefrompng';
277 $imgfonction =
'imagecreatefromwbmp';
280 $imgfonction =
'imagecreatefromwebp';
284 if (!function_exists($imgfonction)) {
286 return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
292 switch ($infoImg[2]) {
294 $img = imagecreatefromgif($filetoread);
298 $img = imagecreatefromjpeg($filetoread);
302 $img = imagecreatefrompng($filetoread);
306 $img = imagecreatefromwbmp($filetoread);
310 $img = imagecreatefromwebp($filetoread);
316 if ($newExt ==
'gif') {
318 $imgTarget = imagecreate($newWidth, $newHeight);
320 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
324 if (function_exists(
'imageantialias')) {
325 imageantialias($imgTarget,
true);
329 if (function_exists(
'imagesavealpha')) {
330 imagesavealpha($imgTarget,
true);
337 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
338 imagecolortransparent($imgTarget, $trans_colour);
342 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
345 imagealphablending($imgTarget,
false);
346 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
349 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
352 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
355 if (function_exists(
"imagefill") && $trans_colour > 0) {
356 imagefill($imgTarget, 0, 0, $trans_colour);
359 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 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
371 imagegif($imgTarget, $imgTargetName);
375 $newquality = ($newquality ? $newquality :
'100');
376 imagejpeg($imgTarget, $imgTargetName, $newquality);
380 imagepng($imgTarget, $imgTargetName, $newquality);
384 imagewbmp($imgTarget, $imgTargetName);
387 $newquality = ($newquality ? $newquality :
'100');
388 imagewebp($imgTarget, $imgTargetName, $newquality);
391 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
399 imagedestroy($imgTarget);
403 return $imgTargetName;
431 if (function_exists(
'exif_read_data')) {
432 $exif = @exif_read_data($fileSource);
433 if ($exif && isset($exif[
'Orientation'])) {
434 $infoImg = getimagesize($fileSource);
436 $orientation = $exif[
'Orientation'];
437 if ($orientation != 1) {
438 $img = imagecreatefromjpeg($fileSource);
440 switch ($orientation) {
452 if ($infoImg[2] === IMAGETYPE_PNG) {
453 imagealphablending($img,
false);
454 imagesavealpha($img,
true);
455 $img = imagerotate($img, $deg, imagecolorallocatealpha($img, 0, 0, 0, 127));
456 imagealphablending($img,
false);
457 imagesavealpha($img,
true);
459 $img = imagerotate($img, $deg, 0);
463 if ($fileDest ===
false) {
470 switch ($infoImg[2]) {
472 $image = imagegif($img, $fileDest);
475 $image = imagejpeg($img, $fileDest, $quality);
478 $image = imagepng($img, $fileDest, $quality);
484 $image = imagewbmp($img, $fileDest);
513 function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
515 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
517 global $conf, $langs;
519 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
527 return 'ErrorBadParameters';
528 } elseif (!file_exists($file)) {
530 dol_syslog($langs->trans(
"ErrorFileNotFound", $file), LOG_ERR);
531 return $langs->trans(
"ErrorFileNotFound", $file);
533 dol_syslog(
'This file '.$file.
' does not seem to be an image format file name.', LOG_WARNING);
534 return 'ErrorBadImageFormat';
535 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
537 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
538 return 'Error: Wrong value for parameter maxWidth';
539 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
541 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
542 return 'Error: Wrong value for parameter maxHeight';
547 $infoImg = getimagesize($filetoread);
548 $imgWidth = $infoImg[0];
549 $imgHeight = $infoImg[1];
552 if (function_exists(
'exif_read_data')) {
553 $exif = @exif_read_data($filetoread);
554 if ($exif && !empty($exif[
'Orientation'])) {
555 $ort = $exif[
'Orientation'];
559 if ($maxWidth == -1) {
560 $maxWidth = $infoImg[0];
562 if ($maxHeight == -1) {
563 $maxHeight = $infoImg[1];
567 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
569 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
574 switch ($infoImg[2]) {
576 $imgfonction =
'imagecreatefromgif';
579 $imgfonction =
'imagecreatefromjpeg';
582 $imgfonction =
'imagecreatefrompng';
588 $imgfonction =
'imagecreatefromwbmp';
591 $imgfonction =
'imagecreatefromwebp';
595 if (!function_exists($imgfonction)) {
597 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
602 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
607 switch ($infoImg[2]) {
609 $img = imagecreatefromgif($filetoread);
613 $img = imagecreatefromjpeg($filetoread);
614 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
617 $img = imagecreatefrompng($filetoread);
625 $img = imagecreatefromwbmp($filetoread);
629 $img = imagecreatefromwebp($filetoread);
635 if (!is_resource($img) && class_exists(
'GdImage') && !($img instanceof GdImage)) {
636 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
649 $trueImgWidth = $infoImg[1];
650 $trueImgHeight = $infoImg[0];
655 $trueImgWidth = $infoImg[1];
656 $trueImgHeight = $infoImg[0];
664 if ($infoImg[2] === IMAGETYPE_PNG) {
665 imagealphablending($img,
false);
666 imagesavealpha($img,
true);
667 $rotated = imagerotate($img, $exifAngle, imagecolorallocatealpha($img, 0, 0, 0, 127));
668 imagealphablending($rotated,
false);
669 imagesavealpha($rotated,
true);
671 $rotated = imagerotate($img, $exifAngle, 0);
675 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
677 $imgWidth = $trueImgWidth;
678 $imgHeight = $trueImgHeight;
683 if ($maxWidth > $imgWidth) {
684 $maxWidth = $imgWidth;
686 if ($maxHeight > $imgHeight) {
687 $maxHeight = $imgHeight;
690 $whFact = $maxWidth / $maxHeight;
691 $imgWhFact = $imgWidth / $imgHeight;
694 if ($whFact < $imgWhFact) {
696 $thumbWidth = $maxWidth;
697 $thumbHeight = $thumbWidth / $imgWhFact;
700 $thumbHeight = $maxHeight;
701 $thumbWidth = $thumbHeight * $imgWhFact;
703 $thumbHeight = (int) round($thumbHeight);
704 $thumbWidth = (int) round($thumbWidth);
707 if (empty($targetformat)) {
708 $targetformat = $infoImg[2];
712 if ($targetformat == IMAGETYPE_GIF) {
714 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
716 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
720 if (function_exists(
'imageantialias')) {
721 imageantialias($imgThumb,
true);
725 if (function_exists(
'imagesavealpha')) {
726 imagesavealpha($imgThumb,
true);
731 switch ($targetformat) {
733 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
734 imagecolortransparent($imgThumb, $trans_colour);
735 $extImgTarget =
'.gif';
739 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
740 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
741 $newquality = $quality;
744 imagealphablending($imgThumb,
false);
745 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
746 $extImgTarget =
'.png';
747 $newquality = $quality - 100;
748 $newquality = round(abs($quality - 100) * 9 / 100);
752 $extImgTarget =
'.bmp';
756 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
757 $extImgTarget =
'.bmp';
761 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
762 $extImgTarget =
'.webp';
763 $newquality = $quality;
766 if (function_exists(
"imagefill")) {
767 imagefill($imgThumb, 0, 0, $trans_colour);
770 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
772 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
774 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
775 $fileName = basename($fileName);
785 switch ($targetformat) {
787 imagegif($imgThumb, $imgThumbName);
790 imagejpeg($imgThumb, $imgThumbName, $newquality);
793 imagepng($imgThumb, $imgThumbName, $newquality);
799 imagewbmp($imgThumb, $imgThumbName);
802 imagewebp($imgThumb, $imgThumbName, $newquality);
811 imagedestroy($imgThumb);
813 return $imgThumbName;
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.
correctExifImageOrientation($fileSource, $fileDest, $quality=95)
Add exif orientation correction for image.
dolRotateImage($file_path)
dolRotateImage if image is a jpg file.
if(!defined('IMAGETYPE_WEBP')) getDefaultImageSizes()
Return default values for image sizes.
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.