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';
190 global $conf, $langs;
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';
518 global $conf, $langs;
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);
636 if (!is_resource($img) && class_exists(
'GdImage') && !($img instanceof GdImage)) {
637 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
650 $trueImgWidth = $infoImg[1];
651 $trueImgHeight = $infoImg[0];
656 $trueImgWidth = $infoImg[1];
657 $trueImgHeight = $infoImg[0];
665 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
666 imagealphablending($img,
false);
667 imagesavealpha($img,
true);
668 $rotated = imagerotate($img, $exifAngle, imageColorAllocateAlpha($img, 0, 0, 0, 127));
669 imagealphablending($rotated,
false);
670 imagesavealpha($rotated,
true);
672 $rotated = imagerotate($img, $exifAngle, 0);
676 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
678 $imgWidth = $trueImgWidth;
679 $imgHeight = $trueImgHeight;
684 if ($maxWidth > $imgWidth) {
685 $maxWidth = $imgWidth;
687 if ($maxHeight > $imgHeight) {
688 $maxHeight = $imgHeight;
691 $whFact = $maxWidth / $maxHeight;
692 $imgWhFact = $imgWidth / $imgHeight;
695 if ($whFact < $imgWhFact) {
697 $thumbWidth = $maxWidth;
698 $thumbHeight = $thumbWidth / $imgWhFact;
701 $thumbHeight = $maxHeight;
702 $thumbWidth = $thumbHeight * $imgWhFact;
704 $thumbHeight = round($thumbHeight);
705 $thumbWidth = round($thumbWidth);
708 if (empty($targetformat)) {
709 $targetformat = $infoImg[2];
713 if ($targetformat == IMAGETYPE_GIF) {
715 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
717 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
721 if (function_exists(
'imageantialias')) {
722 imageantialias($imgThumb,
true);
726 if (function_exists(
'imagesavealpha')) {
727 imagesavealpha($imgThumb,
true);
732 switch ($targetformat) {
734 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
735 imagecolortransparent($imgThumb, $trans_colour);
736 $extImgTarget =
'.gif';
740 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
741 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
742 $newquality = $quality;
745 imagealphablending($imgThumb,
false);
746 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
747 $extImgTarget =
'.png';
748 $newquality = $quality - 100;
749 $newquality = round(abs($quality - 100) * 9 / 100);
753 $extImgTarget =
'.bmp';
757 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
758 $extImgTarget =
'.bmp';
762 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
763 $extImgTarget =
'.webp';
764 $newquality = $quality;
767 if (function_exists(
"imagefill")) {
768 imagefill($imgThumb, 0, 0, $trans_colour);
771 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
773 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
775 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
776 $fileName = basename($fileName);
786 switch ($targetformat) {
788 imagegif($imgThumb, $imgThumbName);
791 imagejpeg($imgThumb, $imgThumbName, $newquality);
794 imagepng($imgThumb, $imgThumbName, $newquality);
800 imagewbmp($imgThumb, $imgThumbName);
803 imagewebp($imgThumb, $imgThumbName, $newquality);
812 imagedestroy($imgThumb);
814 return $imgThumbName;