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 return "Error: Could not create Image from '$filetoread'";
323 if ($newExt ==
'gif') {
325 $imgTarget = imagecreate($newWidth, $newHeight);
327 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
331 if (function_exists(
'imageantialias')) {
332 imageantialias($imgTarget,
true);
336 if (function_exists(
'imagesavealpha')) {
337 imagesavealpha($imgTarget,
true);
344 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
345 imagecolortransparent($imgTarget, $trans_colour);
349 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
352 imagealphablending($imgTarget,
false);
353 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
356 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
359 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
362 if (function_exists(
"imagefill") && $trans_colour > 0) {
363 imagefill($imgTarget, 0, 0, $trans_colour);
366 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
368 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
378 imagegif($imgTarget, $imgTargetName);
382 $newquality = ($newquality ? $newquality :
'100');
383 imagejpeg($imgTarget, $imgTargetName, $newquality);
387 imagepng($imgTarget, $imgTargetName, $newquality);
391 imagewbmp($imgTarget, $imgTargetName);
394 $newquality = ($newquality ? $newquality :
'100');
395 imagewebp($imgTarget, $imgTargetName, $newquality);
398 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
409 imagedestroy($imgTarget);
414 return $imgTargetName;
442 if (function_exists(
'exif_read_data')) {
443 $exif = @exif_read_data($fileSource);
444 if ($exif && isset($exif[
'Orientation'])) {
445 $infoImg = getimagesize($fileSource);
447 $orientation = $exif[
'Orientation'];
448 if ($orientation != 1) {
449 $img = imagecreatefromjpeg($fileSource);
451 switch ($orientation) {
463 if ($infoImg[2] === IMAGETYPE_PNG) {
464 imagealphablending($img,
false);
465 imagesavealpha($img,
true);
466 $img = imagerotate($img, $deg, imagecolorallocatealpha($img, 0, 0, 0, 127));
467 imagealphablending($img,
false);
468 imagesavealpha($img,
true);
470 $img = imagerotate($img, $deg, 0);
474 if ($fileDest ===
false) {
481 switch ($infoImg[2]) {
483 $image = imagegif($img, $fileDest);
486 $image = imagejpeg($img, $fileDest, $quality);
489 $image = imagepng($img, $fileDest, $quality);
495 $image = imagewbmp($img, $fileDest);
525function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
527 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
531 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
539 return 'ErrorBadParameters';
541 dol_syslog(
'This file '.$file.
' does not seem to be a supported image file name (bad extension).', LOG_WARNING);
542 return 'ErrorBadImageFormat';
543 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
545 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
546 return 'Error: Wrong value for parameter maxWidth';
547 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
549 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
550 return 'Error: Wrong value for parameter maxHeight';
555 if (!file_exists($filetoread)) {
557 dol_syslog($langs->trans(
"ErrorFileNotFound", $filetoread), LOG_ERR);
558 return $langs->trans(
"ErrorFileNotFound", $filetoread);
561 $infoImg = getimagesize($filetoread);
562 $imgWidth = $infoImg[0];
563 $imgHeight = $infoImg[1];
569 if (function_exists(
'exif_read_data')) {
570 $exif = @exif_read_data($filetoread);
571 if ($exif && !empty($exif[
'Orientation'])) {
572 $ort = $exif[
'Orientation'];
576 if ($maxWidth == -1) {
577 $maxWidth = $infoImg[0];
579 if ($maxHeight == -1) {
580 $maxHeight = $infoImg[1];
584 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
586 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
591 switch ($infoImg[2]) {
593 $imgfonction =
'imagecreatefromgif';
596 $imgfonction =
'imagecreatefromjpeg';
599 $imgfonction =
'imagecreatefrompng';
605 $imgfonction =
'imagecreatefromwbmp';
608 $imgfonction =
'imagecreatefromwebp';
612 if (!function_exists($imgfonction)) {
614 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
619 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
625 switch ($infoImg[2]) {
627 $img = imagecreatefromgif($filetoread);
631 $img = imagecreatefromjpeg($filetoread);
632 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
635 $img = imagecreatefrompng($filetoread);
643 $img = imagecreatefromwbmp($filetoread);
647 $img = imagecreatefromwebp($filetoread);
654 if (is_null($img) || $img ===
false) {
655 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
668 $trueImgWidth = $infoImg[1];
669 $trueImgHeight = $infoImg[0];
674 $trueImgWidth = $infoImg[1];
675 $trueImgHeight = $infoImg[0];
683 if ($infoImg[2] === IMAGETYPE_PNG) {
684 imagealphablending($img,
false);
685 imagesavealpha($img,
true);
686 $rotated = imagerotate($img, $exifAngle, imagecolorallocatealpha($img, 0, 0, 0, 127));
687 imagealphablending($rotated,
false);
688 imagesavealpha($rotated,
true);
690 $rotated = imagerotate($img, $exifAngle, 0);
694 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
696 $imgWidth = $trueImgWidth;
697 $imgHeight = $trueImgHeight;
702 if ($maxWidth > $imgWidth) {
703 $maxWidth = $imgWidth;
705 if ($maxHeight > $imgHeight) {
706 $maxHeight = $imgHeight;
709 $whFact = $maxWidth / $maxHeight;
710 $imgWhFact = $imgWidth / $imgHeight;
713 if ($whFact < $imgWhFact) {
715 $thumbWidth = $maxWidth;
716 $thumbHeight = $thumbWidth / $imgWhFact;
719 $thumbHeight = $maxHeight;
720 $thumbWidth = $thumbHeight * $imgWhFact;
722 $thumbHeight = (int) round($thumbHeight);
723 $thumbWidth = (int) round($thumbWidth);
726 if (empty($targetformat)) {
727 $targetformat = $infoImg[2];
731 if ($targetformat == IMAGETYPE_GIF) {
733 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
735 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
739 if (function_exists(
'imageantialias')) {
740 imageantialias($imgThumb,
true);
744 if (function_exists(
'imagesavealpha')) {
745 imagesavealpha($imgThumb,
true);
751 $trans_colour =
false;
753 switch ($targetformat) {
755 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
756 imagecolortransparent($imgThumb, $trans_colour);
757 $extImgTarget =
'.gif';
761 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
762 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
763 $newquality = $quality;
766 imagealphablending($imgThumb,
false);
767 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
768 $extImgTarget =
'.png';
769 $newquality = round(abs($quality - 100) * 9 / 100);
773 $extImgTarget =
'.bmp';
777 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
778 $extImgTarget =
'.bmp';
782 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
783 $extImgTarget =
'.webp';
784 $newquality = $quality;
787 if (function_exists(
"imagefill") && $trans_colour !==
false) {
788 imagefill($imgThumb, 0, 0, $trans_colour);
791 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
793 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
795 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
796 $fileName = basename($fileName);
806 switch ($targetformat) {
808 imagegif($imgThumb, $imgThumbName);
811 imagejpeg($imgThumb, $imgThumbName, $newquality);
814 imagepng($imgThumb, $imgThumbName, !is_numeric($newquality) ? -1 : (int) $newquality);
820 imagewbmp($imgThumb, $imgThumbName);
823 imagewebp($imgThumb, $imgThumbName, $newquality);
832 imagedestroy($imgThumb);
834 return $imgThumbName;