NEW Can set a target image in dolcropresize function.
This commit is contained in:
parent
17437670b1
commit
1a70aebba6
@ -1840,14 +1840,15 @@ function deleteFilesIntoDatabaseIndex($dir, $file, $mode = 'uploaded')
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert an image file into another format.
|
* Convert an image file or a PDF into another image format.
|
||||||
* This need Imagick php extension.
|
* This need Imagick php extension. You can use dol_imageResizeOrCrop() for a function that need GD.
|
||||||
*
|
*
|
||||||
* @param string $fileinput Input file name
|
* @param string $fileinput Input file name
|
||||||
* @param string $ext Format of target file (It is also extension added to file if fileoutput is not provided).
|
* @param string $ext Format of target file (It is also extension added to file if fileoutput is not provided).
|
||||||
* @param string $fileoutput Output filename
|
* @param string $fileoutput Output filename
|
||||||
* @param string $page Page number if we convert a PDF into png
|
* @param string $page Page number if we convert a PDF into png
|
||||||
* @return int <0 if KO, 0=Nothing done, >0 if OK
|
* @return int <0 if KO, 0=Nothing done, >0 if OK
|
||||||
|
* @see dol_imageResizeOrCrop()
|
||||||
*/
|
*/
|
||||||
function dol_convert_file($fileinput, $ext = 'png', $fileoutput = '', $page = '')
|
function dol_convert_file($fileinput, $ext = 'png', $fileoutput = '', $page = '')
|
||||||
{
|
{
|
||||||
|
|||||||
@ -97,7 +97,7 @@ function image_format_supported($file, $acceptsvg = 0)
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return size of image file on disk (Supported extensions are gif, jpg, png and bmp)
|
* Return size of image file on disk (Supported extensions are gif, jpg, png, bmp and webp)
|
||||||
*
|
*
|
||||||
* @param string $file Full path name of file
|
* @param string $file Full path name of file
|
||||||
* @param bool $url Image with url (true or false)
|
* @param bool $url Image with url (true or false)
|
||||||
@ -127,17 +127,19 @@ function dol_getImageSize($file, $url = false)
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resize or crop an image file (Supported extensions are gif, jpg, png and bmp)
|
* Resize or crop an image file (Supported extensions are gif, jpg, png, bmp and webp)
|
||||||
*
|
*
|
||||||
* @param string $file Path of file to resize/crop
|
* @param string $file Path of source file to resize/crop
|
||||||
* @param int $mode 0=Resize, 1=Crop
|
* @param int $mode 0=Resize, 1=Crop
|
||||||
* @param int $newWidth Largeur maximum que dois faire l'image destination (0=keep ratio)
|
* @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 $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_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 int $src_y Position of croping image in source image (not use if mode=0)
|
||||||
* @return string File name if OK, error message if KO
|
* @param string $filetowrite Path of file to write (overwrite source file if not provided)
|
||||||
|
* @return string File name if OK, error message if KO
|
||||||
|
* @see dol_convert_file()
|
||||||
*/
|
*/
|
||||||
function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0)
|
function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $src_y = 0, $filetowrite = '')
|
||||||
{
|
{
|
||||||
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
|
||||||
|
|
||||||
@ -159,8 +161,8 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0,
|
|||||||
return 'This filename '.$file.' does not seem to be an image filename.';
|
return 'This filename '.$file.' does not seem to be an image filename.';
|
||||||
} elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
|
} elseif (!is_numeric($newWidth) && !is_numeric($newHeight)) {
|
||||||
return 'Wrong value for parameter newWidth or newHeight';
|
return 'Wrong value for parameter newWidth or newHeight';
|
||||||
} elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0) {
|
} elseif ($mode == 0 && $newWidth <= 0 && $newHeight <= 0 && (empty($filetowrite) || $filetowrite == $file)) {
|
||||||
return 'At least newHeight or newWidth must be defined for resizing';
|
return 'At least newHeight or newWidth must be defined for resizing, or a target filename must be set to convert';
|
||||||
} elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
|
} elseif ($mode == 1 && ($newWidth <= 0 || $newHeight <= 0)) {
|
||||||
return 'Both newHeight or newWidth must be defined for croping';
|
return 'Both newHeight or newWidth must be defined for croping';
|
||||||
}
|
}
|
||||||
@ -172,6 +174,11 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0,
|
|||||||
$imgHeight = $infoImg[1]; // Hauteur de l'image
|
$imgHeight = $infoImg[1]; // Hauteur de l'image
|
||||||
|
|
||||||
if ($mode == 0) { // If resize, we check parameters
|
if ($mode == 0) { // If resize, we check parameters
|
||||||
|
if (!empty($filetowrite) && $filetowrite != $file && $newWidth <= 0 && $newHeight <= 0) {
|
||||||
|
$newWidth = $imgWidth;
|
||||||
|
$newHeight = $imgHeight;
|
||||||
|
}
|
||||||
|
|
||||||
if ($newWidth <= 0) {
|
if ($newWidth <= 0) {
|
||||||
$newWidth = intval(($newHeight / $imgHeight) * $imgWidth); // Keep ratio
|
$newWidth = intval(($newHeight / $imgHeight) * $imgWidth); // Keep ratio
|
||||||
}
|
}
|
||||||
@ -280,34 +287,36 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0,
|
|||||||
//imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
|
//imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
|
||||||
imagecopyresampled($imgThumb, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight)); // Insere l'image de base redimensionnee
|
imagecopyresampled($imgThumb, $img, 0, 0, $src_x, $src_y, $newWidth, $newHeight, ($mode == 0 ? $imgWidth : $newWidth), ($mode == 0 ? $imgHeight : $newHeight)); // Insere l'image de base redimensionnee
|
||||||
|
|
||||||
$imgThumbName = $file;
|
$imgTargetName = ($filetowrite ? $filetowrite : $file);
|
||||||
|
|
||||||
// Check if permission are ok
|
// Check if permission are ok
|
||||||
//$fp = fopen($imgThumbName, "w");
|
//$fp = fopen($imgTargetName, "w");
|
||||||
//fclose($fp);
|
//fclose($fp);
|
||||||
|
|
||||||
// Create image on disk
|
$newExt = strtolower(pathinfo($imgTargetName, PATHINFO_EXTENSION));
|
||||||
switch ($infoImg[2]) {
|
|
||||||
case 1: // Gif
|
// Create image on disk (overwrite file if exists)
|
||||||
imagegif($imgThumb, $imgThumbName);
|
switch ($newExt) {
|
||||||
|
case 'gif': // Gif
|
||||||
|
imagegif($imgThumb, $imgTargetName);
|
||||||
break;
|
break;
|
||||||
case 2: // Jpg
|
case 'jpg': // Jpg
|
||||||
imagejpeg($imgThumb, $imgThumbName, $newquality);
|
imagejpeg($imgThumb, $imgTargetName, $newquality);
|
||||||
break;
|
break;
|
||||||
case 3: // Png
|
case 'png': // Png
|
||||||
imagepng($imgThumb, $imgThumbName, $newquality);
|
imagepng($imgThumb, $imgTargetName, $newquality);
|
||||||
break;
|
break;
|
||||||
case 4: // Bmp
|
case 'bmp': // Bmp
|
||||||
imagewbmp($imgThumb, $imgThumbName);
|
imagewbmp($imgThumb, $imgTargetName);
|
||||||
break;
|
break;
|
||||||
case 18: // Webp
|
case 'webp': // Webp
|
||||||
imagewebp($imgThumb, $imgThumbName, $newquality);
|
imagewebp($imgThumb, $imgTargetName, $newquality);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set permissions on file
|
// Set permissions on file
|
||||||
if (!empty($conf->global->MAIN_UMASK)) {
|
if (!empty($conf->global->MAIN_UMASK)) {
|
||||||
@chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
|
@chmod($imgTargetName, octdec($conf->global->MAIN_UMASK));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free memory. This does not delete image.
|
// Free memory. This does not delete image.
|
||||||
@ -316,7 +325,7 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0,
|
|||||||
|
|
||||||
clearstatcache(); // File was replaced by a modified one, so we clear file caches.
|
clearstatcache(); // File was replaced by a modified one, so we clear file caches.
|
||||||
|
|
||||||
return $imgThumbName;
|
return $imgTargetName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -152,4 +152,20 @@ class ImagesLibTest extends PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testDolImageResizeOrCrop
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function testDolImageResizeOrCrop()
|
||||||
|
{
|
||||||
|
global $conf;
|
||||||
|
|
||||||
|
$file=dirname(__FILE__).'/img250x20.png';
|
||||||
|
$filetarget=$conf->admin->dir_temp.'/img250x20.webp';
|
||||||
|
$result = dol_imageResizeOrCrop($file, 0, 0, 0, 0, 0, $filetarget);
|
||||||
|
print __METHOD__." result=".$result."\n";
|
||||||
|
$this->assertEquals($filetarget, $result, 'Failed to convert into webp');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user