我试图实现两个PHP函数来禁用动画的WordPress上gif图像的图像大小调整。如果文件MIME类型是gif,则有solution来禁用上传,但这还不够。 gif也可以只是一个图像。 所以我认为我将它与一个PHP脚本结合起来,通过使用这个solution来检查文件是否是动画gif。如果我在我的主题文件中回显这个函数,这似乎有效,但如果我在functions.php中使用它似乎没有功能。
/**
* Detects animated GIF from given file pointer resource or filename.
*
* @param resource|string $file File pointer resource or filename
* @return bool
*/
function is_animated_gif($file)
{
$fp = null;
if (is_string($file)) {
$fp = fopen($file, "rb");
} else {
$fp = $file;
/* Make sure that we are at the beginning of the file */
fseek($fp, 0);
}
if (fread($fp, 3) !== "GIF") {
fclose($fp);
return false;
}
$frames = 0;
while (!feof($fp) && $frames < 2) {
if (fread($fp, 1) === "\x00") {
/* Some of the animated GIFs do not contain graphic control extension (starts with 21 f9) */
if (fread($fp, 1) === "\x21" || fread($fp, 2) === "\x21\xf9") {
$frames++;
}
}
}
fclose($fp);
return $frames > 1;
}
function disable_upload_sizes( $sizes, $metadata ) {
$uploads = wp_upload_dir();
$upload_path = $uploads['baseurl'];
$relative_path = $metadata['file'];
$file_url = $upload_path . $relative_path;
if( is_animated_gif( $file_url ) ) {
$sizes = array();
}
// Return sizes you want to create from image (None if image is gif.)
return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'disable_upload_sizes', 10, 2);
我在这里做错了什么,这不起作用?
答案 0 :(得分:3)
您的代码有效...但$file_url
上有错误。
应为$file_url = $upload_path . '/' . $relative_path;