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 return $langs->trans(
"ErrorFileNotFound", $file);
208 return 'This filename '.$file.
' does not seem to be an image filename.';
209 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
210 return 'Wrong value for parameter newWidth or newHeight';
211 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
212 return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
213 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
214 return 'Both newHeight or newWidth must be defined for croping';
223 $infoImg = getimagesize($filetoread);
224 $imgWidth = $infoImg[0];
225 $imgHeight = $infoImg[1];
227 $imgTargetName = ($filetowrite ? $filetowrite : $file);
228 $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
231 if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
232 $newWidth = $imgWidth;
233 $newHeight = $imgHeight;
236 if ($newWidth <= 0) {
237 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth);
239 if ($newHeight <= 0) {
240 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight);
250 switch ($infoImg[2]) {
252 $imgfonction =
'imagecreatefromgif';
255 $imgfonction =
'imagecreatefromjpeg';
258 $imgfonction =
'imagecreatefrompng';
261 $imgfonction =
'imagecreatefromwbmp';
264 $imgfonction =
'imagecreatefromwebp';
267 $imgfonction =
'imagecreatefromavif';
271 if (!function_exists($imgfonction)) {
273 return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
282 $imgfonction =
'imagecreatefromgif';
286 $imgfonction =
'imagecreatefromjpeg';
289 $imgfonction =
'imagecreatefrompng';
292 $imgfonction =
'imagecreatefromwbmp';
295 $imgfonction =
'imagecreatefromwebp';
298 $imgfonction =
'imagecreatefromavif';
302 if (!function_exists($imgfonction)) {
304 return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
312 switch ($infoImg[2]) {
314 $img = imagecreatefromgif($filetoread);
318 $img = imagecreatefromjpeg($filetoread);
322 $img = imagecreatefrompng($filetoread);
326 $img = imagecreatefromwbmp($filetoread);
330 $img = imagecreatefromwebp($filetoread);
336 return "Error: Could not create Image from '$filetoread'";
340 if ($newExt ==
'gif') {
342 $imgTarget = imagecreate($newWidth, $newHeight);
344 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
348 if (function_exists(
'imageantialias')) {
349 imageantialias($imgTarget,
true);
353 if (function_exists(
'imagesavealpha')) {
354 imagesavealpha($imgTarget,
true);
361 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
362 imagecolortransparent($imgTarget, $trans_colour);
366 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
369 imagealphablending($imgTarget,
false);
370 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
373 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
376 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
379 if (function_exists(
"imagefill") && $trans_colour > 0) {
380 imagefill($imgTarget, 0, 0, $trans_colour);
383 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
385 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
395 imagegif($imgTarget, $imgTargetName);
399 $newquality = ($newquality ? $newquality :
'100');
400 imagejpeg($imgTarget, $imgTargetName, $newquality);
404 imagepng($imgTarget, $imgTargetName, $newquality);
408 imagewbmp($imgTarget, $imgTargetName);
411 $newquality = ($newquality ? $newquality :
'100');
412 imagewebp($imgTarget, $imgTargetName, $newquality);
415 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
426 imagedestroy($imgTarget);
431 return $imgTargetName;
459 if (function_exists(
'exif_read_data')) {
460 $exif = @exif_read_data($fileSource);
461 if ($exif && isset($exif[
'Orientation'])) {
462 $infoImg = getimagesize($fileSource);
464 $orientation = $exif[
'Orientation'];
465 if ($orientation != 1) {
466 $img = imagecreatefromjpeg($fileSource);
468 switch ($orientation) {
480 if ($infoImg[2] === IMAGETYPE_PNG) {
481 imagealphablending($img,
false);
482 imagesavealpha($img,
true);
483 $img = imagerotate($img, $deg, imagecolorallocatealpha($img, 0, 0, 0, 127));
484 imagealphablending($img,
false);
485 imagesavealpha($img,
true);
487 $img = imagerotate($img, $deg, 0);
491 if ($fileDest ===
false) {
498 switch ($infoImg[2]) {
500 $image = imagegif($img, $fileDest);
503 $image = imagejpeg($img, $fileDest, $quality);
506 $image = imagepng($img, $fileDest, $quality);
512 $image = imagewbmp($img, $fileDest);
542function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
544 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
548 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
556 return 'ErrorBadParameters';
558 dol_syslog(
'This file '.$file.
' does not seem to be a supported image file name (bad extension).', LOG_WARNING);
559 return 'ErrorBadImageFormat';
560 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
562 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
563 return 'Error: Wrong value for parameter maxWidth';
564 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
566 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
567 return 'Error: Wrong value for parameter maxHeight';
572 if (!file_exists($filetoread)) {
574 dol_syslog($langs->trans(
"ErrorFileNotFound", $filetoread), LOG_ERR);
575 return $langs->trans(
"ErrorFileNotFound", $filetoread);
578 $infoImg = getimagesize($filetoread);
579 $imgWidth = $infoImg[0];
580 $imgHeight = $infoImg[1];
586 if (function_exists(
'exif_read_data')) {
587 $exif = @exif_read_data($filetoread);
588 if ($exif && !empty($exif[
'Orientation'])) {
589 $ort = $exif[
'Orientation'];
593 if ($maxWidth == -1) {
594 $maxWidth = $infoImg[0];
596 if ($maxHeight == -1) {
597 $maxHeight = $infoImg[1];
601 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
603 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
608 switch ($infoImg[2]) {
610 $imgfonction =
'imagecreatefromgif';
613 $imgfonction =
'imagecreatefromjpeg';
616 $imgfonction =
'imagecreatefrompng';
623 $imgfonction =
'imagecreatefromwbmp';
626 $imgfonction =
'imagecreatefromwebp';
629 $imgfonction =
'imagecreatefromavif';
635 if (!function_exists($imgfonction)) {
637 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
642 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
648 switch ($infoImg[2]) {
650 $img = imagecreatefromgif($filetoread);
654 $img = imagecreatefromjpeg($filetoread);
655 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
658 $img = imagecreatefrompng($filetoread);
666 $img = imagecreatefromwbmp($filetoread);
670 $img = imagecreatefromwebp($filetoread);
674 $img = imagecreatefromavif($filetoread);
681 if (is_null($img) || $img ===
false) {
682 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
695 $trueImgWidth = $infoImg[1];
696 $trueImgHeight = $infoImg[0];
701 $trueImgWidth = $infoImg[1];
702 $trueImgHeight = $infoImg[0];
710 if ($infoImg[2] === IMAGETYPE_PNG) {
711 imagealphablending($img,
false);
712 imagesavealpha($img,
true);
713 $rotated = imagerotate($img, $exifAngle, imagecolorallocatealpha($img, 0, 0, 0, 127));
714 imagealphablending($rotated,
false);
715 imagesavealpha($rotated,
true);
717 $rotated = imagerotate($img, $exifAngle, 0);
721 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
723 $imgWidth = $trueImgWidth;
724 $imgHeight = $trueImgHeight;
729 if ($maxWidth > $imgWidth) {
730 $maxWidth = $imgWidth;
732 if ($maxHeight > $imgHeight) {
733 $maxHeight = $imgHeight;
736 $whFact = $maxWidth / $maxHeight;
737 $imgWhFact = $imgWidth / $imgHeight;
740 if ($whFact < $imgWhFact) {
742 $thumbWidth = $maxWidth;
743 $thumbHeight = $thumbWidth / $imgWhFact;
746 $thumbHeight = $maxHeight;
747 $thumbWidth = $thumbHeight * $imgWhFact;
749 $thumbHeight = (int) round($thumbHeight);
750 $thumbWidth = (int) round($thumbWidth);
753 if (empty($targetformat)) {
754 $targetformat = $infoImg[2];
758 if ($targetformat == IMAGETYPE_GIF) {
760 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
762 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
766 if (function_exists(
'imageantialias')) {
767 imageantialias($imgThumb,
true);
771 if (function_exists(
'imagesavealpha')) {
772 imagesavealpha($imgThumb,
true);
778 $trans_colour =
false;
780 switch ($targetformat) {
782 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
783 imagecolortransparent($imgThumb, $trans_colour);
784 $extImgTarget =
'.gif';
788 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
789 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
790 $newquality = $quality;
793 imagealphablending($imgThumb,
false);
794 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
795 $extImgTarget =
'.png';
796 $newquality = round(abs($quality - 100) * 9 / 100);
800 $extImgTarget =
'.bmp';
804 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
805 $extImgTarget =
'.bmp';
809 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
810 $extImgTarget =
'.webp';
811 $newquality = $quality;
814 if (function_exists(
"imagefill") && $trans_colour !==
false) {
815 imagefill($imgThumb, 0, 0, $trans_colour);
818 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
820 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
822 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp|\.avif)$/i',
'', $file);
823 $fileName = basename($fileName);
825 $imgThumbName = getImageFileNameForSize($file, $extName, $extImgTarget);
833 switch ($targetformat) {
835 imagegif($imgThumb, $imgThumbName);
838 imagejpeg($imgThumb, $imgThumbName, $newquality);
841 imagepng($imgThumb, $imgThumbName, !is_numeric($newquality) ? -1 : (int) $newquality);
847 imagewbmp($imgThumb, $imgThumbName);
850 imagewebp($imgThumb, $imgThumbName, $newquality);
853 imageavif($imgThumb, $imgThumbName, $newquality);
862 imagedestroy($imgThumb);
864 return $imgThumbName;