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);
521function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
523 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
527 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
535 return 'ErrorBadParameters';
537 dol_syslog(
'This file '.$file.
' does not seem to be a supported image file name (bad extension).', LOG_WARNING);
538 return 'ErrorBadImageFormat';
539 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
541 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
542 return 'Error: Wrong value for parameter maxWidth';
543 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
545 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
546 return 'Error: Wrong value for parameter maxHeight';
551 if (!file_exists($filetoread)) {
553 dol_syslog($langs->trans(
"ErrorFileNotFound", $filetoread), LOG_ERR);
554 return $langs->trans(
"ErrorFileNotFound", $filetoread);
557 $infoImg = getimagesize($filetoread);
558 $imgWidth = $infoImg[0];
559 $imgHeight = $infoImg[1];
562 if (function_exists(
'exif_read_data')) {
563 $exif = @exif_read_data($filetoread);
564 if ($exif && !empty($exif[
'Orientation'])) {
565 $ort = $exif[
'Orientation'];
569 if ($maxWidth == -1) {
570 $maxWidth = $infoImg[0];
572 if ($maxHeight == -1) {
573 $maxHeight = $infoImg[1];
577 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
579 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
584 switch ($infoImg[2]) {
586 $imgfonction =
'imagecreatefromgif';
589 $imgfonction =
'imagecreatefromjpeg';
592 $imgfonction =
'imagecreatefrompng';
598 $imgfonction =
'imagecreatefromwbmp';
601 $imgfonction =
'imagecreatefromwebp';
605 if (!function_exists($imgfonction)) {
607 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
612 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
617 switch ($infoImg[2]) {
619 $img = imagecreatefromgif($filetoread);
623 $img = imagecreatefromjpeg($filetoread);
624 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
627 $img = imagecreatefrompng($filetoread);
635 $img = imagecreatefromwbmp($filetoread);
639 $img = imagecreatefromwebp($filetoread);
646 if (is_null($img) || $img ===
false) {
647 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
660 $trueImgWidth = $infoImg[1];
661 $trueImgHeight = $infoImg[0];
666 $trueImgWidth = $infoImg[1];
667 $trueImgHeight = $infoImg[0];
675 if ($infoImg[2] === IMAGETYPE_PNG) {
676 imagealphablending($img,
false);
677 imagesavealpha($img,
true);
678 $rotated = imagerotate($img, $exifAngle, imagecolorallocatealpha($img, 0, 0, 0, 127));
679 imagealphablending($rotated,
false);
680 imagesavealpha($rotated,
true);
682 $rotated = imagerotate($img, $exifAngle, 0);
686 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
688 $imgWidth = $trueImgWidth;
689 $imgHeight = $trueImgHeight;
694 if ($maxWidth > $imgWidth) {
695 $maxWidth = $imgWidth;
697 if ($maxHeight > $imgHeight) {
698 $maxHeight = $imgHeight;
701 $whFact = $maxWidth / $maxHeight;
702 $imgWhFact = $imgWidth / $imgHeight;
705 if ($whFact < $imgWhFact) {
707 $thumbWidth = $maxWidth;
708 $thumbHeight = $thumbWidth / $imgWhFact;
711 $thumbHeight = $maxHeight;
712 $thumbWidth = $thumbHeight * $imgWhFact;
714 $thumbHeight = (int) round($thumbHeight);
715 $thumbWidth = (int) round($thumbWidth);
718 if (empty($targetformat)) {
719 $targetformat = $infoImg[2];
723 if ($targetformat == IMAGETYPE_GIF) {
725 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
727 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
731 if (function_exists(
'imageantialias')) {
732 imageantialias($imgThumb,
true);
736 if (function_exists(
'imagesavealpha')) {
737 imagesavealpha($imgThumb,
true);
742 $extImgTarget =
null;
743 $trans_colour =
false;
745 switch ($targetformat) {
747 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
748 imagecolortransparent($imgThumb, $trans_colour);
749 $extImgTarget =
'.gif';
753 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
754 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
755 $newquality = $quality;
758 imagealphablending($imgThumb,
false);
759 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
760 $extImgTarget =
'.png';
761 $newquality = round(abs($quality - 100) * 9 / 100);
765 $extImgTarget =
'.bmp';
769 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
770 $extImgTarget =
'.bmp';
774 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
775 $extImgTarget =
'.webp';
776 $newquality = $quality;
779 if (function_exists(
"imagefill") && $trans_colour !==
false) {
780 imagefill($imgThumb, 0, 0, $trans_colour);
783 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
785 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
787 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
788 $fileName = basename($fileName);
798 switch ($targetformat) {
800 imagegif($imgThumb, $imgThumbName);
803 imagejpeg($imgThumb, $imgThumbName, $newquality);
806 imagepng($imgThumb, $imgThumbName, $newquality);
812 imagewbmp($imgThumb, $imgThumbName);
815 imagewebp($imgThumb, $imgThumbName, $newquality);
824 imagedestroy($imgThumb);
826 return $imgThumbName;