186function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite =
'', $newquality = 0)
188 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
192 dol_syslog(
"dol_imageResizeOrCrop file=".$file.
" mode=".$mode.
" newWidth=".$newWidth.
" newHeight=".$newHeight.
" src_x=".$src_x.
" src_y=".$src_y);
200 return 'Bad parameter file';
201 } elseif (!file_exists($file)) {
203 return $langs->trans(
"ErrorFileNotFound", $file);
205 return 'This filename '.$file.
' does not seem to be an image filename.';
206 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
207 return 'Wrong value for parameter newWidth or newHeight';
208 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
209 return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
210 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
211 return 'Both newHeight or newWidth must be defined for croping';
216 $infoImg = getimagesize($filetoread);
217 $imgWidth = $infoImg[0];
218 $imgHeight = $infoImg[1];
220 $imgTargetName = ($filetowrite ? $filetowrite : $file);
221 $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
224 if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
225 $newWidth = $imgWidth;
226 $newHeight = $imgHeight;
229 if ($newWidth <= 0) {
230 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth);
232 if ($newHeight <= 0) {
233 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight);
239 switch ($infoImg[2]) {
241 $imgfonction =
'imagecreatefromgif';
244 $imgfonction =
'imagecreatefromjpeg';
247 $imgfonction =
'imagecreatefrompng';
250 $imgfonction =
'imagecreatefromwbmp';
253 $imgfonction =
'imagecreatefromwebp';
257 if (!function_exists($imgfonction)) {
259 return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
268 $imgfonction =
'imagecreatefromgif';
272 $imgfonction =
'imagecreatefromjpeg';
275 $imgfonction =
'imagecreatefrompng';
278 $imgfonction =
'imagecreatefromwbmp';
281 $imgfonction =
'imagecreatefromwebp';
285 if (!function_exists($imgfonction)) {
287 return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
295 switch ($infoImg[2]) {
297 $img = imagecreatefromgif($filetoread);
301 $img = imagecreatefromjpeg($filetoread);
305 $img = imagecreatefrompng($filetoread);
309 $img = imagecreatefromwbmp($filetoread);
313 $img = imagecreatefromwebp($filetoread);
319 if ($newExt ==
'gif') {
321 $imgTarget = imagecreate($newWidth, $newHeight);
323 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
327 if (function_exists(
'imageantialias')) {
328 imageantialias($imgTarget,
true);
332 if (function_exists(
'imagesavealpha')) {
333 imagesavealpha($imgTarget,
true);
340 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
341 imagecolortransparent($imgTarget, $trans_colour);
345 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
348 imagealphablending($imgTarget,
false);
349 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
352 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
355 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
358 if (function_exists(
"imagefill") && $trans_colour > 0) {
359 imagefill($imgTarget, 0, 0, $trans_colour);
362 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
364 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
374 imagegif($imgTarget, $imgTargetName);
378 $newquality = ($newquality ? $newquality :
'100');
379 imagejpeg($imgTarget, $imgTargetName, $newquality);
383 imagepng($imgTarget, $imgTargetName, $newquality);
387 imagewbmp($imgTarget, $imgTargetName);
390 $newquality = ($newquality ? $newquality :
'100');
391 imagewebp($imgTarget, $imgTargetName, $newquality);
394 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
405 imagedestroy($imgTarget);
410 return $imgTargetName;
438 if (function_exists(
'exif_read_data')) {
439 $exif = @exif_read_data($fileSource);
440 if ($exif && isset($exif[
'Orientation'])) {
441 $infoImg = getimagesize($fileSource);
443 $orientation = $exif[
'Orientation'];
444 if ($orientation != 1) {
445 $img = imagecreatefromjpeg($fileSource);
447 switch ($orientation) {
459 if ($infoImg[2] === IMAGETYPE_PNG) {
460 imagealphablending($img,
false);
461 imagesavealpha($img,
true);
462 $img = imagerotate($img, $deg, imagecolorallocatealpha($img, 0, 0, 0, 127));
463 imagealphablending($img,
false);
464 imagesavealpha($img,
true);
466 $img = imagerotate($img, $deg, 0);
470 if ($fileDest ===
false) {
477 switch ($infoImg[2]) {
479 $image = imagegif($img, $fileDest);
482 $image = imagejpeg($img, $fileDest, $quality);
485 $image = imagepng($img, $fileDest, $quality);
491 $image = imagewbmp($img, $fileDest);
520function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
522 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
526 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
534 return 'ErrorBadParameters';
535 } elseif (!file_exists($file)) {
537 dol_syslog($langs->trans(
"ErrorFileNotFound", $file), LOG_ERR);
538 return $langs->trans(
"ErrorFileNotFound", $file);
540 dol_syslog(
'This file '.$file.
' does not seem to be an image format file name.', LOG_WARNING);
541 return 'ErrorBadImageFormat';
542 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
544 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
545 return 'Error: Wrong value for parameter maxWidth';
546 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
548 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
549 return 'Error: Wrong value for parameter maxHeight';
554 $infoImg = getimagesize($filetoread);
555 $imgWidth = $infoImg[0];
556 $imgHeight = $infoImg[1];
559 if (function_exists(
'exif_read_data')) {
560 $exif = @exif_read_data($filetoread);
561 if ($exif && !empty($exif[
'Orientation'])) {
562 $ort = $exif[
'Orientation'];
566 if ($maxWidth == -1) {
567 $maxWidth = $infoImg[0];
569 if ($maxHeight == -1) {
570 $maxHeight = $infoImg[1];
574 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
576 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
581 switch ($infoImg[2]) {
583 $imgfonction =
'imagecreatefromgif';
586 $imgfonction =
'imagecreatefromjpeg';
589 $imgfonction =
'imagecreatefrompng';
595 $imgfonction =
'imagecreatefromwbmp';
598 $imgfonction =
'imagecreatefromwebp';
602 if (!function_exists($imgfonction)) {
604 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
609 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
614 switch ($infoImg[2]) {
616 $img = imagecreatefromgif($filetoread);
620 $img = imagecreatefromjpeg($filetoread);
621 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
624 $img = imagecreatefrompng($filetoread);
632 $img = imagecreatefromwbmp($filetoread);
636 $img = imagecreatefromwebp($filetoread);
643 if (is_null($img) || $img ===
false) {
644 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
657 $trueImgWidth = $infoImg[1];
658 $trueImgHeight = $infoImg[0];
663 $trueImgWidth = $infoImg[1];
664 $trueImgHeight = $infoImg[0];
672 if ($infoImg[2] === IMAGETYPE_PNG) {
673 imagealphablending($img,
false);
674 imagesavealpha($img,
true);
675 $rotated = imagerotate($img, $exifAngle, imagecolorallocatealpha($img, 0, 0, 0, 127));
676 imagealphablending($rotated,
false);
677 imagesavealpha($rotated,
true);
679 $rotated = imagerotate($img, $exifAngle, 0);
683 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
685 $imgWidth = $trueImgWidth;
686 $imgHeight = $trueImgHeight;
691 if ($maxWidth > $imgWidth) {
692 $maxWidth = $imgWidth;
694 if ($maxHeight > $imgHeight) {
695 $maxHeight = $imgHeight;
698 $whFact = $maxWidth / $maxHeight;
699 $imgWhFact = $imgWidth / $imgHeight;
702 if ($whFact < $imgWhFact) {
704 $thumbWidth = $maxWidth;
705 $thumbHeight = $thumbWidth / $imgWhFact;
708 $thumbHeight = $maxHeight;
709 $thumbWidth = $thumbHeight * $imgWhFact;
711 $thumbHeight = (int) round($thumbHeight);
712 $thumbWidth = (int) round($thumbWidth);
715 if (empty($targetformat)) {
716 $targetformat = $infoImg[2];
720 if ($targetformat == IMAGETYPE_GIF) {
722 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
724 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
728 if (function_exists(
'imageantialias')) {
729 imageantialias($imgThumb,
true);
733 if (function_exists(
'imagesavealpha')) {
734 imagesavealpha($imgThumb,
true);
739 $extImgTarget =
null;
740 $trans_colour =
false;
742 switch ($targetformat) {
744 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
745 imagecolortransparent($imgThumb, $trans_colour);
746 $extImgTarget =
'.gif';
750 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
751 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
752 $newquality = $quality;
755 imagealphablending($imgThumb,
false);
756 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
757 $extImgTarget =
'.png';
758 $newquality = $quality - 100;
759 $newquality = round(abs($quality - 100) * 9 / 100);
763 $extImgTarget =
'.bmp';
767 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
768 $extImgTarget =
'.bmp';
772 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
773 $extImgTarget =
'.webp';
774 $newquality = $quality;
777 if (function_exists(
"imagefill") && $trans_colour !==
false) {
778 imagefill($imgThumb, 0, 0, $trans_colour);
781 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
783 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
785 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
786 $fileName = basename($fileName);
796 switch ($targetformat) {
798 imagegif($imgThumb, $imgThumbName);
801 imagejpeg($imgThumb, $imgThumbName, $newquality);
804 imagepng($imgThumb, $imgThumbName, $newquality);
810 imagewbmp($imgThumb, $imgThumbName);
813 imagewebp($imgThumb, $imgThumbName, $newquality);
822 imagedestroy($imgThumb);
824 return $imgThumbName;