Support converting BMP and WEBP images to JPG

This commit is contained in:
Kijin Sung 2019-08-01 03:13:58 +09:00
parent 62eb21abbb
commit bf93afd7e5
7 changed files with 95 additions and 17 deletions

View file

@ -527,6 +527,12 @@ class FileHandler
case '6' :
$type = 'bmp';
break;
case '8' :
$type = 'wbmp';
break;
case '18' :
$type = 'webp';
break;
default :
return;
}
@ -603,13 +609,24 @@ class FileHandler
$source = @imagecreatefrompng($source_file);
}
break;
case 'wbmp' :
case 'bmp' :
if(function_exists('imagecreatefrombmp'))
{
$source = @imagecreatefrombmp($source_file);
}
break;
case 'wbmp' :
if(function_exists('imagecreatefromwbmp'))
{
$source = @imagecreatefromwbmp($source_file);
}
break;
case 'webp' :
if(function_exists('imagecreatefromwebp'))
{
$source = @imagecreatefromwebp($source_file);
}
break;
}
if(!$source)
@ -665,13 +682,24 @@ class FileHandler
$output = imagepng($thumb, $target_file, 9);
}
break;
case 'wbmp' :
case 'bmp' :
if(function_exists('imagebmp'))
{
$output = imagebmp($thumb, $target_file, 100);
}
break;
case 'wbmp' :
if(function_exists('imagewbmp'))
{
$output = imagewbmp($thumb, $target_file, 100);
}
break;
case 'webp' :
if(function_exists('imagewebp'))
{
$output = imagewebp($thumb, $target_file, 100);
}
break;
}
imagedestroy($thumb);

View file

@ -69,6 +69,8 @@ class fileAdminController extends file
$config->max_image_width = intval(Context::get('max_image_width')) ?: '';
$config->max_image_height = intval(Context::get('max_image_height')) ?: '';
$config->max_image_size_action = Context::get('max_image_size_action') ?: '';
$config->image_autoconv['bmp2jpg'] = Context::get('image_autoconv_bmp2jpg') === 'Y' ? true : false;
$config->image_autoconv['webp2jpg'] = Context::get('image_autoconv_webp2jpg') === 'Y' ? true : false;
// Check maximum file size
if (PHP_INT_SIZE < 8)

View file

@ -863,12 +863,15 @@ class fileController extends file
// Sanitize filename
$file_info['name'] = Rhymix\Framework\Filters\FilenameFilter::clean($file_info['name']);
$file_info['resized'] = false;
// Get extension
$extension = explode('.', $file_info['name']) ?: array('');
$extension = strtolower(array_pop($extension));
// Add extra fields to file info array
$file_info['extension'] = $extension;
$file_info['resized'] = false;
// Check file type, size, and other attributes
if(!$manual_insert && !$this->user->isAdmin())
{
@ -892,7 +895,7 @@ class fileController extends file
}
}
// Check image dimensions
// Check image type and size
if(in_array($extension, array('gif', 'jpg', 'jpeg', 'png', 'webp', 'bmp')))
{
$file_info = $this->checkUploadedImage($file_info, $config);
@ -953,7 +956,7 @@ class fileController extends file
}
// Move the file
if($manual_insert && !$file_info['resized'])
if($manual_insert && !$file_info['converted'])
{
@copy($file_info['tmp_name'], $filename);
if(!file_exists($filename))
@ -965,7 +968,7 @@ class fileController extends file
}
}
}
elseif(starts_with(RX_BASEDIR . 'files/attach/chunks/', $file_info['tmp_name']) || $file_info['resized'])
elseif(starts_with(RX_BASEDIR . 'files/attach/chunks/', $file_info['tmp_name']) || $file_info['converted'])
{
if (!Rhymix\Framework\Storage::move($file_info['tmp_name'], $filename))
{
@ -1034,11 +1037,24 @@ class fileController extends file
return $file_info;
}
$image_width = $image_info[0];
$image_height = $image_info[1];
$image_type = $image_info[2];
$convert = false;
// Check image type
if($config->image_autoconv['bmp2jpg'] && function_exists('imagebmp') && $image_type === 6)
{
$convert = array($image_width, $image_height, 'jpg');
}
if($config->image_autoconv['webp2jpg'] && function_exists('imagewebp') && $image_type === 18)
{
$convert = array($image_width, $image_height, 'jpg');
}
// Check image size
if($config->max_image_size_action && ($config->max_image_width || $config->max_image_height))
{
$image_width = $image_info[0];
$image_height = $image_info[1];
$exceeded = false;
if ($config->max_image_width > 0 && $image_width > $config->max_image_width)
{
@ -1085,18 +1101,25 @@ class fileController extends file
$resize_width = $resize_width * ($config->max_image_height / $resize_height);
$resize_height = $config->max_image_height;
}
$target_type = ($extension === 'webp' || $extension === 'bmp') ? 'jpg' : $extension;
$resize_result = FileHandler::createImageFile($file_info['tmp_name'], $file_info['tmp_name'] . '.resized', intval($resize_width), intval($resize_height), $target_type);
if ($resize_result)
{
$file_info['tmp_name'] = $file_info['tmp_name'] . '.resized';
$file_info['size'] = filesize($file_info['tmp_name']);
$file_info['resized'] = true;
}
$target_type = in_array($image_type, array(6, 8, 18)) ? 'jpg' : $file_info['extension'];
$convert = array(intval($resize_width), intval($resize_height), $target_type);
}
}
}
// Convert image if necessary
if ($convert)
{
$result = FileHandler::createImageFile($file_info['tmp_name'], $file_info['tmp_name'] . '.conv', $convert[0], $convert[1], $convert[2]);
if ($result)
{
$file_info['name'] = preg_replace('/\.' . preg_quote($file_info['extension'], '/') . '$/i', '.' . $convert[2], $file_info['name']);
$file_info['tmp_name'] = $file_info['tmp_name'] . '.conv';
$file_info['size'] = filesize($file_info['tmp_name']);
$file_info['converted'] = true;
}
}
return $file_info;
}

View file

@ -192,6 +192,7 @@ class fileModel extends file
$config->max_image_width = $file_config->max_image_width;
$config->max_image_height = $file_config->max_image_height;
$config->max_image_size_action = $file_config->max_image_size_action;
$config->image_autoconv = $file_config->image_autoconv;
$config->download_grant = $file_config->download_grant;
$config->allow_outlink = $file_config->allow_outlink;
$config->allow_outlink_site = $file_config->allow_outlink_site;
@ -209,6 +210,7 @@ class fileModel extends file
if(!$config->max_image_width) $config->max_image_width = $file_module_config->max_image_width;
if(!$config->max_image_height) $config->max_image_height = $file_module_config->max_image_height;
if(!$config->max_image_size_action) $config->max_image_size_action = $file_module_config->max_image_size_action;
if(!$config->image_autoconv) $config->image_autoconv = $file_module_config->image_autoconv;
// Default setting if not exists
if(!$config->allowed_filesize) $config->allowed_filesize = '2';
@ -217,6 +219,7 @@ class fileModel extends file
if(!$config->allow_outlink) $config->allow_outlink = 'Y';
if(!$config->download_grant) $config->download_grant = array();
if(!$config->inline_download_format) $config->inline_download_format = array();
if(!$config->image_autoconv) $config->image_autoconv = array();
return $config;
}

View file

@ -21,6 +21,9 @@ $lang->max_image_size = 'Maximum Image Size';
$lang->max_image_size_action_nothing = 'If exceeded, do nothing';
$lang->max_image_size_action_block = 'If exceeded, block upload';
$lang->max_image_size_action_resize = 'If exceeded, resize automatically';
$lang->image_autoconv = 'Auto-Convert Image';
$lang->image_autoconv_bmp2jpg = 'BMP → JPG';
$lang->image_autoconv_webp2jpg = 'WEBP → JPG';
$lang->inline_download_format = 'Open in current window';
$lang->inline_download_image = 'Image';
$lang->inline_download_audio = 'Audio';
@ -39,6 +42,7 @@ $lang->about_allowed_attach_size_global = 'This is the global limit on the combi
$lang->about_allowed_size_limits = 'The file size will be limited to the value set in php.ini (%sB) in IE9 and below and older Android browsers.';
$lang->about_allowed_filetypes = 'To allow an extension, use "*.[extention]". To allow multiple extensions, use ";" between each extension. ex) *.* or *.jpg;*.gif; ';
$lang->about_max_image_size = 'You can limit the maximum width and height of uploaded images.';
$lang->about_image_autoconv = 'Automatically convert types of images that often cause trouble or waste disk space into other types.';
$lang->cmd_delete_checked_file = 'Delete Selected Item(s)';
$lang->cmd_move_to_document = 'Move to Document';
$lang->cmd_download = 'Download';

View file

@ -21,6 +21,9 @@ $lang->max_image_size = '이미지 크기 제한';
$lang->max_image_size_action_nothing = '초과시 아무 것도 하지 않음';
$lang->max_image_size_action_block = '초과시 업로드 금지';
$lang->max_image_size_action_resize = '초과시 자동 크기 조정';
$lang->image_autoconv = '이미지 자동 변환';
$lang->image_autoconv_bmp2jpg = 'BMP → JPG';
$lang->image_autoconv_webp2jpg = 'WEBP → JPG';
$lang->inline_download_format = '다운로드시 현재 창 사용';
$lang->inline_download_image = '이미지';
$lang->inline_download_audio = '오디오';
@ -39,6 +42,7 @@ $lang->about_allowed_attach_size_global = '관리자를 포함하여 사이트
$lang->about_allowed_size_limits = 'IE9 이하, 구버전 안드로이드 등에서는 php.ini에서 지정한 %sB로 제한됩니다.';
$lang->about_allowed_filetypes = '"*.확장자"로 지정할 수 있고 ";" 으로 여러 개 지정이 가능합니다. 예) *.* or *.jpg;*.gif;';
$lang->about_max_image_size = '이미지 파일의 가로세로 크기를 제한할 수 있습니다.';
$lang->about_image_autoconv = '종종 문제를 일으키거나 용량을 낭비하는 이미지 타입을 다른 타입으로 자동 변환합니다.';
$lang->cmd_delete_checked_file = '선택항목 삭제';
$lang->cmd_move_to_document = '문서로 이동';
$lang->cmd_download = '다운로드';

View file

@ -23,7 +23,7 @@
</div>
</div>
<div class="x_control-group">
<label for="max_image_width" class="x_control-label">{$lang->max_image_size}</label>
<label class="x_control-label">{$lang->max_image_size}</label>
<div class="x_controls">
<input type="number" min="0" name="max_image_width" id="max_image_width" value="{$config->max_image_width}" size="7" style="min-width:80px" /> &times;
<input type="number" min="0" name="max_image_height" id="max_image_height" value="{$config->max_image_height}" size="7" style="min-width:80px" /> px &nbsp;
@ -35,6 +35,20 @@
<p class="x_help-block">{$lang->about_max_image_size}</p>
</div>
</div>
<div class="x_control-group">
<label class="x_control-label">{$lang->image_autoconv}</label>
<div class="x_controls">
<label for="image_autoconv_bmp2jpg" class="x_inline">
<input type="checkbox" name="image_autoconv_bmp2jpg" id="image_autoconv_bmp2jpg" value="Y" checked="checked"|cond="$config->image_autoconv['bmp2jpg']" disabled="disabled"|cond="!function_exists('imagebmp')" />
{$lang->image_autoconv_bmp2jpg}
</label>
<label for="image_autoconv_webp2jpg" class="x_inline">
<input type="checkbox" name="image_autoconv_webp2jpg" id="image_autoconv_webp2jpg" value="Y" checked="checked"|cond="$config->image_autoconv['webp2jpg']" disabled="disabled"|cond="!function_exists('imagewebp')" />
{$lang->image_autoconv_webp2jpg}
</label>
<p class="x_help-block">{$lang->about_image_autoconv}</p>
</div>
</div>
<div class="x_control-group">
<label for="allowedFiletypes" class="x_control-label">{$lang->allowed_filetypes}</label>
<div class="x_controls">