From 7b84518e59d5ad7cc012f36329eabbc780240c42 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 3 Jun 2020 14:05:18 +0200 Subject: [PATCH] Fix webp support --- htdocs/core/js/lib_head.js.php | 6 +++++- htdocs/core/lib/functions.lib.php | 6 ++++-- htdocs/core/lib/images.lib.php | 25 +++++++++++++++++++++---- htdocs/product/class/product.class.php | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/htdocs/core/js/lib_head.js.php b/htdocs/core/js/lib_head.js.php index 1efa4c88771..9de5ceba476 100644 --- a/htdocs/core/js/lib_head.js.php +++ b/htdocs/core/js/lib_head.js.php @@ -880,12 +880,13 @@ function newpopup(url, title) { */ function document_preview(file, type, title) { - var ValidImageTypes = ["image/gif", "image/jpeg", "image/png"]; + var ValidImageTypes = ["image/gif", "image/jpeg", "image/png", "image/webp"]; var showOriginalSizeButton = false; console.log("document_preview A click was done. file="+file+", type="+type+", title="+title); if ($.inArray(type, ValidImageTypes) < 0) { + /* Not an image */ var width='85%'; var object_width='100%'; var height = ($( window ).height() - 60) * 0.90; @@ -894,6 +895,7 @@ function document_preview(file, type, title) show_preview('notimage'); } else { + /* This is an image */ var object_width=0; var object_height=0; @@ -904,11 +906,13 @@ function document_preview(file, type, title) object_height = this.height; width = $( window ).width()*0.90; + console.log("object_width="+object_width+" window width="+width); if(object_width < width){ console.log("Object width is small, we set width of popup according to image width."); width = object_width + 30 } height = $( window ).height()*0.85; + console.log("object_height="+object_height+" window height="+height); if(object_height < height){ console.log("Object height is small, we set height of popup according to image height."); height = object_height + 80 diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index b30bc99f38a..dd107308fed 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -8055,7 +8055,7 @@ function getImageFileNameForSize($file, $extName, $extImgTarget = '') $dirName = dirname($file); if ($dirName == '.') $dirName = ''; - $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i', '', $file); // We remove extension, whatever is its case + $fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp|\.webp)$/i', '', $file); // We remove extension, whatever is its case $fileName = basename($fileName); if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.jpg$/i', $file) ? '.jpg' : ''); @@ -8063,6 +8063,7 @@ function getImageFileNameForSize($file, $extName, $extImgTarget = '') if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.gif$/i', $file) ? '.gif' : ''); if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.png$/i', $file) ? '.png' : ''); if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.bmp$/i', $file) ? '.bmp' : ''); + if (empty($extImgTarget)) $extImgTarget = (preg_match('/\.webp$/i', $file) ? '.webp' : ''); if (!$extImgTarget) return $file; @@ -8088,7 +8089,7 @@ function getAdvancedPreviewUrl($modulepart, $relativepath, $alldata = 0, $param if (empty($conf->use_javascript_ajax)) return ''; - $mime_preview = array('bmp', 'jpeg', 'png', 'gif', 'tiff', 'pdf', 'plain', 'css', 'svg+xml'); + $mime_preview = array('bmp', 'jpeg', 'png', 'gif', 'tiff', 'pdf', 'plain', 'css', 'svg+xml', 'webp'); //$mime_preview[]='vnd.oasis.opendocument.presentation'; //$mime_preview[]='archive'; $num_mime = array_search(dol_mimetype($relativepath, '', 1), $mime_preview); @@ -8199,6 +8200,7 @@ function dol_mimetype($file, $default = 'application/octet-stream', $mode = 0) if (preg_match('/\.bmp$/i', $tmpfile)) { $mime = 'image/bmp'; $imgmime = 'image.png'; $famime = 'file-image-o'; } if (preg_match('/\.(tif|tiff)$/i', $tmpfile)) { $mime = 'image/tiff'; $imgmime = 'image.png'; $famime = 'file-image-o'; } if (preg_match('/\.svg$/i', $tmpfile)) { $mime = 'image/svg+xml'; $imgmime = 'image.png'; $famime = 'file-image-o'; } + if (preg_match('/\.webp$/i', $tmpfile)) { $mime = 'image/webp'; $imgmime = 'image.png'; $famime = 'file-image-o'; } // Calendar if (preg_match('/\.vcs$/i', $tmpfile)) { $mime = 'text/calendar'; $imgmime = 'other.png'; $famime = 'file-text-o'; } if (preg_match('/\.ics$/i', $tmpfile)) { $mime = 'text/calendar'; $imgmime = 'other.png'; $famime = 'file-text-o'; } diff --git a/htdocs/core/lib/images.lib.php b/htdocs/core/lib/images.lib.php index 4f65ca3a2bf..b65ad51db86 100644 --- a/htdocs/core/lib/images.lib.php +++ b/htdocs/core/lib/images.lib.php @@ -37,7 +37,7 @@ $quality = 80; */ function image_format_supported($file) { - $regeximgext = '\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.xpm|\.xbm|\.svg'; // See also into product.class.php + $regeximgext = '\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm|\.svg'; // See also into product.class.php // Case filename is not a format image $reg = array(); @@ -46,10 +46,11 @@ function image_format_supported($file) // Case filename is a format image but not supported by this PHP $imgfonction = ''; if (strtolower($reg[1]) == '.gif') $imgfonction = 'imagecreatefromgif'; - if (strtolower($reg[1]) == '.png') $imgfonction = 'imagecreatefrompng'; 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 @@ -60,10 +61,12 @@ function image_format_supported($file) // Fonctions of conversion not available in this PHP return 0; } + + // Filename is a format image and supported for conversion by this PHP + return 1; } - // Filename is a format image and supported for conversion by this PHP - return 1; + return 0; } @@ -180,6 +183,9 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, case 4: // IMG_WBMP $imgfonction = 'imagecreatefromwbmp'; break; + case 17: // IMG_WBMP + $imgfonction = 'imagecreatefromwebp'; + break; } if ($imgfonction) { @@ -213,6 +219,11 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, $extImg = '.bmp'; $newquality = 'NU'; // Quality is not used for this format break; + case 18: // Webp + $img = imagecreatefromwebp($filetoread); + $extImg = '.webp'; + $newquality = '100'; // % quality maximum + break; } // Create empty image @@ -255,6 +266,9 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, case 4: // Bmp $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0); break; + case 18: // Webp + $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); + break; } if (function_exists("imagefill")) imagefill($imgThumb, 0, 0, $trans_colour); @@ -283,6 +297,9 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x = 0, case 4: // Bmp imagewbmp($imgThumb, $imgThumbName); break; + case 18: // Webp + imagewebp($imgThumb, $imgThumbName, $newquality); + break; } // Set permissions on file diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 7b6892552d1..d557dd90392 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -79,7 +79,7 @@ class Product extends CommonObject */ protected $table_ref_field = 'ref'; - public $regeximgext = '\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.xpm|\.xbm'; // See also into images.lib.php + public $regeximgext = '\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.webp|\.xpm|\.xbm'; // See also into images.lib.php /* * @deprecated