簡體   English   中英

禁用動態gif圖像大小調整

[英]Disable image resizing if animated gif

我正在嘗試實現兩個PHP函數,以禁用WordPress上動畫的gif圖像的圖像大小調整。 如果文件的MIME類型為gif,有一種解決方案可以禁止上傳,但這還不夠。 gif也可以只是圖像。 所以我想我將它與PHP腳本結合起來,通過使用此解決方案來檢查文件是否為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);

我在這里做錯了什么,這是行不通的?

您的代碼有效...但是$file_url上有錯誤。

應該是$file_url = $upload_path . '/' . $relative_path; $file_url = $upload_path . '/' . $relative_path;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM