Close #17108 : New button to convert images in webp format

This commit is contained in:
lmarcouiller 2021-04-06 11:52:04 +02:00
parent a3fb8d3e14
commit c04387ff3b
2 changed files with 34 additions and 2 deletions

View File

@ -141,3 +141,7 @@ GenerateSitemaps=Generate website sitemap file
ConfirmGenerateSitemaps=If you confirm, you will erase the existing sitemap file...
ConfirmSitemapsCreation=Confirm sitemap generation
SitemapGenerated=Sitemap Generated
GenerateImgWebp=Convert all images into webp
ConfirmGenerateImgWebp=If you confirm, you will generate all website's images in webp format...
ConfirmImgWebpCreation=Confirm all images convertion
SucessConvertImgWebp=Images successfully converted

View File

@ -2230,7 +2230,31 @@ if ($action == 'generatesitemaps' && $usercanedit) {
}
$action = 'preview';
}
$imagefolder = $conf->website->dir_output.'/'.$websitekey.'/medias/image/'.$websitekey.'/';
if ($action == 'convertimgwebp' && $usercanedit) {
include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
$filelist = dol_dir_list($imagefolder, "all", 1);
foreach ($filelist as $filename) {
$filepath = $filename['fullname'];
if (!(substr_compare($filepath, 'webp', -strlen('webp')) === 0)) {
if (image_format_supported($filepath) == 1) {
$filepathnoext = preg_replace("/\..*/", "", $filepath);
$result = dol_imageResizeOrCrop($filepath, 0, 0, 0, 0, 0, $filepathnoext.'.webp');
if (!dol_is_file($result)) {
$error++;
setEventMessages($result, null, 'errors');
}
}
}
if ($error) {
break;
}
}
if (!$error) {
setEventMessages($langs->trans('SucessConvertImgWebp'), null);
}
$action = 'preview';
}
/*
* View
@ -2246,7 +2270,10 @@ if ($action == 'confirmgeneratesitemaps') {
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?website='.$website->ref, $langs->trans('ConfirmSitemapsCreation'), $langs->trans('ConfirmGenerateSitemaps', $object->ref), 'generatesitemaps', '', "yes", 1);
$action = 'preview';
}
if ($action == 'confirmconvertimgwebp') {
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?website='.$website->ref, $langs->trans('ConfirmImgWebpCreation'), $langs->trans('ConfirmGenerateImgWebp', $object->ref), 'convertimgwebp', '', "yes", 1);
$action = 'preview';
}
$helpurl = 'EN:Module_Website|FR:Module_Website_FR|ES:Módulo_Website';
$arrayofjs = array(
@ -2465,6 +2492,7 @@ if (!GETPOST('hide_websitemenu')) {
// Generate site map
print '<a href="'.$_SERVER["PHP_SEFL"].'?action=confirmgeneratesitemaps&website='.$website->ref.'" class="button bordertransp"'.$disabled.' title="'.dol_escape_htmltag($langs->trans("GenerateSitemaps")).'"><span class="fa fa-sitemap"><span></a>';
print '<a href="'.$_SERVER["PHP_SEFL"].'?action=confirmconvertimgwebp&website='.$website->ref.'" class="button bordertransp"'.$disabled.' title="'.dol_escape_htmltag($langs->trans("GenerateImgWebp")).'"><span class="fa fa-cogs"><span></a>';
print ' &nbsp; ';
print '<a href="'.$_SERVER["PHP_SEFL"].'?action=replacesite&website='.$website->ref.'" class="button bordertransp"'.$disabled.' title="'.dol_escape_htmltag($langs->trans("ReplaceWebsiteContent")).'"><span class="fa fa-search"><span></a>';