181function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite =
'', $newquality = 0)
183 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
185 global $conf, $langs;
187 dol_syslog(
"dol_imageResizeOrCrop file=".$file.
" mode=".$mode.
" newWidth=".$newWidth.
" newHeight=".$newHeight.
" src_x=".$src_x.
" src_y=".$src_y);
195 return 'Bad parameter file';
196 } elseif (!file_exists($file)) {
198 return $langs->trans(
"ErrorFileNotFound", $file);
200 return 'This filename '.$file.
' does not seem to be an image filename.';
201 } elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
202 return 'Wrong value for parameter newWidth or newHeight';
203 } elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
204 return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
205 } elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
206 return 'Both newHeight or newWidth must be defined for croping';
211 $infoImg = getimagesize($filetoread);
212 $imgWidth = $infoImg[0];
213 $imgHeight = $infoImg[1];
215 $imgTargetName = ($filetowrite ? $filetowrite : $file);
216 $newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
219 if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
220 $newWidth = $imgWidth;
221 $newHeight = $imgHeight;
224 if ($newWidth <= 0) {
225 $newWidth = intval(($newHeight / $imgHeight) * $imgWidth);
227 if ($newHeight <= 0) {
228 $newHeight = intval(($newWidth / $imgWidth) * $imgHeight);
234 switch ($infoImg[2]) {
236 $imgfonction =
'imagecreatefromgif';
239 $imgfonction =
'imagecreatefromjpeg';
242 $imgfonction =
'imagecreatefrompng';
245 $imgfonction =
'imagecreatefromwbmp';
248 $imgfonction =
'imagecreatefromwebp';
252 if (!function_exists($imgfonction)) {
254 return 'Read of image not possible. This PHP does not support GD functions '.$imgfonction;
263 $imgfonction =
'imagecreatefromgif';
267 $imgfonction =
'imagecreatefromjpeg';
270 $imgfonction =
'imagecreatefrompng';
273 $imgfonction =
'imagecreatefromwbmp';
276 $imgfonction =
'imagecreatefromwebp';
280 if (!function_exists($imgfonction)) {
282 return 'Write of image not possible. This PHP does not support GD functions '.$imgfonction;
288 switch ($infoImg[2]) {
290 $img = imagecreatefromgif($filetoread);
294 $img = imagecreatefromjpeg($filetoread);
298 $img = imagecreatefrompng($filetoread);
302 $img = imagecreatefromwbmp($filetoread);
306 $img = imagecreatefromwebp($filetoread);
312 if ($newExt ==
'gif') {
314 $imgTarget = imagecreate($newWidth, $newHeight);
316 $imgTarget = imagecreatetruecolor($newWidth, $newHeight);
320 if (function_exists(
'imageantialias')) {
321 imageantialias($imgTarget,
true);
325 if (function_exists(
'imagesavealpha')) {
326 imagesavealpha($imgTarget,
true);
333 $trans_colour = imagecolorallocate($imgTarget, 255, 255, 255);
334 imagecolortransparent($imgTarget, $trans_colour);
338 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
341 imagealphablending($imgTarget,
false);
342 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
345 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 0);
348 $trans_colour = imagecolorallocatealpha($imgTarget, 255, 255, 255, 127);
351 if (function_exists(
"imagefill") && $trans_colour > 0) {
352 imagefill($imgTarget, 0, 0, $trans_colour);
355 dol_syslog(
"dol_imageResizeOrCrop: convert image from ($imgWidth x $imgHeight) at position ($src_x x $src_y) to ($newWidth x $newHeight) as a $extImg");
357 imagecopyresampled($imgTarget, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight));
367 imagegif($imgTarget, $imgTargetName);
371 $newquality = ($newquality ? $newquality :
'100');
372 imagejpeg($imgTarget, $imgTargetName, $newquality);
376 imagepng($imgTarget, $imgTargetName, $newquality);
380 imagewbmp($imgTarget, $imgTargetName);
383 $newquality = ($newquality ? $newquality :
'100');
384 imagewebp($imgTarget, $imgTargetName, $newquality);
387 dol_syslog(
"images.lib.php::imageResizeOrCrop() Format ".$newExt.
" is not supported", LOG_WARNING);
395 imagedestroy($imgTarget);
399 return $imgTargetName;
427 if (function_exists(
'exif_read_data')) {
428 $exif = @exif_read_data($fileSource);
429 if ($exif && isset($exif[
'Orientation'])) {
430 $infoImg = getimagesize($fileSource);
432 $orientation = $exif[
'Orientation'];
433 if ($orientation != 1) {
434 $img = imagecreatefromjpeg($fileSource);
436 switch ($orientation) {
448 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
449 imagealphablending($img,
false);
450 imagesavealpha($img,
true);
451 $img = imagerotate($img, $deg, imageColorAllocateAlpha($img, 0, 0, 0, 127));
452 imagealphablending($img,
false);
453 imagesavealpha($img,
true);
455 $img = imagerotate($img, $deg, 0);
459 if ($fileDest ===
false) {
466 switch ($infoImg[2]) {
468 $image = imagegif($img, $fileDest);
471 $image = imagejpeg($img, $fileDest, $quality);
474 $image = imagepng($img, $fileDest, $quality);
480 $image = imagewbmp($img, $fileDest);
509function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName =
'_small', $quality = 50, $outdir =
'thumbs', $targetformat = 0)
511 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
513 global $conf, $langs;
515 dol_syslog(
"vignette file=".$file.
" extName=".$extName.
" maxWidth=".$maxWidth.
" maxHeight=".$maxHeight.
" quality=".$quality.
" outdir=".$outdir.
" targetformat=".$targetformat);
523 return 'ErrorBadParameters';
524 } elseif (!file_exists($file)) {
526 dol_syslog($langs->trans(
"ErrorFileNotFound", $file), LOG_ERR);
527 return $langs->trans(
"ErrorFileNotFound", $file);
529 dol_syslog(
'This file '.$file.
' does not seem to be an image format file name.', LOG_WARNING);
530 return 'ErrorBadImageFormat';
531 } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
533 dol_syslog(
'Wrong value for parameter maxWidth', LOG_ERR);
534 return 'Error: Wrong value for parameter maxWidth';
535 } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
537 dol_syslog(
'Wrong value for parameter maxHeight', LOG_ERR);
538 return 'Error: Wrong value for parameter maxHeight';
543 $infoImg = getimagesize($filetoread);
544 $imgWidth = $infoImg[0];
545 $imgHeight = $infoImg[1];
548 if (function_exists(
'exif_read_data')) {
549 $exif = @exif_read_data($filetoread);
550 if ($exif && !empty($exif[
'Orientation'])) {
551 $ort = $exif[
'Orientation'];
555 if ($maxWidth == -1) {
556 $maxWidth = $infoImg[0];
558 if ($maxHeight == -1) {
559 $maxHeight = $infoImg[1];
563 if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
565 dol_syslog(
"File size is smaller than thumb size", LOG_DEBUG);
570 switch ($infoImg[2]) {
572 $imgfonction =
'imagecreatefromgif';
575 $imgfonction =
'imagecreatefromjpeg';
578 $imgfonction =
'imagecreatefrompng';
584 $imgfonction =
'imagecreatefromwbmp';
588 if (!function_exists($imgfonction)) {
590 return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
595 $dirthumb = dirname($file).($outdir ?
'/'.$outdir :
'');
600 switch ($infoImg[2]) {
602 $img = imagecreatefromgif($filetoread);
606 $img = imagecreatefromjpeg($filetoread);
607 $extImg = (preg_match(
'/\.jpeg$/', $file) ?
'.jpeg' :
'.jpg');
610 $img = imagecreatefrompng($filetoread);
618 $img = imagecreatefromwbmp($filetoread);
624 if (!is_resource($img) && !($img instanceof GdImage)) {
625 dol_syslog(
'Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING);
630 if ($ort && !empty($conf->global->MAIN_USE_EXIF_ROTATION)) {
638 $trueImgWidth = $infoImg[1];
639 $trueImgHeight = $infoImg[0];
644 $trueImgWidth = $infoImg[1];
645 $trueImgHeight = $infoImg[0];
653 if ($infoImg[2] ===
'IMAGETYPE_PNG') {
654 imagealphablending($img,
false);
655 imagesavealpha($img,
true);
656 $rotated = imagerotate($img, $exifAngle, imageColorAllocateAlpha($img, 0, 0, 0, 127));
657 imagealphablending($rotated,
false);
658 imagesavealpha($rotated,
true);
660 $rotated = imagerotate($img, $exifAngle, 0);
664 if (!empty($rotated) && isset($trueImgWidth) && isset($trueImgHeight)) {
666 $imgWidth = $trueImgWidth;
667 $imgHeight = $trueImgHeight;
672 if ($maxWidth > $imgWidth) {
673 $maxWidth = $imgWidth;
675 if ($maxHeight > $imgHeight) {
676 $maxHeight = $imgHeight;
679 $whFact = $maxWidth / $maxHeight;
680 $imgWhFact = $imgWidth / $imgHeight;
683 if ($whFact < $imgWhFact) {
685 $thumbWidth = $maxWidth;
686 $thumbHeight = $thumbWidth / $imgWhFact;
689 $thumbHeight = $maxHeight;
690 $thumbWidth = $thumbHeight * $imgWhFact;
692 $thumbHeight = round($thumbHeight);
693 $thumbWidth = round($thumbWidth);
696 if (empty($targetformat)) {
697 $targetformat = $infoImg[2];
701 if ($targetformat == IMAGETYPE_GIF) {
703 $imgThumb = imagecreate($thumbWidth, $thumbHeight);
705 $imgThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
709 if (function_exists(
'imageantialias')) {
710 imageantialias($imgThumb,
true);
714 if (function_exists(
'imagesavealpha')) {
715 imagesavealpha($imgThumb,
true);
720 switch ($targetformat) {
722 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255);
723 imagecolortransparent($imgThumb, $trans_colour);
724 $extImgTarget =
'.gif';
728 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
729 $extImgTarget = (preg_match(
'/\.jpeg$/i', $file) ?
'.jpeg' :
'.jpg');
730 $newquality = $quality;
733 imagealphablending($imgThumb,
false);
734 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127);
735 $extImgTarget =
'.png';
736 $newquality = $quality - 100;
737 $newquality = round(abs($quality - 100) * 9 / 100);
741 $extImgTarget =
'.bmp';
745 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
746 $extImgTarget =
'.bmp';
750 if (function_exists(
"imagefill")) {
751 imagefill($imgThumb, 0, 0, $trans_colour);
754 dol_syslog(
"vignette: convert image from ($imgWidth x $imgHeight) to ($thumbWidth x $thumbHeight) as $extImg, newquality=$newquality");
756 imagecopyresampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
758 $fileName = preg_replace(
'/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i',
'', $file);
759 $fileName = basename($fileName);
769 switch ($targetformat) {
771 imagegif($imgThumb, $imgThumbName);
774 imagejpeg($imgThumb, $imgThumbName, $newquality);
777 imagepng($imgThumb, $imgThumbName, $newquality);
783 imagewbmp($imgThumb, $imgThumbName);
792 imagedestroy($imgThumb);
794 return $imgThumbName;