26 $maxwidthsmall = 480; $maxheightsmall = 270;
27 $maxwidthmini = 128; $maxheightmini = 72;
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';
50 if (!preg_match(
'/('.$regeximgext.
')$/i', $file, $reg))
return -1;
54 if (strtolower($reg[1]) ==
'.gif') $imgfonction =
'imagecreatefromgif';
55 if (strtolower($reg[1]) ==
'.jpg') $imgfonction =
'imagecreatefromjpeg';
56 if (strtolower($reg[1]) ==
'.jpeg') $imgfonction =
'imagecreatefromjpeg';
57 if (strtolower($reg[1]) ==
'.png') $imgfonction =
'imagecreatefrompng';
58 if (strtolower($reg[1]) ==
'.bmp') $imgfonction =
'imagecreatefromwbmp';
59 if (strtolower($reg[1]) ==
'.webp') $imgfonction =
'imagecreatefromwebp';
60 if (strtolower($reg[1]) ==
'.xpm') $imgfonction =
'imagecreatefromxpm';
61 if (strtolower($reg[1]) ==
'.xbm') $imgfonction =
'imagecreatefromxbm';
62 if (strtolower($reg[1]) ==
'.svg') $imgfonction =
'imagecreatefromsvg';
65 if (!function_exists($imgfonction))
100 $infoImg = getimagesize($filetoread);
101 $ret[
'width'] = $infoImg[0];
102 $ret[
'height'] = $infoImg[1];
122 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
124 global $conf, $langs;
126 dol_syslog(
"dol_imageResizeOrCrop file=".$file.
" mode=".$mode.
" newWidth=".$newWidth.
" newHeight=".$newHeight.
" src_x=".$src_x.
" src_y=".$src_y);
135 return 'Bad parameter file';
136 } elseif (!file_exists($file))
139 return $langs->trans(
"ErrorFileNotFound", $file);
142 return 'This filename '.$file.
' does not seem to be an image filename.';
143 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight))
145 return 'Wrong value for parameter newWidth or newHeight';
146 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0)
148 return 'At least newHeight or newWidth must be defined for resizing';
149 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0))
151 return 'Both newHeight or newWidth must be defined for croping';
156 $infoImg = getimagesize($filetoread);
157 $imgWidth = $infoImg[0];
158 $imgHeight = $infoImg[1];
164 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth);
168 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight);
176 $imgfonction =
'imagecreatefromgif';
179 $imgfonction =
'imagecreatefromjpeg';
182 $imgfonction =
'imagecreatefrompng';
185 $imgfonction =
'imagecreatefromwbmp';
188 $imgfonction =
'imagecreatefromwebp';
193 if (!function_exists($imgfonction))
196 return 'Resize not possible. This PHP does not support GD functions '.$imgfonction;
204 $img = imagecreatefromgif($filetoread);
209 $img = imagecreatefromjpeg($filetoread);
214 $img = imagecreatefrompng($filetoread);
219 $img = imagecreatefromwbmp($filetoread);
224 $img = imagecreatefromwebp($filetoread);
231 if ($infoImg[2] == 1)
234 $imgThumb = imagecreate($newWidth, $newHeight);
236 $imgThumb = imagecreatetruecolor($newWidth, $newHeight);
240 if (function_exists(
'imageantialias'))
242 imageantialias($imgThumb,
true);
246 if (function_exists(
'imagesavealpha'))
248 imagesavealpha($imgThumb,
true);
255 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
256 imagecolortransparent($imgThumb, $trans_colour);
259 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
262 imagealphablending($imgThumb,
false);
263 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
266 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
269 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
272 if (function_exists(
"imagefill")) imagefill($imgThumb, 0, 0, $trans_colour);
274 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as $extImg, newquality=$newquality");
276 imagecopyresampled($imgThumb, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
278 $imgThumbName = $file;
288 imagegif($imgThumb, $imgThumbName);
291 imagejpeg($imgThumb, $imgThumbName, $newquality);
294 imagepng($imgThumb, $imgThumbName, $newquality);
297 imagewbmp($imgThumb, $imgThumbName);
300 imagewebp($imgThumb, $imgThumbName, $newquality);
305 if (!empty($conf->global->MAIN_UMASK)) @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
309 imagedestroy($imgThumb);
313 return $imgThumbName;
341 if (function_exists(
'exif_read_data')) {
342 $exif = @exif_read_data($fileSource);
343 if ($exif && isset($exif[
'Orientation'])) {
344 $infoImg = getimagesize($fileSource);
346 $orientation = $exif[
'Orientation'];
347 if ($orientation != 1) {
348 $img = imagecreatefromjpeg($fileSource);
350 switch ($orientation) {
362 if ($infoImg[2] ===
'IMAGETYPE_PNG')
364 imagealphablending($img,
false);
365 imagesavealpha($img,
true);
366 $img = imagerotate($img, $deg, imageColorAllocateAlpha($img, 0, 0, 0, 127));
367 imagealphablending($img,
false);
368 imagesavealpha($img,
true);
370 $img = imagerotate($img, $deg, 0);
374 if ($fileDest ===
false) {
384 $image = imagegif($img, $fileDest);
387 $image = imagejpeg($img, $fileDest, $quality);
390 $image = imagepng($img, $fileDest, $quality);
396 $image = imagewbmp($img, $fileDest);
425 function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
427 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
429 global $conf, $langs;
431 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
440 return 'ErrorBadParameters';
441 } elseif (!file_exists($file))
444 dol_syslog($langs->trans(
"ErrorFileNotFound", $file), LOG_ERR);
445 return $langs->trans(
"ErrorFileNotFound", $file);
448 dol_syslog(
'This file '.$file.
' does not seem to be an image format file name.', LOG_WARNING);
449 return 'ErrorBadImageFormat';
450 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
452 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
453 return 'Error: Wrong value for parameter maxWidth';
454 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
456 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
457 return 'Error: Wrong value for parameter maxHeight';
462 $infoImg = getimagesize($filetoread);
463 $imgWidth = $infoImg[0];
464 $imgHeight = $infoImg[1];
467 if (function_exists(
'exif_read_data')) {
468 $exif = @exif_read_data($filetoread);
469 if ($exif && !empty($exif[
'Orientation'])) {
470 $ort = $exif[
'Orientation'];
474 if ($maxWidth == -1) $maxWidth = $infoImg[0];
475 if ($maxHeight == -1) $maxHeight = $infoImg[1];
478 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight)
481 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
489 $imgfonction =
'imagecreatefromgif';
492 $imgfonction =
'imagecreatefromjpeg';
495 $imgfonction =
'imagecreatefrompng';
501 $imgfonction =
'imagecreatefromwbmp';
506 if (!function_exists($imgfonction))
509 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
514 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
522 $img = imagecreatefromgif($filetoread);
526 $img = imagecreatefromjpeg($filetoread);
527 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
530 $img = imagecreatefrompng($filetoread);
538 $img = imagecreatefromwbmp($filetoread);
543 if (!is_resource($img))
545 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
550 if ($ort && !empty($conf->global->MAIN_USE_EXIF_ROTATION)) {
559 $trueImgWidth = $infoImg[1];
560 $trueImgHeight = $infoImg[0];
565 $trueImgWidth = $infoImg[1];
566 $trueImgHeight = $infoImg[0];
575 if ($infoImg[2] ===
'IMAGETYPE_PNG')
577 imagealphablending($img,
false);
578 imagesavealpha($img,
true);
579 $rotated = imagerotate($img, $exifAngle, imageColorAllocateAlpha($img, 0, 0, 0, 127));
580 imagealphablending($rotated,
false);
581 imagesavealpha($rotated,
true);
583 $rotated = imagerotate($img, $exifAngle, 0);
587 if (!empty($rotated)) {
589 $imgWidth = $trueImgWidth;
590 $imgHeight = $trueImgHeight;
595 if ($maxWidth > $imgWidth) { $maxWidth = $imgWidth; }
596 if ($maxHeight > $imgHeight) { $maxHeight = $imgHeight; }
598 $whFact = $maxWidth / $maxHeight;
599 $imgWhFact = $imgWidth / $imgHeight;
602 if ($whFact < $imgWhFact)
605 $thumbWidth = $maxWidth;
606 $thumbHeight = $thumbWidth / $imgWhFact;
609 $thumbHeight = $maxHeight;
610 $thumbWidth = $thumbHeight * $imgWhFact;
612 $thumbHeight = round($thumbHeight);
613 $thumbWidth = round($thumbWidth);
616 if (empty($targetformat)) $targetformat = $infoImg[2];
619 if ($targetformat == IMAGETYPE_GIF)
622 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
624 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
628 if (function_exists(
'imageantialias'))
630 imageantialias($imgThumb,
true);
634 if (function_exists(
'imagesavealpha'))
636 imagesavealpha($imgThumb,
true);
641 switch ($targetformat)
644 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
645 imagecolortransparent($imgThumb, $trans_colour);
646 $extImgTarget =
'.gif';
650 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
651 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
652 $newquality = $quality;
655 imagealphablending($imgThumb,
false);
656 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
657 $extImgTarget =
'.png';
658 $newquality = $quality - 100;
659 $newquality = round(abs($quality - 100) * 9 / 100);
663 $extImgTarget =
'.bmp';
667 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
668 $extImgTarget =
'.bmp';
672 if (function_exists(
"imagefill")) imagefill($imgThumb, 0, 0, $trans_colour);
674 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
676 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
678 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
679 $fileName = basename($fileName);
689 switch ($targetformat)
692 imagegif($imgThumb, $imgThumbName);
695 imagejpeg($imgThumb, $imgThumbName, $newquality);
698 imagepng($imgThumb, $imgThumbName, $newquality);
704 imagewbmp($imgThumb, $imgThumbName);
709 if (!empty($conf->global->MAIN_UMASK)) @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
713 imagedestroy($imgThumb);
715 return $imgThumbName;
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
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)...
dol_getImageSize($file, $url=false)
Return size of image file on disk (Supported extensions are gif, jpg, png and bmp) ...
dolRotateImage($file_path)
dolRotateImage if image is a jpg file.
correctExifImageOrientation($fileSource, $fileDest, $quality=95)
Add exif orientation correction for image.
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.
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_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x=0, $src_y=0)
Resize or crop an image file (Supported extensions are gif, jpg, png and bmp)
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)