gd_edit_image_support()
云策文档标注
概述
gd_edit_image_support() 函数用于检查已安装的 GD 库版本是否支持特定的图像类型,如 JPEG、PNG、GIF、WebP 或 AVIF。该函数自 WordPress 3.5.0 起已被弃用,建议使用 wp_image_editor_supports() 替代。
关键要点
- 函数用途:验证 GD 库对指定 MIME 类型图像的支持情况
- 参数:$mime_type(字符串,必需),指定要检查的图像 MIME 类型
- 返回值:布尔值,true 表示支持,false 表示不支持
- 弃用状态:自 WordPress 3.5.0 起弃用,推荐使用 wp_image_editor_supports()
- 内部逻辑:优先使用 imagetypes() 函数检查,若不可用则回退到检查相关 imagecreatefrom* 函数是否存在
代码示例
function gd_edit_image_support($mime_type) {
_deprecated_function( __FUNCTION__, '3.5.0', 'wp_image_editor_supports()' );
if ( function_exists('imagetypes') ) {
switch( $mime_type ) {
case 'image/jpeg':
return (imagetypes() & IMG_JPG) != 0;
case 'image/png':
return (imagetypes() & IMG_PNG) != 0;
case 'image/gif':
return (imagetypes() & IMG_GIF) != 0;
case 'image/webp':
return (imagetypes() & IMG_WEBP) != 0;
case 'image/avif':
return (imagetypes() & IMG_AVIF) != 0;
}
} else {
switch( $mime_type ) {
case 'image/jpeg':
return function_exists('imagecreatefromjpeg');
case 'image/png':
return function_exists('imagecreatefrompng');
case 'image/gif':
return function_exists('imagecreatefromgif');
case 'image/webp':
return function_exists('imagecreatefromwebp');
case 'image/avif':
return function_exists('imagecreatefromavif');
}
}
return false;
}注意事项
- 该函数已弃用,新代码应避免使用,改用 wp_image_editor_supports() 以确保兼容性和维护性
- 检查逻辑依赖于 GD 库的版本和配置,不同服务器环境可能返回不同结果
- 函数内部调用 _deprecated_function() 来标记弃用状态,使用时可能触发相关警告
原文内容
Check if the installed version of GD supports particular image type
Description
See also
Parameters
$mime_typestringrequired
Source
function gd_edit_image_support($mime_type) {
_deprecated_function( __FUNCTION__, '3.5.0', 'wp_image_editor_supports()' );
if ( function_exists('imagetypes') ) {
switch( $mime_type ) {
case 'image/jpeg':
return (imagetypes() & IMG_JPG) != 0;
case 'image/png':
return (imagetypes() & IMG_PNG) != 0;
case 'image/gif':
return (imagetypes() & IMG_GIF) != 0;
case 'image/webp':
return (imagetypes() & IMG_WEBP) != 0;
case 'image/avif':
return (imagetypes() & IMG_AVIF) != 0;
}
} else {
switch( $mime_type ) {
case 'image/jpeg':
return function_exists('imagecreatefromjpeg');
case 'image/png':
return function_exists('imagecreatefrompng');
case 'image/gif':
return function_exists('imagecreatefromgif');
case 'image/webp':
return function_exists('imagecreatefromwebp');
case 'image/avif':
return function_exists('imagecreatefromavif');
}
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 3.5.0 | Deprecated. Use wp_image_editor_supports() |
| 2.9.0 | Introduced. |