2015-06-18 42 views
2

我已將我網站上圖片的直接網址傳遞到wp_get_image_editor()函數,該函數始終返回「invalid_image」錯誤。WordPress wp_get_image_editor返回「invalid_image」錯誤

該URL當然是正確的,並且圖像當然是真實的; Chrome允許我在調試輸出中單擊URL並打開一個顯示圖像的新選項卡。該圖像是一個620×413的JPEG,所以它不完全是一個邊緣情況(我也測試了一個700×490 PNG)。我也嘗試使用我在Google圖片搜索中找到的無關公開訪問圖像的URL,並且發生了同樣的情況。

爲了讓事情變得更加令人困惑,這發生在插件的內部,該插件在我開發的Ubuntu安裝上完美運行,並且只有在將其部署到生產服務器後纔開始投放。這讓我想知道它是否可能是某種深奧的配置問題,但我不知道它會是怎樣的。我正在使用WordPress 4.2.2。

+0

存在於大多數PHP產品上的標準安全設置不允許獲取文件的URL以'的file_get_contents()'該函數可能在某個階段使用,offhand'allow_url_fopen'是php的設置改變。 – David

+0

感謝您的回覆。我會在wp-settings.php,wp-config.php中設置allow_url_fopen,還是直接在插件.php文件中設置allow_url_fopen? –

+0

劃痕。 init_set函數(大概用於設置allow_url_fopen)似乎在設置和普通插件代碼中都是未定義的。這個功能是否與WordPress兼容? –

回答

3

最後通過使用本地路徑而不是公共路徑(wp-content/uploads/2015/06/my_image.png而不是www.mysite.com/wp-content/uploads/2015/06/my_image.png)來解決此問題。 WordPress可以在這裏真正使用更有用的消息。

0

此外,這裏是一個在網站中隨處可用的功能。在的functions.php補充一點:

function ab_image_resize($imgurl, $width, $height, $imgname) { 
    if(!$imgurl) { $imgurl = get_stylesheet_directory().'/default.png'; } //set a default image if no image is found 
     $image = wp_get_image_editor($imgurl); 
     if (! is_wp_error($image)) { 
      $image->resize($width, $height, true); 
      $filename = $image->generate_filename($imgname, ABSPATH.'wp-content/uploads/ab_resized/', NULL); //create the upload folder; here: ab_resized 
      $image->save($filename); 
      $new_img_name = basename($filename); 
      return get_site_url().'/wp-content/uploads/ab_resized/'.$new_img_name; 
} 
    else { 
     return "An error occured while saving the image"; 
    } 
} 

此電話爲:

ab_image_resize('full_image_url_here', $width, $height, 'any_name_here');