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;
293 switch ($infoImg[2]) {
295 $img = imagecreatefromgif($filetoread);
299 $img = imagecreatefromjpeg($filetoread);
303 $img = imagecreatefrompng($filetoread);
307 $img = imagecreatefromwbmp($filetoread);
311 $img = imagecreatefromwebp($filetoread);
317 if ($newExt ==
'gif') {
319 $imgTarget = imagecreate($newWidth, $newHeight);
321 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
325 if (function_exists(
'imageantialias')) {
326 imageantialias($imgTarget,
true);
330 if (function_exists(
'imagesavealpha')) {
331 imagesavealpha($imgTarget,
true);
338 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
339 imagecolortransparent($imgTarget, $trans_colour);
343 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
346 imagealphablending($imgTarget,
false);
347 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
350 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
353 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
356 if (function_exists(
"imagefill") && $trans_colour > 0) {
357 imagefill($imgTarget, 0, 0, $trans_colour);
360 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
362 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
372 imagegif($imgTarget, $imgTargetName);
376 $newquality = ($newquality ? $newquality :
'100');
377 imagejpeg($imgTarget, $imgTargetName, $newquality);
381 imagepng($imgTarget, $imgTargetName, $newquality);
385 imagewbmp($imgTarget, $imgTargetName);
388 $newquality = ($newquality ? $newquality :
'100');
389 imagewebp($imgTarget, $imgTargetName, $newquality);
392 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
400 imagedestroy($imgTarget);
404 return $imgTargetName;
432 if (function_exists(
'exif_read_data')) {
433 $exif = @exif_read_data($fileSource);
434 if ($exif && isset($exif[
'Orientation'])) {
435 $infoImg = getimagesize($fileSource);
437 $orientation = $exif[
'Orientation'];
438 if ($orientation != 1) {
439 $img = imagecreatefromjpeg($fileSource);
441 switch ($orientation) {
453 if ($infoImg[2] === IMAGETYPE_PNG) {
454 imagealphablending($img,
false);
455 imagesavealpha($img,
true);
456 $img = imagerotate($img, $deg, imagecolorallocatealpha($img, 0, 0, 0, 127));
457 imagealphablending($img,
false);
458 imagesavealpha($img,
true);
460 $img = imagerotate($img, $deg, 0);
464 if ($fileDest ===
false) {
471 switch ($infoImg[2]) {
473 $image = imagegif($img, $fileDest);
476 $image = imagejpeg($img, $fileDest, $quality);
479 $image = imagepng($img, $fileDest, $quality);
485 $image = imagewbmp($img, $fileDest);
514function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
516 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
520 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
528 return 'ErrorBadParameters';
529 } elseif (!file_exists($file)) {
531 dol_syslog($langs->trans(
"ErrorFileNotFound", $file), LOG_ERR);
532 return $langs->trans(
"ErrorFileNotFound", $file);
534 dol_syslog(
'This file '.$file.
' does not seem to be an image format file name.', LOG_WARNING);
535 return 'ErrorBadImageFormat';
536 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
538 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
539 return 'Error: Wrong value for parameter maxWidth';
540 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
542 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
543 return 'Error: Wrong value for parameter maxHeight';
548 $infoImg = getimagesize($filetoread);
549 $imgWidth = $infoImg[0];
550 $imgHeight = $infoImg[1];
553 if (function_exists(
'exif_read_data')) {
554 $exif = @exif_read_data($filetoread);
555 if ($exif && !empty($exif[
'Orientation'])) {
556 $ort = $exif[
'Orientation'];
560 if ($maxWidth == -1) {
561 $maxWidth = $infoImg[0];
563 if ($maxHeight == -1) {
564 $maxHeight = $infoImg[1];
568 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
570 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
575 switch ($infoImg[2]) {
577 $imgfonction =
'imagecreatefromgif';
580 $imgfonction =
'imagecreatefromjpeg';
583 $imgfonction =
'imagecreatefrompng';
589 $imgfonction =
'imagecreatefromwbmp';
592 $imgfonction =
'imagecreatefromwebp';
596 if (!function_exists($imgfonction)) {
598 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
603 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
608 switch ($infoImg[2]) {
610 $img = imagecreatefromgif($filetoread);
614 $img = imagecreatefromjpeg($filetoread);
615 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
618 $img = imagecreatefrompng($filetoread);
626 $img = imagecreatefromwbmp($filetoread);
630 $img = imagecreatefromwebp($filetoread);
637 if (is_null($img) || $img ===
false) {
638 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
651 $trueImgWidth = $infoImg[1];
652 $trueImgHeight = $infoImg[0];
657 $trueImgWidth = $infoImg[1];
658 $trueImgHeight = $infoImg[0];
666 if ($infoImg[2] === IMAGETYPE_PNG) {
667 imagealphablending($img,
false);
668 imagesavealpha($img,
true);
669 $rotated = imagerotate($img, $exifAngle, imagecolorallocatealpha($img, 0, 0, 0, 127));
670 imagealphablending($rotated,
false);
671 imagesavealpha($rotated,
true);
673 $rotated = imagerotate($img, $exifAngle, 0);
677 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
679 $imgWidth = $trueImgWidth;
680 $imgHeight = $trueImgHeight;
685 if ($maxWidth > $imgWidth) {
686 $maxWidth = $imgWidth;
688 if ($maxHeight > $imgHeight) {
689 $maxHeight = $imgHeight;
692 $whFact = $maxWidth / $maxHeight;
693 $imgWhFact = $imgWidth / $imgHeight;
696 if ($whFact < $imgWhFact) {
698 $thumbWidth = $maxWidth;
699 $thumbHeight = $thumbWidth / $imgWhFact;
702 $thumbHeight = $maxHeight;
703 $thumbWidth = $thumbHeight * $imgWhFact;
705 $thumbHeight = (int) round($thumbHeight);
706 $thumbWidth = (int) round($thumbWidth);
709 if (empty($targetformat)) {
710 $targetformat = $infoImg[2];
714 if ($targetformat == IMAGETYPE_GIF) {
716 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
718 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
722 if (function_exists(
'imageantialias')) {
723 imageantialias($imgThumb,
true);
727 if (function_exists(
'imagesavealpha')) {
728 imagesavealpha($imgThumb,
true);
733 switch ($targetformat) {
735 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
736 imagecolortransparent($imgThumb, $trans_colour);
737 $extImgTarget =
'.gif';
741 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
742 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
743 $newquality = $quality;
746 imagealphablending($imgThumb,
false);
747 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
748 $extImgTarget =
'.png';
749 $newquality = $quality - 100;
750 $newquality = round(abs($quality - 100) * 9 / 100);
754 $extImgTarget =
'.bmp';
758 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
759 $extImgTarget =
'.bmp';
763 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
764 $extImgTarget =
'.webp';
765 $newquality = $quality;
768 if (function_exists(
"imagefill")) {
769 imagefill($imgThumb, 0, 0, $trans_colour);
772 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
774 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
776 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
777 $fileName = basename($fileName);
787 switch ($targetformat) {
789 imagegif($imgThumb, $imgThumbName);
792 imagejpeg($imgThumb, $imgThumbName, $newquality);
795 imagepng($imgThumb, $imgThumbName, $newquality);
801 imagewbmp($imgThumb, $imgThumbName);
804 imagewebp($imgThumb, $imgThumbName, $newquality);
813 imagedestroy($imgThumb);
815 return $imgThumbName;