From 5272d7e00be6dba3a195cd318b815cc84149c237 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Mon, 2 Oct 2023 01:04:29 +0700 Subject: [PATCH] =?UTF-8?q?Added=20support=20for=20webp=20images=20?= =?UTF-8?q?=F0=9F=8C=86=20(#919)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 1 * Temp: added webp in bbcode * Update functions_thumbs.php * Update functions_thumbs.php * Updated * Update functions_thumbs.php * Update changes.txt * Update functions_filetypes.php * Update functions_filetypes.php * Update posting_tpl.tpl --- admin/admin_attachments.php | 2 +- install/sql/mysql.sql | 1 + install/upgrade/changes.txt | 1 + .../includes/functions_filetypes.php | 10 --- .../attach_mod/includes/functions_thumbs.php | 77 +++++++++++-------- library/config.php | 2 +- library/defines.php | 6 +- src/Legacy/BBCode.php | 2 +- styles/templates/posting_tpl.tpl | 4 +- 9 files changed, 57 insertions(+), 48 deletions(-) diff --git a/admin/admin_attachments.php b/admin/admin_attachments.php index cd0aee645..02752ea36 100644 --- a/admin/admin_attachments.php +++ b/admin/admin_attachments.php @@ -291,7 +291,7 @@ if ($mode == 'cats') { $use_gd2_no = ($new_attach['use_gd2'] == '0') ? 'checked' : ''; // Check Thumbnail Support - if (!is_imagick() && !@extension_loaded('gd')) { + if (!is_imagick() && !extension_loaded('gd')) { $new_attach['img_create_thumbnail'] = '0'; } else { $template->assign_block_vars('switch_thumbnail_support', []); diff --git a/install/sql/mysql.sql b/install/sql/mysql.sql index ef295f970..f14617989 100644 --- a/install/sql/mysql.sql +++ b/install/sql/mysql.sql @@ -708,6 +708,7 @@ VALUES ('1', 'gif', ''), ('1', 'jpg', ''), ('1', 'tif', ''), ('1', 'tga', ''), + ('1', 'webp', ''), ('2', 'gtar', ''), ('2', 'gz', ''), ('2', 'tar', ''), diff --git a/install/upgrade/changes.txt b/install/upgrade/changes.txt index 3e19ced51..544fccfe9 100644 --- a/install/upgrade/changes.txt +++ b/install/upgrade/changes.txt @@ -72,4 +72,5 @@ ALTER TABLE `bb_bt_tracker_snap` ADD COLUMN `completed` INT(10) NOT NULL DEFAULT ALTER TABLE `bb_bt_tracker` CHANGE `complete` `complete` TINYINT(1) NOT NULL DEFAULT '0'; // 2.4.0-beta3 +INSERT INTO `bb_extensions` VALUES ('1', 'webp', ''); INSERT INTO `bb_extensions` VALUES ('2', '7z', ''); diff --git a/library/attach_mod/includes/functions_filetypes.php b/library/attach_mod/includes/functions_filetypes.php index ad2c1fba3..69aea6df7 100644 --- a/library/attach_mod/includes/functions_filetypes.php +++ b/library/attach_mod/includes/functions_filetypes.php @@ -70,7 +70,6 @@ function image_getdimension($file) $error = false; // BMP - IMAGE - $tmp_str = fread($fp, 2); if ($tmp_str == 'BM') { $length = read_longint($fp); @@ -120,13 +119,10 @@ function image_getdimension($file) fclose($fp); // GIF - IMAGE - $fp = @fopen($file, 'rb'); - $tmp_str = fread($fp, 3); if ($tmp_str == 'GIF') { - $tmp_str = fread($fp, 3); $width = read_word($fp); $height = read_word($fp); @@ -159,8 +155,6 @@ function image_getdimension($file) // JPG - IMAGE $fp = @fopen($file, 'rb'); - - $tmp_str = fread($fp, 4); $w1 = read_word($fp); if ((int)$w1 < 16) { @@ -176,7 +170,6 @@ function image_getdimension($file) } if (!$error) { - $str = fread($fp, 2); $b = read_byte($fp); if ($b != 0 && $b != 1 && $b != 2) { @@ -210,9 +203,7 @@ function image_getdimension($file) fclose($fp); // PCX - IMAGE - $fp = @fopen($file, 'rb'); - $tmp_str = fread($fp, 3); if ((ord($tmp_str[0]) == 10) && (ord($tmp_str[1]) == 0 || ord($tmp_str[1]) == 2 || ord($tmp_str[1]) == 3 || ord($tmp_str[1]) == 4 || ord($tmp_str[1]) == 5) && (ord($tmp_str[2]) == 1)) { @@ -227,7 +218,6 @@ function image_getdimension($file) $ymin = read_word($fp); $xmax = read_word($fp); $ymax = read_word($fp); - $tmp_str = fread($fp, 52); $b = fread($fp, 1); if ($b != 0) { diff --git a/library/attach_mod/includes/functions_thumbs.php b/library/attach_mod/includes/functions_thumbs.php index 42b636db9..984a71c99 100644 --- a/library/attach_mod/includes/functions_thumbs.php +++ b/library/attach_mod/includes/functions_thumbs.php @@ -54,42 +54,49 @@ function is_imagick() */ function get_supported_image_types($type) { - if (@extension_loaded('gd')) { - $format = imagetypes(); - $new_type = 0; - - switch ($type) { - case 1: - $new_type = ($format & IMG_GIF) ? IMG_GIF : 0; - break; - case 2: - case 9: - case 10: - case 11: - case 12: - $new_type = ($format & IMG_JPG) ? IMG_JPG : 0; - break; - case 3: - $new_type = ($format & IMG_PNG) ? IMG_PNG : 0; - break; - case 6: - case 15: - $new_type = ($format & IMG_WBMP) ? IMG_WBMP : 0; - break; - } - - return [ - 'gd' => (bool)$new_type, - 'format' => $new_type, - 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1 - ]; + // Check GD extension installed + if (!extension_loaded('gd')) { + return ['gd' => false]; } - return ['gd' => false]; + $format = imagetypes(); + $new_type = 0; + + switch ($type) { + case 1: + $new_type = ($format & IMG_GIF) ? IMG_GIF : 0; + break; + case 2: + case 9: + case 10: + case 11: + case 12: + $new_type = ($format & IMG_JPG) ? IMG_JPG : 0; + break; + case 3: + case 4: + $new_type = ($format & IMG_PNG) ? IMG_PNG : 0; + break; + case 6: + case 8: + case 15: + $new_type = ($format & IMG_WBMP) ? IMG_WBMP : 0; + break; + case 32: + $new_type = ($format & IMG_WEBP) ? IMG_WEBP : 0; + break; + } + + return [ + 'gd' => (bool)$new_type, + 'format' => $new_type, + 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1 + ]; } /** * Create thumbnail + * @throws Exception */ function create_thumbnail($source, $new_file, $mimetype) { @@ -138,6 +145,11 @@ function create_thumbnail($source, $new_file, $mimetype) case IMG_WBMP: $image = imagecreatefromwbmp($source); break; + case IMG_WEBP: + $image = imagecreatefromwebp($source); + break; + default: + throw new Exception('Unknown file format: ' . $type['format']); } if ($type['version'] == 1 || !$attach_config['use_gd2']) { @@ -161,6 +173,11 @@ function create_thumbnail($source, $new_file, $mimetype) case IMG_WBMP: imagewbmp($new_image, $new_file); break; + case IMG_WEBP: + imagewebp($new_image, $new_file); + break; + default: + throw new Exception('Unknown file format: ' . $type['format']); } imagedestroy($new_image); diff --git a/library/config.php b/library/config.php index 89d9a3033..ef9068969 100644 --- a/library/config.php +++ b/library/config.php @@ -508,7 +508,7 @@ $bb_cfg['user_not_active_days_keep'] = 180; // inactive users but only with no p $bb_cfg['group_members_per_page'] = 50; // количество групп на одной странице // Tidy -$bb_cfg['tidy_post'] = in_array('tidy', get_loaded_extensions(), true); +$bb_cfg['tidy_post'] = extension_loaded('tidy'); // Misc $bb_cfg['mem_on_start'] = memory_get_usage(); diff --git a/library/defines.php b/library/defines.php index 68c85c652..1c0643b4e 100644 --- a/library/defines.php +++ b/library/defines.php @@ -19,10 +19,10 @@ define('CHECK_REQIREMENTS', [ 'php_min_version' => '8.1.0', 'ext_list' => [ 'json', - 'gd', - 'zlib', + // 'gd', (optional) + // 'zlib', (optional) 'curl', - 'tidy', + // 'tidy', (optional) 'mysqli', 'bcmath', 'mbstring', diff --git a/src/Legacy/BBCode.php b/src/Legacy/BBCode.php index ca9caa9f9..ea064903f 100644 --- a/src/Legacy/BBCode.php +++ b/src/Legacy/BBCode.php @@ -76,7 +76,7 @@ class BBCode private function init_replacements(): void { $tpl = $this->tpl; - $img_exp = '(https?:)?//[^\s\?&;=\#\"<>]+?\.(jpg|jpeg|gif|png|bmp)([a-z0-9/?&%;][^\[\]]*)?'; + $img_exp = '(https?:)?//[^\s\?&;=\#\"<>]+?\.(jpg|jpeg|gif|png|bmp|webp)([a-z0-9/?&%;][^\[\]]*)?'; $email_exp = '[a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+'; $this->preg = [ diff --git a/styles/templates/posting_tpl.tpl b/styles/templates/posting_tpl.tpl index 18d4c71e7..4a556ef5e 100644 --- a/styles/templates/posting_tpl.tpl +++ b/styles/templates/posting_tpl.tpl @@ -374,8 +374,8 @@ var TPL = { reg: { num : /^\d+$/, URL : /^https?:\/\/[\w\#$%&~/.\-;:=?@\[\]+]+$/i, - img : /^https?:\/\/[^\s\?&;:=\#\"<>]+\.(jpg|jpeg|gif|png|bmp)$/i, - img_tag : /(https?:\/\/[^\s\?&;:=\#\"<>]+\.(jpg|jpeg|gif|png|bmp)(?!\[|\]|\.))/ig + img : /^https?:\/\/[^\s\?&;:=\#\"<>]+\.(jpg|jpeg|gif|png|bmp|webp)$/i, + img_tag : /(https?:\/\/[^\s\?&;:=\#\"<>]+\.(jpg|jpeg|gif|png|bmp|webp)(?!\[|\]|\.))/ig }, // построение сообщения на основе данных из формы