189function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite =
'', $newquality = 0)
191 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
195 dol_syslog(
"dol_imageResizeOrCrop file=".$file.
" mode=".$mode.
" newWidth=".$newWidth.
" newHeight=".$newHeight.
" src_x=".$src_x.
" src_y=".$src_y);
203 return 'Bad parameter file';
204 } elseif (!file_exists($file)) {
206 $langs->load(
"errors");
207 return $langs->trans(
"ErrorFileNotFound", $file);
209 return 'This filename '.$file.
' does not seem to be an image filename.';
210 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
211 return 'Wrong value for parameter newWidth or newHeight';
212 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
213 return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
214 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
215 return 'Both newHeight or newWidth must be defined for croping';
224 $infoImg = getimagesize($filetoread);
225 $imgWidth = $infoImg[0];
226 $imgHeight = $infoImg[1];
228 $imgTargetName = ($filetowrite ? $filetowrite : $file);
229 $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
232 if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
233 $newWidth = $imgWidth;
234 $newHeight = $imgHeight;
237 if ($newWidth <= 0) {
238 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth);
240 if ($newHeight <= 0) {
241 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight);
251 switch ($infoImg[2]) {
253 $imgfonction =
'imagecreatefromgif';
256 $imgfonction =
'imagecreatefromjpeg';
259 $imgfonction =
'imagecreatefrompng';
262 $imgfonction =
'imagecreatefromwbmp';
265 $imgfonction =
'imagecreatefromwebp';
268 $imgfonction =
'imagecreatefromavif';
272 if (!function_exists($imgfonction)) {
274 return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
283 $imgfonction =
'imagecreatefromgif';
287 $imgfonction =
'imagecreatefromjpeg';
290 $imgfonction =
'imagecreatefrompng';
293 $imgfonction =
'imagecreatefromwbmp';
296 $imgfonction =
'imagecreatefromwebp';
299 $imgfonction =
'imagecreatefromavif';
303 if (!function_exists($imgfonction)) {
305 return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
313 switch ($infoImg[2]) {
315 $img = imagecreatefromgif($filetoread);
319 $img = imagecreatefromjpeg($filetoread);
323 $img = imagecreatefrompng($filetoread);
327 $img = imagecreatefromwbmp($filetoread);
331 $img = imagecreatefromwebp($filetoread);
337 return "Error: Could not create Image from '$filetoread'";
341 if ($newExt ==
'gif') {
343 $imgTarget = imagecreate($newWidth, $newHeight);
345 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
349 if (function_exists(
'imageantialias')) {
350 imageantialias($imgTarget,
true);
354 if (function_exists(
'imagesavealpha')) {
355 imagesavealpha($imgTarget,
true);
362 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
363 imagecolortransparent($imgTarget, $trans_colour);
367 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
370 imagealphablending($imgTarget,
false);
371 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
374 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
377 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
380 if (function_exists(
"imagefill") && $trans_colour > 0) {
381 imagefill($imgTarget, 0, 0, $trans_colour);
384 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
386 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
396 imagegif($imgTarget, $imgTargetName);
400 $newquality = ($newquality ? $newquality :
'100');
401 imagejpeg($imgTarget, $imgTargetName, $newquality);
405 imagepng($imgTarget, $imgTargetName, $newquality);
409 imagewbmp($imgTarget, $imgTargetName);
412 $newquality = ($newquality ? $newquality :
'100');
413 imagewebp($imgTarget, $imgTargetName, $newquality);
416 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
427 imagedestroy($imgTarget);
432 return $imgTargetName;
460 if (function_exists(
'exif_read_data')) {
461 $exif = @exif_read_data($fileSource);
462 if ($exif && isset($exif[
'Orientation'])) {
463 $infoImg = getimagesize($fileSource);
465 $orientation = $exif[
'Orientation'];
466 if ($orientation != 1) {
467 $img = imagecreatefromjpeg($fileSource);
469 switch ($orientation) {
481 if ($infoImg[2] === IMAGETYPE_PNG) {
482 imagealphablending($img,
false);
483 imagesavealpha($img,
true);
484 $img = imagerotate($img, $deg, imagecolorallocatealpha($img, 0, 0, 0, 127));
485 imagealphablending($img,
false);
486 imagesavealpha($img,
true);
488 $img = imagerotate($img, $deg, 0);
492 if ($fileDest ===
false) {
499 switch ($infoImg[2]) {
501 $image = imagegif($img, $fileDest);
504 $image = imagejpeg($img, $fileDest, $quality);
507 $image = imagepng($img, $fileDest, $quality);
513 $image = imagewbmp($img, $fileDest);
543function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
545 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
549 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
557 return 'ErrorBadParameters';
559 dol_syslog(
'This file '.$file.
' does not seem to be a supported image file name (bad extension).', LOG_WARNING);
560 return 'ErrorBadImageFormat';
561 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
563 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
564 return 'Error: Wrong value for parameter maxWidth';
565 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
567 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
568 return 'Error: Wrong value for parameter maxHeight';
573 if (!file_exists($filetoread)) {
575 $langs->load(
"errors");
576 dol_syslog($langs->trans(
"ErrorFileNotFound", $filetoread), LOG_ERR);
577 return $langs->trans(
"ErrorFileNotFound", $filetoread);
580 $infoImg = getimagesize($filetoread);
581 $imgWidth = $infoImg[0];
582 $imgHeight = $infoImg[1];
588 if (function_exists(
'exif_read_data')) {
589 $exif = @exif_read_data($filetoread);
590 if ($exif && !empty($exif[
'Orientation'])) {
591 $ort = $exif[
'Orientation'];
595 if ($maxWidth == -1) {
596 $maxWidth = $infoImg[0];
598 if ($maxHeight == -1) {
599 $maxHeight = $infoImg[1];
603 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
605 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
610 switch ($infoImg[2]) {
612 $imgfonction =
'imagecreatefromgif';
615 $imgfonction =
'imagecreatefromjpeg';
618 $imgfonction =
'imagecreatefrompng';
625 $imgfonction =
'imagecreatefromwbmp';
628 $imgfonction =
'imagecreatefromwebp';
631 $imgfonction =
'imagecreatefromavif';
637 if (!function_exists($imgfonction)) {
639 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
644 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
650 switch ($infoImg[2]) {
652 $img = imagecreatefromgif($filetoread);
656 $img = imagecreatefromjpeg($filetoread);
657 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
660 $img = imagecreatefrompng($filetoread);
668 $img = imagecreatefromwbmp($filetoread);
672 $img = imagecreatefromwebp($filetoread);
676 $img = imagecreatefromavif($filetoread);
683 if (is_null($img) || $img ===
false) {
684 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
697 $trueImgWidth = $infoImg[1];
698 $trueImgHeight = $infoImg[0];
703 $trueImgWidth = $infoImg[1];
704 $trueImgHeight = $infoImg[0];
712 if ($infoImg[2] === IMAGETYPE_PNG) {
713 imagealphablending($img,
false);
714 imagesavealpha($img,
true);
715 $rotated = imagerotate($img, $exifAngle, imagecolorallocatealpha($img, 0, 0, 0, 127));
716 imagealphablending($rotated,
false);
717 imagesavealpha($rotated,
true);
719 $rotated = imagerotate($img, $exifAngle, 0);
723 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
725 $imgWidth = $trueImgWidth;
726 $imgHeight = $trueImgHeight;
731 if ($maxWidth > $imgWidth) {
732 $maxWidth = $imgWidth;
734 if ($maxHeight > $imgHeight) {
735 $maxHeight = $imgHeight;
738 $whFact = $maxWidth / $maxHeight;
739 $imgWhFact = $imgWidth / $imgHeight;
742 if ($whFact < $imgWhFact) {
744 $thumbWidth = $maxWidth;
745 $thumbHeight = $thumbWidth / $imgWhFact;
748 $thumbHeight = $maxHeight;
749 $thumbWidth = $thumbHeight * $imgWhFact;
751 $thumbHeight = (int) round($thumbHeight);
752 $thumbWidth = (int) round($thumbWidth);
755 if (empty($targetformat)) {
756 $targetformat = $infoImg[2];
760 if ($targetformat == IMAGETYPE_GIF) {
762 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
764 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
768 if (function_exists(
'imageantialias')) {
769 imageantialias($imgThumb,
true);
773 if (function_exists(
'imagesavealpha')) {
774 imagesavealpha($imgThumb,
true);
780 $trans_colour =
false;
782 switch ($targetformat) {
784 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
785 imagecolortransparent($imgThumb, $trans_colour);
786 $extImgTarget =
'.gif';
790 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
791 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
792 $newquality = $quality;
795 imagealphablending($imgThumb,
false);
796 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
797 $extImgTarget =
'.png';
798 $newquality = round(abs($quality - 100) * 9 / 100);
802 $extImgTarget =
'.bmp';
806 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
807 $extImgTarget =
'.bmp';
811 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
812 $extImgTarget =
'.webp';
813 $newquality = $quality;
816 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
817 $extImgTarget =
'.avif';
818 $newquality = $quality;
821 if (function_exists(
"imagefill") && $trans_colour !==
false) {
822 imagefill($imgThumb, 0, 0, $trans_colour);
825 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
827 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
829 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp|\.avif)$/i',
'', $file);
830 $fileName = basename($fileName);
840 switch ($targetformat) {
842 imagegif($imgThumb, $imgThumbName);
845 imagejpeg($imgThumb, $imgThumbName, $newquality);
848 imagepng($imgThumb, $imgThumbName, !is_numeric($newquality) ? -1 : (int) $newquality);
854 imagewbmp($imgThumb, $imgThumbName);
857 imagewebp($imgThumb, $imgThumbName, $newquality);
860 imageavif($imgThumb, $imgThumbName, $newquality);
869 imagedestroy($imgThumb);
871 return $imgThumbName;