2008-04-05 16:18:13 +02:00
< ? php
2010-02-21 04:09:45 +01:00
/* Copyright ( C ) 2004 - 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2007 Regis Houssin < regis . houssin @ inodbox . com >
2025-01-05 16:53:07 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2024-06-15 16:05:41 +02:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2008-04-05 16:18:13 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2008-04-05 16:18:13 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2008-04-05 16:18:13 +02:00
*/
/**
2011-10-24 10:45:06 +02:00
* \file htdocs / core / lib / images . lib . php
2011-02-07 15:23:57 +01:00
* \brief Set of function for manipulating images
2008-11-09 01:49:57 +01:00
*/
2008-04-05 16:18:13 +02:00
2011-02-07 15:23:57 +01:00
// Define size of logo small and mini
2023-04-25 11:32:49 +02:00
// TODO Remove this and call getDefaultImageSizes() instead
2021-03-01 20:37:16 +01:00
$maxwidthsmall = 480 ;
$maxheightsmall = 270 ; // Near 16/9eme
$maxwidthmini = 128 ;
$maxheightmini = 72 ; // 16/9eme
2011-02-07 15:23:57 +01:00
$quality = 80 ;
2023-12-08 01:42:29 +01:00
if ( ! defined ( 'IMAGETYPE_WEBP' )) {
define ( 'IMAGETYPE_WEBP' , 18 );
}
2023-04-25 11:32:49 +02:00
/**
* Return default values for image sizes
*
2024-11-04 12:32:13 +01:00
* @ return array { maxwidthsmall : int , maxheightsmall : int , maxwidthmini : int , maxheightmini : int , quality : int } Array of default values
2023-04-25 11:32:49 +02:00
*/
function getDefaultImageSizes ()
{
$maxwidthsmall = 480 ;
$maxheightsmall = 270 ; // Near 16/9eme
$maxwidthmini = 128 ;
$maxheightmini = 72 ; // 16/9eme
$quality = 80 ;
return array (
'maxwidthsmall' => $maxwidthsmall ,
'maxheightsmall' => $maxheightsmall ,
'maxwidthmini' => $maxwidthmini ,
'maxheightmini' => $maxheightmini ,
'quality' => $quality
);
}
2011-02-07 15:23:57 +01:00
2010-02-21 02:45:58 +01:00
/**
2011-02-07 15:23:57 +01:00
* Return if a filename is file name of a supported image format
2012-02-04 14:39:47 +01:00
*
2020-08-06 17:28:57 +02:00
* @ param int $acceptsvg 0 = Default ( depends on setup ), 1 = Always accept SVG as image files
2024-01-13 19:48:20 +01:00
* @ return string Return list of image formats
2010-02-21 02:45:58 +01:00
*/
2021-04-08 14:20:59 +02:00
function getListOfPossibleImageExt ( $acceptsvg = 0 )
2010-02-21 02:45:58 +01:00
{
2020-09-07 10:18:17 +02:00
$regeximgext = '\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm' ; // See also into product.class.php
2023-11-27 11:39:32 +01:00
if ( $acceptsvg || getDolGlobalString ( 'MAIN_ALLOW_SVG_FILES_AS_IMAGES' )) {
2020-09-08 21:27:28 +02:00
$regeximgext .= '|\.svg' ; // Not allowed by default. SVG can contains javascript
2020-09-07 10:18:17 +02:00
}
2021-04-08 14:20:59 +02:00
return $regeximgext ;
}
/**
* Return if a filename is file name of a supported image format
*
* @ param string $file Filename
* @ param int $acceptsvg 0 = Default ( depends on setup ), 1 = Always accept SVG as image files
* @ return int - 1 = Not image filename , 0 = Image filename but format not supported for conversion by PHP , 1 = Image filename with format supported by this PHP
*/
function image_format_supported ( $file , $acceptsvg = 0 )
{
$regeximgext = getListOfPossibleImageExt ();
2020-09-07 10:18:17 +02:00
// Case filename is not a format image
$reg = array ();
2021-02-23 22:03:23 +01:00
if ( ! preg_match ( '/(' . $regeximgext . ')$/i' , $file , $reg )) {
return - 1 ;
}
2020-09-07 10:18:17 +02:00
// Case filename is a format image but not supported by this PHP
$imgfonction = '' ;
2021-02-23 22:03:23 +01:00
if ( strtolower ( $reg [ 1 ]) == '.gif' ) {
$imgfonction = 'imagecreatefromgif' ;
}
if ( strtolower ( $reg [ 1 ]) == '.jpg' ) {
$imgfonction = 'imagecreatefromjpeg' ;
}
if ( strtolower ( $reg [ 1 ]) == '.jpeg' ) {
$imgfonction = 'imagecreatefromjpeg' ;
}
if ( strtolower ( $reg [ 1 ]) == '.png' ) {
$imgfonction = 'imagecreatefrompng' ;
}
if ( strtolower ( $reg [ 1 ]) == '.bmp' ) {
$imgfonction = 'imagecreatefromwbmp' ;
}
if ( strtolower ( $reg [ 1 ]) == '.webp' ) {
$imgfonction = 'imagecreatefromwebp' ;
}
if ( strtolower ( $reg [ 1 ]) == '.xpm' ) {
$imgfonction = 'imagecreatefromxpm' ;
}
if ( strtolower ( $reg [ 1 ]) == '.xbm' ) {
$imgfonction = 'imagecreatefromxbm' ;
}
if ( strtolower ( $reg [ 1 ]) == '.svg' ) {
$imgfonction = 'imagecreatefromsvg' ; // Never available
}
if ( $imgfonction ) {
if ( ! function_exists ( $imgfonction )) {
2024-01-13 19:48:20 +01:00
// Functions of conversion not available in this PHP
2020-09-07 10:18:17 +02:00
return 0 ;
}
// Filename is a format image and supported for conversion by this PHP
return 1 ;
}
return 0 ;
2011-02-07 15:23:57 +01:00
}
2010-02-21 02:45:58 +01:00
2011-02-07 15:23:57 +01:00
/**
2021-04-04 01:12:25 +02:00
* Return size of image file on disk ( Supported extensions are gif , jpg , png , bmp and webp )
2012-02-04 14:39:47 +01:00
*
* @ param string $file Full path name of file
2013-07-03 16:06:42 +02:00
* @ param bool $url Image with url ( true or false )
2024-11-04 12:32:13 +01:00
* @ return array { width : int , height : int } | array {} | array { width : '' , height : '' } array ( 'width' => width , 'height' => height )
2011-02-07 15:23:57 +01:00
*/
2013-07-03 16:06:42 +02:00
function dol_getImageSize ( $file , $url = false )
2011-02-07 15:23:57 +01:00
{
2019-12-06 09:26:49 +01:00
$ret = array ();
2010-02-21 02:45:58 +01:00
2021-02-23 22:03:23 +01:00
if ( image_format_supported ( $file ) < 0 ) {
return $ret ;
}
2010-02-21 02:45:58 +01:00
2017-01-12 16:03:07 +01:00
$filetoread = $file ;
2021-02-23 22:03:23 +01:00
if ( ! $url ) {
2019-12-06 09:26:49 +01:00
$filetoread = realpath ( dol_osencode ( $file )); // Chemin canonique absolu de l'image
2013-07-03 16:06:42 +02:00
}
2010-02-21 02:45:58 +01:00
2021-02-23 22:03:23 +01:00
if ( $filetoread ) {
2020-09-07 10:18:17 +02:00
$infoImg = getimagesize ( $filetoread ); // Recuperation des infos de l'image
2023-02-18 20:47:19 +01:00
if ( $infoImg ) {
$ret [ 'width' ] = $infoImg [ 0 ]; // Largeur de l'image
$ret [ 'height' ] = $infoImg [ 1 ]; // Hauteur de l'image
} else {
$ret [ 'width' ] = $ret [ 'height' ] = '' ;
}
2017-01-12 16:03:07 +01:00
}
2017-06-09 00:27:19 +02:00
2010-02-21 02:45:58 +01:00
return $ret ;
}
2008-04-05 16:18:13 +02:00
2010-02-21 04:09:45 +01:00
/**
2021-04-04 01:12:25 +02:00
* Resize or crop an image file ( Supported extensions are gif , jpg , png , bmp and webp )
2012-02-04 14:39:47 +01:00
*
2021-04-04 01:12:25 +02:00
* @ param string $file Path of source file to resize / crop
* @ param int $mode 0 = Resize , 1 = Crop
* @ param int $newWidth Largeur maximum que dois faire l ' image destination ( 0 = keep ratio )
* @ param int $newHeight Hauteur maximum que dois faire l ' image destination ( 0 = keep ratio )
* @ param int $src_x Position of croping image in source image ( not use if mode = 0 )
* @ param int $src_y Position of croping image in source image ( not use if mode = 0 )
* @ param string $filetowrite Path of file to write ( overwrite source file if not provided )
2021-05-01 15:38:58 +02:00
* @ param int $newquality Value for the new quality of image , for supported format ( use 0 for maximum / unchanged ) .
2021-04-04 01:12:25 +02:00
* @ return string File name if OK , error message if KO
* @ see dol_convert_file ()
2010-02-21 04:09:45 +01:00
*/
2021-05-01 15:38:58 +02:00
function dol_imageResizeOrCrop ( $file , $mode , $newWidth , $newHeight , $src_x = 0 , $src_y = 0 , $filetowrite = '' , $newquality = 0 )
2010-02-21 04:09:45 +01:00
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
2010-02-21 04:09:45 +01:00
2024-09-30 15:28:52 +02:00
global $langs ;
2010-02-21 04:09:45 +01:00
2010-02-21 12:50:21 +01:00
dol_syslog ( " dol_imageResizeOrCrop file= " . $file . " mode= " . $mode . " newWidth= " . $newWidth . " newHeight= " . $newHeight . " src_x= " . $src_x . " src_y= " . $src_y );
2010-02-21 04:09:45 +01:00
// Clean parameters
2019-12-06 09:26:49 +01:00
$file = trim ( $file );
2010-02-21 04:09:45 +01:00
// Check parameters
2021-02-23 22:03:23 +01:00
if ( ! $file ) {
2010-02-21 04:09:45 +01:00
// Si le fichier n'a pas ete indique
return 'Bad parameter file' ;
2021-02-23 22:03:23 +01:00
} elseif ( ! file_exists ( $file )) {
2024-01-13 19:48:20 +01:00
// Si le fichier passe en parameter n'existe pas
2019-01-27 11:55:16 +01:00
return $langs -> trans ( " ErrorFileNotFound " , $file );
2021-02-23 22:03:23 +01:00
} elseif ( image_format_supported ( $file ) < 0 ) {
2010-02-21 12:50:21 +01:00
return 'This filename ' . $file . ' does not seem to be an image filename.' ;
2021-02-23 22:03:23 +01:00
} elseif ( ! is_numeric ( $newWidth ) && ! is_numeric ( $newHeight )) {
2010-02-21 04:09:45 +01:00
return 'Wrong value for parameter newWidth or newHeight' ;
2021-04-04 01:12:25 +02:00
} elseif ( $mode == 0 && $newWidth <= 0 && $newHeight <= 0 && ( empty ( $filetowrite ) || $filetowrite == $file )) {
return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert' ;
2021-02-23 22:03:23 +01:00
} elseif ( $mode == 1 && ( $newWidth <= 0 || $newHeight <= 0 )) {
2010-02-21 12:50:21 +01:00
return 'Both newHeight or newWidth must be defined for croping' ;
2010-02-21 04:09:45 +01:00
}
2019-12-06 09:26:49 +01:00
$filetoread = realpath ( dol_osencode ( $file )); // Chemin canonique absolu de l'image
2010-02-21 04:09:45 +01:00
2021-05-02 15:15:27 +02:00
$infoImg = getimagesize ( $filetoread ); // Get data about src image
2010-02-21 04:09:45 +01:00
$imgWidth = $infoImg [ 0 ]; // Largeur de l'image
$imgHeight = $infoImg [ 1 ]; // Hauteur de l'image
2021-05-02 15:15:27 +02:00
$imgTargetName = ( $filetowrite ? $filetowrite : $file );
2021-05-03 15:56:56 +02:00
$newExt = strtolower ( pathinfo ( $imgTargetName , PATHINFO_EXTENSION ));
2021-05-02 15:15:27 +02:00
2021-02-23 22:03:23 +01:00
if ( $mode == 0 ) { // If resize, we check parameters
2021-04-04 01:12:25 +02:00
if ( ! empty ( $filetowrite ) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0 ) {
$newWidth = $imgWidth ;
$newHeight = $imgHeight ;
}
2021-02-23 22:03:23 +01:00
if ( $newWidth <= 0 ) {
2019-12-06 09:26:49 +01:00
$newWidth = intval (( $newHeight / $imgHeight ) * $imgWidth ); // Keep ratio
2010-02-21 12:50:21 +01:00
}
2021-02-23 22:03:23 +01:00
if ( $newHeight <= 0 ) {
2019-12-06 09:26:49 +01:00
$newHeight = intval (( $newWidth / $imgWidth ) * $imgHeight ); // Keep ratio
2010-02-21 12:50:21 +01:00
}
2010-02-21 04:09:45 +01:00
}
2021-05-03 19:25:16 +02:00
// Test function to read source image exists
2019-12-06 09:26:49 +01:00
$imgfonction = '' ;
2021-02-23 22:03:23 +01:00
switch ( $infoImg [ 2 ]) {
2010-02-21 04:09:45 +01:00
case 1 : // IMG_GIF
$imgfonction = 'imagecreatefromgif' ;
break ;
case 2 : // IMG_JPG
$imgfonction = 'imagecreatefromjpeg' ;
break ;
case 3 : // IMG_PNG
$imgfonction = 'imagecreatefrompng' ;
break ;
case 4 : // IMG_WBMP
$imgfonction = 'imagecreatefromwbmp' ;
break ;
2021-05-02 15:29:52 +02:00
case 18 : // IMG_WEBP
2020-06-03 14:05:18 +02:00
$imgfonction = 'imagecreatefromwebp' ;
break ;
2010-02-21 04:09:45 +01:00
}
2021-02-23 22:03:23 +01:00
if ( $imgfonction ) {
if ( ! function_exists ( $imgfonction )) {
2024-01-13 19:48:20 +01:00
// Functions de conversion non presente dans ce PHP
2021-05-02 15:15:27 +02:00
return 'Read of image not possible. This PHP does not support GD functions ' . $imgfonction ;
2010-02-21 04:09:45 +01:00
}
}
2021-05-03 19:25:16 +02:00
// Test function to write target image exists
if ( $filetowrite ) {
$imgfonction = '' ;
switch ( $newExt ) {
2022-08-12 14:31:58 +02:00
case 'gif' : // IMG_GIF
2021-05-03 19:25:16 +02:00
$imgfonction = 'imagecreatefromgif' ;
break ;
2022-08-12 14:31:58 +02:00
case 'jpg' : // IMG_JPG
case 'jpeg' : // IMG_JPEG
2021-05-03 19:25:16 +02:00
$imgfonction = 'imagecreatefromjpeg' ;
break ;
2022-08-12 14:31:58 +02:00
case 'png' : // IMG_PNG
2021-05-03 19:25:16 +02:00
$imgfonction = 'imagecreatefrompng' ;
break ;
2022-08-12 14:31:58 +02:00
case 'bmp' : // IMG_WBMP
2021-05-03 19:25:16 +02:00
$imgfonction = 'imagecreatefromwbmp' ;
break ;
2022-08-12 14:31:58 +02:00
case 'webp' : // IMG_WEBP
2021-05-03 19:25:16 +02:00
$imgfonction = 'imagecreatefromwebp' ;
break ;
}
if ( $imgfonction ) {
if ( ! function_exists ( $imgfonction )) {
2024-01-13 19:48:20 +01:00
// Functions de conversion non presente dans ce PHP
2021-05-03 19:25:16 +02:00
return 'Write of image not possible. This PHP does not support GD functions ' . $imgfonction ;
}
}
}
2021-05-02 15:15:27 +02:00
// Read source image file
2024-11-04 12:32:13 +01:00
$img = null ;
$extImg = null ;
2021-02-23 22:03:23 +01:00
switch ( $infoImg [ 2 ]) {
2010-02-21 04:09:45 +01:00
case 1 : // Gif
2017-01-12 16:03:07 +01:00
$img = imagecreatefromgif ( $filetoread );
2019-12-06 09:26:49 +01:00
$extImg = '.gif' ; // File name extension of image
2010-02-21 04:09:45 +01:00
break ;
case 2 : // Jpg
2017-01-12 16:03:07 +01:00
$img = imagecreatefromjpeg ( $filetoread );
2010-02-21 12:50:21 +01:00
$extImg = '.jpg' ;
2010-02-21 04:09:45 +01:00
break ;
case 3 : // Png
2017-01-12 16:03:07 +01:00
$img = imagecreatefrompng ( $filetoread );
2010-02-21 04:09:45 +01:00
$extImg = '.png' ;
break ;
case 4 : // Bmp
2017-01-12 16:03:07 +01:00
$img = imagecreatefromwbmp ( $filetoread );
2010-02-21 04:09:45 +01:00
$extImg = '.bmp' ;
break ;
2020-06-03 14:05:18 +02:00
case 18 : // Webp
$img = imagecreatefromwebp ( $filetoread );
$extImg = '.webp' ;
break ;
2010-02-21 04:09:45 +01:00
}
2025-01-05 16:53:07 +01:00
if ( $img === null ) {
return " Error: Could not create Image from ' $filetoread ' " ;
}
2021-05-02 15:15:27 +02:00
// Create empty image for target
2021-05-03 15:56:56 +02:00
if ( $newExt == 'gif' ) {
2021-05-02 15:15:27 +02:00
// Compatibility image GIF
$imgTarget = imagecreate ( $newWidth , $newHeight );
2020-05-21 15:05:19 +02:00
} else {
2021-05-02 15:15:27 +02:00
$imgTarget = imagecreatetruecolor ( $newWidth , $newHeight );
2010-02-21 04:09:45 +01:00
}
// Activate antialiasing for better quality
2021-02-23 22:03:23 +01:00
if ( function_exists ( 'imageantialias' )) {
2021-05-02 15:15:27 +02:00
imageantialias ( $imgTarget , true );
2010-02-21 04:09:45 +01:00
}
// This is to keep transparent alpha channel if exists (PHP >= 4.2)
2021-02-23 22:03:23 +01:00
if ( function_exists ( 'imagesavealpha' )) {
2021-05-02 15:15:27 +02:00
imagesavealpha ( $imgTarget , true );
2010-02-21 04:09:45 +01:00
}
2021-05-02 15:29:52 +02:00
// Set transparent color according to image extension
2022-08-12 14:31:58 +02:00
$trans_colour = - 1 ; // By default, undefined
2021-05-03 15:56:56 +02:00
switch ( $newExt ) {
case 'gif' : // Gif
2024-01-13 19:48:20 +01:00
$trans_colour = imagecolorallocate ( $imgTarget , 255 , 255 , 255 ); // The method is different for the GIF format
2021-05-02 15:15:27 +02:00
imagecolortransparent ( $imgTarget , $trans_colour );
2010-02-21 04:09:45 +01:00
break ;
2021-05-03 15:56:56 +02:00
case 'jpg' : // Jpg
2022-08-12 14:31:58 +02:00
case 'jpeg' : // Jpeg
2021-05-02 15:15:27 +02:00
$trans_colour = imagecolorallocatealpha ( $imgTarget , 255 , 255 , 255 , 0 );
2010-02-21 04:09:45 +01:00
break ;
2021-05-03 15:56:56 +02:00
case 'png' : // Png
2024-01-13 19:48:20 +01:00
imagealphablending ( $imgTarget , false ); // For compatibility with certain systems
2021-05-02 15:15:27 +02:00
$trans_colour = imagecolorallocatealpha ( $imgTarget , 255 , 255 , 255 , 127 ); // Keep transparent channel
2010-02-21 04:09:45 +01:00
break ;
2021-05-03 15:56:56 +02:00
case 'bmp' : // Bmp
2021-05-02 15:15:27 +02:00
$trans_colour = imagecolorallocatealpha ( $imgTarget , 255 , 255 , 255 , 0 );
2010-02-21 04:09:45 +01:00
break ;
2021-05-03 15:56:56 +02:00
case 'webp' : // Webp
2021-05-02 15:15:27 +02:00
$trans_colour = imagecolorallocatealpha ( $imgTarget , 255 , 255 , 255 , 127 );
2020-06-03 14:05:18 +02:00
break ;
2010-02-21 04:09:45 +01:00
}
2022-08-12 14:31:58 +02:00
if ( function_exists ( " imagefill " ) && $trans_colour > 0 ) {
2021-05-02 15:15:27 +02:00
imagefill ( $imgTarget , 0 , 0 , $trans_colour );
2021-02-23 22:03:23 +01:00
}
2010-02-21 04:09:45 +01:00
2022-08-12 14:31:58 +02:00
dol_syslog ( " dol_imageResizeOrCrop: convert image from ( $imgWidth x $imgHeight ) at position ( $src_x x $src_y ) to ( $newWidth x $newHeight ) as a $extImg " );
2021-05-02 15:15:27 +02:00
//imagecopyresized($imgTarget, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
imagecopyresampled ( $imgTarget , $img , 0 , 0 , $src_x , $src_y , $newWidth , $newHeight , ( $mode == 0 ? $imgWidth : $newWidth ), ( $mode == 0 ? $imgHeight : $newHeight )); // Insere l'image de base redimensionnee
2010-02-21 04:09:45 +01:00
// Check if permission are ok
2021-04-04 01:12:25 +02:00
//$fp = fopen($imgTargetName, "w");
2010-02-21 04:09:45 +01:00
//fclose($fp);
2021-04-04 01:12:25 +02:00
// Create image on disk (overwrite file if exists)
switch ( $newExt ) {
case 'gif' : // Gif
2021-05-02 15:15:27 +02:00
$newquality = 'NU' ; // Quality is not used for this format
imagegif ( $imgTarget , $imgTargetName );
2010-02-21 04:09:45 +01:00
break ;
2021-04-04 01:12:25 +02:00
case 'jpg' : // Jpg
2022-08-12 14:31:58 +02:00
case 'jpeg' : // Jpeg
2021-05-02 15:15:27 +02:00
$newquality = ( $newquality ? $newquality : '100' ); // % quality maximum
imagejpeg ( $imgTarget , $imgTargetName , $newquality );
2010-02-21 04:09:45 +01:00
break ;
2021-04-04 01:12:25 +02:00
case 'png' : // Png
2021-05-02 15:15:27 +02:00
$newquality = 0 ; // No compression (0-9)
imagepng ( $imgTarget , $imgTargetName , $newquality );
2010-02-21 04:09:45 +01:00
break ;
2021-04-04 01:12:25 +02:00
case 'bmp' : // Bmp
2021-05-02 15:15:27 +02:00
$newquality = 'NU' ; // Quality is not used for this format
imagewbmp ( $imgTarget , $imgTargetName );
2010-02-21 04:09:45 +01:00
break ;
2021-04-04 01:12:25 +02:00
case 'webp' : // Webp
2021-05-02 15:15:27 +02:00
$newquality = ( $newquality ? $newquality : '100' ); // % quality maximum
imagewebp ( $imgTarget , $imgTargetName , $newquality );
2020-06-03 14:05:18 +02:00
break ;
2022-08-12 14:31:58 +02:00
default :
dol_syslog ( " images.lib.php::imageResizeOrCrop() Format " . $newExt . " is not supported " , LOG_WARNING );
2010-02-21 04:09:45 +01:00
}
// Set permissions on file
2023-02-17 19:30:50 +01:00
dolChmod ( $imgTargetName );
2010-02-21 04:09:45 +01:00
2011-07-06 00:51:24 +02:00
// Free memory. This does not delete image.
2024-11-04 12:32:13 +01:00
if ( $img ) {
imagedestroy ( $img );
}
if ( $imgTarget ) {
imagedestroy ( $imgTarget );
}
2010-02-21 04:09:45 +01:00
2019-12-06 09:26:49 +01:00
clearstatcache (); // File was replaced by a modified one, so we clear file caches.
2010-02-21 12:50:21 +01:00
2021-04-04 01:12:25 +02:00
return $imgTargetName ;
2010-02-21 04:09:45 +01:00
}
2016-04-09 19:42:34 +02:00
/**
* dolRotateImage if image is a jpg file .
* Currently use an autodetection to know if we can rotate .
* TODO Introduce a new parameter to force rotate .
*
* @ param string $file_path Full path to image to rotate
* @ return boolean Success or not
*/
function dolRotateImage ( $file_path )
{
2019-12-02 17:32:06 +01:00
return correctExifImageOrientation ( $file_path , $file_path );
2016-04-09 19:42:34 +02:00
}
2019-12-02 17:32:06 +01:00
/**
* Add exif orientation correction for image
*
2019-12-03 09:24:11 +01:00
* @ param string $fileSource Full path to source image to rotate
2024-09-29 21:49:52 +02:00
* @ param string | bool | null $fileDest string : Full path to image to rotate | false return gd img | null the raw image stream will be outputted directly
* @ param int <- 1 , 100 > $quality output image quality
2019-12-03 09:24:11 +01:00
* @ return bool : true on success or false on failure or gd img if $fileDest is false .
2019-12-02 17:32:06 +01:00
*/
function correctExifImageOrientation ( $fileSource , $fileDest , $quality = 95 )
{
2019-12-04 22:19:35 +01:00
if ( function_exists ( 'exif_read_data' )) {
2019-12-09 14:44:37 +01:00
$exif = @ exif_read_data ( $fileSource );
2019-12-03 22:52:20 +01:00
if ( $exif && isset ( $exif [ 'Orientation' ])) {
2019-12-02 17:32:06 +01:00
$infoImg = getimagesize ( $fileSource ); // Get image infos
$orientation = $exif [ 'Orientation' ];
2020-04-10 10:59:32 +02:00
if ( $orientation != 1 ) {
2019-12-02 17:32:06 +01:00
$img = imagecreatefromjpeg ( $fileSource );
$deg = 0 ;
switch ( $orientation ) {
case 3 :
$deg = 180 ;
break ;
case 6 :
$deg = 270 ;
break ;
case 8 :
$deg = 90 ;
break ;
}
if ( $deg ) {
2023-12-08 01:42:29 +01:00
if ( $infoImg [ 2 ] === IMAGETYPE_PNG ) { // In fact there is no exif on PNG but just in case
2019-12-02 17:32:06 +01:00
imagealphablending ( $img , false );
imagesavealpha ( $img , true );
2024-01-12 20:40:34 +01:00
$img = imagerotate ( $img , $deg , imagecolorallocatealpha ( $img , 0 , 0 , 0 , 127 ));
2019-12-02 17:32:06 +01:00
imagealphablending ( $img , false );
imagesavealpha ( $img , true );
2020-05-21 15:05:19 +02:00
} else {
2019-12-02 17:32:06 +01:00
$img = imagerotate ( $img , $deg , 0 );
}
}
// then rewrite the rotated image back to the disk as $fileDest
2019-12-06 09:26:49 +01:00
if ( $fileDest === false ) {
2019-12-02 17:32:06 +01:00
return $img ;
2020-05-21 15:05:19 +02:00
} else {
2019-12-02 17:32:06 +01:00
// In fact there exif is only for JPG but just in case
// Create image on disk
$image = false ;
2021-02-23 22:03:23 +01:00
switch ( $infoImg [ 2 ]) {
2019-12-02 17:32:06 +01:00
case IMAGETYPE_GIF : // 1
$image = imagegif ( $img , $fileDest );
break ;
case IMAGETYPE_JPEG : // 2
2019-12-04 22:13:50 +01:00
$image = imagejpeg ( $img , $fileDest , $quality );
2019-12-02 17:32:06 +01:00
break ;
case IMAGETYPE_PNG : // 3
$image = imagepng ( $img , $fileDest , $quality );
break ;
case IMAGETYPE_BMP : // 6
// Not supported by PHP GD
break ;
case IMAGETYPE_WBMP : // 15
2019-12-03 17:27:13 +01:00
$image = imagewbmp ( $img , $fileDest );
2019-12-02 17:32:06 +01:00
break ;
}
// Free up memory (imagedestroy does not delete files):
@ imagedestroy ( $img );
return $image ;
}
} // if there is some rotation necessary
} // if have the exif orientation info
} // if function exists
return false ;
}
2016-04-09 19:42:34 +02:00
2008-04-08 01:46:01 +02:00
/**
2011-02-07 15:23:57 +01:00
* Create a thumbnail from an image file ( Supported extensions are gif , jpg , png and bmp ) .
2025-01-17 01:05:11 +01:00
* If file is myfile . jpg , new file may be myfile_small . jpg . But extension may differs if original file has a format and an extension
* of another one , like a . jpg file when real format is png .
2011-12-14 17:13:09 +01:00
*
* @ param string $file Path of source file to resize
2023-11-23 17:45:59 +01:00
* @ param int $maxWidth Maximum width of the thumbnail ( - 1 = unchanged , 160 by default )
* @ param int $maxHeight Maximum height of the thumbnail ( - 1 = unchanged , 120 by default )
* @ param string $extName Extension to differentiate thumb file name ( '_small' , '_mini' )
2025-01-23 17:33:11 +01:00
* @ param int $quality Quality after compression ( 0 = worst so better compression , 100 = best so low or no compression )
2011-12-14 17:13:09 +01:00
* @ param string $outdir Directory where to store thumb
2025-01-17 01:03:35 +01:00
* @ param int $targetformat New format of target ( IMAGETYPE_GIF , IMAGETYPE_JPG , IMAGETYPE_PNG , IMAGETYPE_BMP , IMAGETYPE_WBMP ... or 0 to keep original format )
2024-09-14 01:36:31 +02:00
* @ return string | int < 0 , 0 > Full path of thumb or '' if it fails or 'Error...' if it fails , or 0 if it fails to detect the type of image
2008-04-05 16:18:13 +02:00
*/
2019-01-27 15:20:16 +01:00
function vignette ( $file , $maxWidth = 160 , $maxHeight = 120 , $extName = '_small' , $quality = 50 , $outdir = 'thumbs' , $targetformat = 0 )
2008-04-05 16:18:13 +02:00
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
2008-11-16 02:09:04 +01:00
2024-06-21 13:10:50 +02:00
global $langs ;
2008-04-05 16:18:13 +02:00
2011-06-22 11:11:00 +02:00
dol_syslog ( " vignette file= " . $file . " extName= " . $extName . " maxWidth= " . $maxWidth . " maxHeight= " . $maxHeight . " quality= " . $quality . " outdir= " . $outdir . " targetformat= " . $targetformat );
2008-11-28 10:46:47 +01:00
2008-11-09 01:49:57 +01:00
// Clean parameters
2025-01-23 17:33:11 +01:00
$file = dol_sanitizePathName ( trim ( $file ));
2008-04-05 16:18:13 +02:00
2008-11-09 01:49:57 +01:00
// Check parameters
2021-02-23 22:03:23 +01:00
if ( ! $file ) {
2023-11-23 17:45:59 +01:00
// If the file has not been indicated
2011-06-22 11:11:00 +02:00
return 'ErrorBadParameters' ;
2021-02-23 22:03:23 +01:00
} elseif ( image_format_supported ( $file ) < 0 ) {
2025-01-23 17:33:11 +01:00
dol_syslog ( 'This file ' . $file . ' does not seem to be a supported image file name (bad extension).' , LOG_WARNING );
2020-09-07 10:18:17 +02:00
return 'ErrorBadImageFormat' ;
2020-05-21 15:05:19 +02:00
} elseif ( ! is_numeric ( $maxWidth ) || empty ( $maxWidth ) || $maxWidth < - 1 ) {
2023-11-23 17:45:59 +01:00
// If max width is incorrect (not numeric, empty, or less than 0)
2020-09-07 10:18:17 +02:00
dol_syslog ( 'Wrong value for parameter maxWidth' , LOG_ERR );
return 'Error: Wrong value for parameter maxWidth' ;
2020-05-21 15:05:19 +02:00
} elseif ( ! is_numeric ( $maxHeight ) || empty ( $maxHeight ) || $maxHeight < - 1 ) {
2023-11-23 17:45:59 +01:00
// If max height is incorrect (not numeric, empty, or less than 0)
2020-09-07 10:18:17 +02:00
dol_syslog ( 'Wrong value for parameter maxHeight' , LOG_ERR );
return 'Error: Wrong value for parameter maxHeight' ;
2008-04-05 16:18:13 +02:00
}
2025-01-23 17:33:11 +01:00
$filetoread = realpath ( dol_osencode ( $file )); // Absolute canonical path of image
if ( ! file_exists ( $filetoread )) {
// If the file passed in parameter does not exist
dol_syslog ( $langs -> trans ( " ErrorFileNotFound " , $filetoread ), LOG_ERR );
return $langs -> trans ( " ErrorFileNotFound " , $filetoread );
}
2008-04-05 16:18:13 +02:00
2025-01-17 01:03:35 +01:00
$infoImg = getimagesize ( $filetoread ); // Get information like size and real format of image. Warning real format may be png when extension is .jpg
$imgWidth = $infoImg [ 0 ]; // Width of image
$imgHeight = $infoImg [ 1 ]; // Height of image
2008-04-05 16:18:13 +02:00
2025-01-17 01:39:14 +01:00
// TODO LDR
//if $infoImg[2] != extension of file $file, return a string 'Error: content of file has a format that differs of the format of its extension
2019-12-04 22:19:35 +01:00
$ort = false ;
if ( function_exists ( 'exif_read_data' )) {
2019-12-09 14:44:37 +01:00
$exif = @ exif_read_data ( $filetoread );
2019-12-04 22:19:35 +01:00
if ( $exif && ! empty ( $exif [ 'Orientation' ])) {
$ort = $exif [ 'Orientation' ];
}
2019-12-02 17:32:06 +01:00
}
2021-02-23 22:03:23 +01:00
if ( $maxWidth == - 1 ) {
$maxWidth = $infoImg [ 0 ]; // If size is -1, we keep unchanged
}
if ( $maxHeight == - 1 ) {
$maxHeight = $infoImg [ 1 ]; // If size is -1, we keep unchanged
}
2010-02-13 23:20:32 +01:00
2023-11-23 17:45:59 +01:00
// If the image is smaller than the maximum width and height, no thumbnail is created.
2021-02-23 22:03:23 +01:00
if ( $infoImg [ 0 ] < $maxWidth && $infoImg [ 1 ] < $maxHeight ) {
2008-04-05 16:18:13 +02:00
// On cree toujours les vignettes
2019-01-27 11:55:16 +01:00
dol_syslog ( " File size is smaller than thumb size " , LOG_DEBUG );
2008-11-09 01:49:57 +01:00
//return 'Le fichier '.$file.' ne necessite pas de creation de vignette';
2008-04-05 16:18:13 +02:00
}
2019-12-06 09:26:49 +01:00
$imgfonction = '' ;
2021-02-23 22:03:23 +01:00
switch ( $infoImg [ 2 ]) {
2011-12-14 17:13:09 +01:00
case IMAGETYPE_GIF : // 1
2008-04-05 16:18:13 +02:00
$imgfonction = 'imagecreatefromgif' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_JPEG : // 2
2008-04-05 16:18:13 +02:00
$imgfonction = 'imagecreatefromjpeg' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_PNG : // 3
2008-04-05 16:18:13 +02:00
$imgfonction = 'imagecreatefrompng' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_BMP : // 6
2020-09-07 10:18:17 +02:00
// Not supported by PHP GD
2011-12-14 17:13:09 +01:00
break ;
case IMAGETYPE_WBMP : // 15
2008-04-05 16:18:13 +02:00
$imgfonction = 'imagecreatefromwbmp' ;
break ;
2023-12-06 17:08:09 +01:00
case IMAGETYPE_WEBP : // 18
$imgfonction = 'imagecreatefromwebp' ;
break ;
2008-04-05 16:18:13 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $imgfonction ) {
if ( ! function_exists ( $imgfonction )) {
2023-11-23 17:45:59 +01:00
// Conversion functions not present in this PHP
2011-07-06 00:40:35 +02:00
return 'Error: Creation of thumbs not possible. This PHP does not support GD function ' . $imgfonction ;
2008-04-05 16:18:13 +02:00
}
}
2023-11-23 17:45:59 +01:00
// We create the directory containing the thumbnails
$dirthumb = dirname ( $file ) . ( $outdir ? '/' . $outdir : '' ); // Path to thumbnail folder
2012-02-19 18:34:22 +01:00
dol_mkdir ( $dirthumb );
2008-04-05 16:18:13 +02:00
2023-11-23 17:45:59 +01:00
// Variable initialization according to image extension
2019-12-06 09:26:49 +01:00
$img = null ;
2025-01-05 16:53:07 +01:00
$extImg = null ;
2021-02-23 22:03:23 +01:00
switch ( $infoImg [ 2 ]) {
2011-12-14 17:13:09 +01:00
case IMAGETYPE_GIF : // 1
2017-01-12 16:03:07 +01:00
$img = imagecreatefromgif ( $filetoread );
2023-12-06 17:08:09 +01:00
$extImg = '.gif' ;
2008-04-05 16:18:13 +02:00
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_JPEG : // 2
2017-01-12 16:03:07 +01:00
$img = imagecreatefromjpeg ( $filetoread );
2023-12-06 17:08:09 +01:00
$extImg = ( preg_match ( '/\.jpeg$/' , $file ) ? '.jpeg' : '.jpg' );
2008-04-05 16:18:13 +02:00
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_PNG : // 3
2017-01-12 16:03:07 +01:00
$img = imagecreatefrompng ( $filetoread );
2008-04-05 16:18:13 +02:00
$extImg = '.png' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_BMP : // 6
2020-09-07 10:18:17 +02:00
// Not supported by PHP GD
2011-12-14 17:13:09 +01:00
$extImg = '.bmp' ;
break ;
case IMAGETYPE_WBMP : // 15
2017-01-12 16:03:07 +01:00
$img = imagecreatefromwbmp ( $filetoread );
2008-04-05 16:18:13 +02:00
$extImg = '.bmp' ;
break ;
2023-12-06 17:08:09 +01:00
case IMAGETYPE_WEBP : // 18
$img = imagecreatefromwebp ( $filetoread );
$extImg = '.webp' ;
break ;
2008-04-05 16:18:13 +02:00
}
2019-12-06 08:11:23 +01:00
2022-04-03 12:25:43 +02:00
// Before PHP8, img was a resource, With PHP8, it is a GdImage
2024-06-15 16:05:41 +02:00
// if (!is_resource($img) && class_exists('GdImage') && !($img instanceof GdImage)) {
if ( is_null ( $img ) || $img === false ) {
2020-09-07 10:18:17 +02:00
dol_syslog ( 'Failed to detect type of image. We found infoImg[2]=' . $infoImg [ 2 ], LOG_WARNING );
return 0 ;
}
2019-12-06 08:11:23 +01:00
2019-12-02 17:32:06 +01:00
$exifAngle = false ;
2023-11-27 11:39:32 +01:00
if ( $ort && getDolGlobalString ( 'MAIN_USE_EXIF_ROTATION' )) {
2021-02-23 22:03:23 +01:00
switch ( $ort ) {
2019-12-02 17:32:06 +01:00
case 3 : // 180 rotate left
$exifAngle = 180 ;
break ;
case 6 : // 90 rotate right
$exifAngle = - 90 ;
// changing sizes
$trueImgWidth = $infoImg [ 1 ];
$trueImgHeight = $infoImg [ 0 ];
break ;
case 8 : // 90 rotate left
$exifAngle = 90 ;
// changing sizes
$trueImgWidth = $infoImg [ 1 ]; // Largeur de l'image
$trueImgHeight = $infoImg [ 0 ]; // Hauteur de l'image
break ;
}
}
2021-02-23 22:03:23 +01:00
if ( $exifAngle ) {
2019-12-02 17:32:06 +01:00
$rotated = false ;
2024-03-30 23:08:43 +01:00
if ( $infoImg [ 2 ] === IMAGETYPE_PNG ) { // In fact there is no exif on PNG but just in case
2019-12-02 17:32:06 +01:00
imagealphablending ( $img , false );
imagesavealpha ( $img , true );
2024-01-12 20:40:34 +01:00
$rotated = imagerotate ( $img , $exifAngle , imagecolorallocatealpha ( $img , 0 , 0 , 0 , 127 ));
2019-12-02 17:32:06 +01:00
imagealphablending ( $rotated , false );
imagesavealpha ( $rotated , true );
2020-05-21 15:05:19 +02:00
} else {
2019-12-02 17:32:06 +01:00
$rotated = imagerotate ( $img , $exifAngle , 0 );
}
2020-09-07 10:18:17 +02:00
// replace image with good orientation
2021-12-28 19:19:51 +01:00
if ( ! empty ( $rotated ) && isset ( $trueImgWidth ) && isset ( $trueImgHeight )) {
2020-09-07 10:18:17 +02:00
$img = $rotated ;
2019-12-02 17:32:06 +01:00
$imgWidth = $trueImgWidth ;
$imgHeight = $trueImgHeight ;
}
}
2023-11-23 17:45:59 +01:00
// Initialize thumbnail dimensions if larger than original
2021-02-23 22:03:23 +01:00
if ( $maxWidth > $imgWidth ) {
$maxWidth = $imgWidth ;
}
if ( $maxHeight > $imgHeight ) {
$maxHeight = $imgHeight ;
}
2008-04-05 16:18:13 +02:00
2023-11-23 17:45:59 +01:00
$whFact = $maxWidth / $maxHeight ; // Width/height factor for maximum label dimensions
$imgWhFact = $imgWidth / $imgHeight ; // Original width/height factor
2008-04-05 16:18:13 +02:00
2023-11-23 17:45:59 +01:00
// Set label dimensions
2021-02-23 22:03:23 +01:00
if ( $whFact < $imgWhFact ) {
2023-11-23 17:45:59 +01:00
// If determining width
2008-04-05 16:18:13 +02:00
$thumbWidth = $maxWidth ;
$thumbHeight = $thumbWidth / $imgWhFact ;
2020-05-21 15:05:19 +02:00
} else {
2023-11-23 17:45:59 +01:00
// If determining height
2008-04-05 16:18:13 +02:00
$thumbHeight = $maxHeight ;
$thumbWidth = $thumbHeight * $imgWhFact ;
}
2024-03-18 23:43:38 +01:00
$thumbHeight = ( int ) round ( $thumbHeight );
$thumbWidth = ( int ) round ( $thumbWidth );
2008-11-28 10:46:47 +01:00
2020-09-07 10:18:17 +02:00
// Define target format
2021-02-23 22:03:23 +01:00
if ( empty ( $targetformat )) {
$targetformat = $infoImg [ 2 ];
}
2010-08-20 21:19:24 +02:00
2008-04-05 16:18:13 +02:00
// Create empty image
2021-02-23 22:03:23 +01:00
if ( $targetformat == IMAGETYPE_GIF ) {
2008-11-09 01:49:57 +01:00
// Compatibilite image GIF
2008-04-05 16:18:13 +02:00
$imgThumb = imagecreate ( $thumbWidth , $thumbHeight );
2020-05-21 15:05:19 +02:00
} else {
2008-04-05 16:18:13 +02:00
$imgThumb = imagecreatetruecolor ( $thumbWidth , $thumbHeight );
}
// Activate antialiasing for better quality
2021-02-23 22:03:23 +01:00
if ( function_exists ( 'imageantialias' )) {
2008-04-05 16:18:13 +02:00
imageantialias ( $imgThumb , true );
}
// This is to keep transparent alpha channel if exists (PHP >= 4.2)
2021-02-23 22:03:23 +01:00
if ( function_exists ( 'imagesavealpha' )) {
2008-04-05 16:18:13 +02:00
imagesavealpha ( $imgThumb , true );
}
2008-11-28 10:46:47 +01:00
2023-11-23 17:45:59 +01:00
// Variable initialization according to image extension
2016-04-02 15:00:58 +02:00
// $targetformat is 0 by default, in such case, we keep original extension
2024-11-04 12:32:13 +01:00
$extImgTarget = null ;
$trans_colour = false ;
$newquality = null ;
2021-02-23 22:03:23 +01:00
switch ( $targetformat ) {
2011-12-14 17:13:09 +01:00
case IMAGETYPE_GIF : // 1
2023-11-23 17:45:59 +01:00
$trans_colour = imagecolorallocate ( $imgThumb , 255 , 255 , 255 ); // The GIF format works differently
2019-01-27 11:55:16 +01:00
imagecolortransparent ( $imgThumb , $trans_colour );
2020-09-07 10:18:17 +02:00
$extImgTarget = '.gif' ;
$newquality = 'NU' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_JPEG : // 2
2020-09-07 10:18:17 +02:00
$trans_colour = imagecolorallocatealpha ( $imgThumb , 255 , 255 , 255 , 0 );
$extImgTarget = ( preg_match ( '/\.jpeg$/i' , $file ) ? '.jpeg' : '.jpg' );
$newquality = $quality ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_PNG : // 3
2023-11-23 17:45:59 +01:00
imagealphablending ( $imgThumb , false ); // For compatibility on certain systems
2019-12-06 09:26:49 +01:00
$trans_colour = imagecolorallocatealpha ( $imgThumb , 255 , 255 , 255 , 127 ); // Keep transparent channel
2020-09-07 10:18:17 +02:00
$extImgTarget = '.png' ;
$newquality = round ( abs ( $quality - 100 ) * 9 / 100 );
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_BMP : // 6
2020-09-07 10:18:17 +02:00
// Not supported by PHP GD
$extImgTarget = '.bmp' ;
$newquality = 'NU' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_WBMP : // 15
2008-04-05 16:18:13 +02:00
$trans_colour = imagecolorallocatealpha ( $imgThumb , 255 , 255 , 255 , 0 );
2020-09-07 10:18:17 +02:00
$extImgTarget = '.bmp' ;
$newquality = 'NU' ;
break ;
2023-12-06 17:08:09 +01:00
case IMAGETYPE_WEBP : // 18
$trans_colour = imagecolorallocatealpha ( $imgThumb , 255 , 255 , 255 , 0 );
$extImgTarget = '.webp' ;
$newquality = $quality ;
break ;
2008-04-05 16:18:13 +02:00
}
2024-11-04 12:32:13 +01:00
if ( function_exists ( " imagefill " ) && $trans_colour !== false ) {
2021-02-23 22:03:23 +01:00
imagefill ( $imgThumb , 0 , 0 , $trans_colour );
}
2008-04-05 16:18:13 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( " vignette: convert image from ( $imgWidth x $imgHeight ) to ( $thumbWidth x $thumbHeight ) as $extImg , newquality= $newquality " );
2023-11-23 17:45:59 +01:00
//imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insert resized base image
imagecopyresampled ( $imgThumb , $img , 0 , 0 , 0 , 0 , $thumbWidth , $thumbHeight , $imgWidth , $imgHeight ); // Insert resized base image
2008-04-05 16:18:13 +02:00
2023-11-23 17:45:59 +01:00
$fileName = preg_replace ( '/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i' , '' , $file ); // We remove any extension box
2008-04-05 16:18:13 +02:00
$fileName = basename ( $fileName );
2015-11-06 01:56:56 +01:00
//$imgThumbName = $dirthumb.'/'.getImageFileNameForSize(basename($file), $extName, $extImgTarget); // Full path of thumb file
2019-12-06 09:26:49 +01:00
$imgThumbName = getImageFileNameForSize ( $file , $extName , $extImgTarget ); // Full path of thumb file
2015-11-06 01:56:56 +01:00
2008-04-05 16:18:13 +02:00
// Check if permission are ok
//$fp = fopen($imgThumbName, "w");
//fclose($fp);
// Create image on disk
2021-02-23 22:03:23 +01:00
switch ( $targetformat ) {
2011-12-14 17:13:09 +01:00
case IMAGETYPE_GIF : // 1
2008-04-05 16:18:13 +02:00
imagegif ( $imgThumb , $imgThumbName );
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_JPEG : // 2
2024-11-04 12:32:13 +01:00
imagejpeg ( $imgThumb , $imgThumbName , $newquality ); // @phan-suppress-current-line PhanTypeMismatchArgumentNullableInternal,PhanPossiblyUndeclaredVariable
2008-04-05 16:18:13 +02:00
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_PNG : // 3
2025-01-05 16:53:07 +01:00
imagepng ( $imgThumb , $imgThumbName , ! is_numeric ( $newquality ) ? - 1 : ( int ) $newquality ); // @phan-suppress-current-line PhanPossiblyUndeclaredVariable
2008-04-05 16:18:13 +02:00
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_BMP : // 6
2020-09-07 10:18:17 +02:00
// Not supported by PHP GD
2011-12-14 17:13:09 +01:00
break ;
case IMAGETYPE_WBMP : // 15
2019-11-26 18:41:38 +01:00
imagewbmp ( $imgThumb , $imgThumbName );
2008-04-05 16:18:13 +02:00
break ;
2023-12-06 17:08:09 +01:00
case IMAGETYPE_WEBP : // 18
2024-11-04 12:32:13 +01:00
imagewebp ( $imgThumb , $imgThumbName , $newquality ); // @phan-suppress-current-line PhanTypeMismatchArgumentNullableInternal,PhanPossiblyUndeclaredVariable
2023-12-06 17:08:09 +01:00
break ;
2008-04-05 16:18:13 +02:00
}
2008-11-09 01:49:57 +01:00
// Set permissions on file
2023-02-17 19:30:50 +01:00
dolChmod ( $imgThumbName );
2008-11-09 01:49:57 +01:00
2020-09-07 10:18:17 +02:00
// Free memory. This does not delete image.
imagedestroy ( $img );
imagedestroy ( $imgThumb );
2008-04-05 16:18:13 +02:00
return $imgThumbName ;
}
2024-06-21 13:10:50 +02:00
/**
* Beautify an image by adding a link edit and delete on image
*
* @ param string $htmlid ID of HTML img tag
* @ param string $urledit URL to submit to edit Image
* @ param string $urldelete URL to call when deleting the image
* @ return string HTML and JS code to manage the update / delete of image .
*/
function imgAddEditDeleteButton ( $htmlid , $urledit , $urldelete )
{
// TODO
return '' ;
}