2012-01-31 32 views
1

我試圖創建一個使用last.fm.節點現在編程方式保存圖像從外部URL - Drupal的

$field = content_fields('field_my_image',"events"); //to get field 
$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field)); //image validator 
$files_path = filefield_widget_file_path($field); //save path 
$src_path=$data->image[3]; // img url from last.fm eg: http://userserve-ak.last.fm/serve/252/502025.jpg 
$file = field_file_save_file($src_path, $validators, $files_path, FILE_EXISTS_REPLACE);//save file 
$nodenew->field_my_image = array(
       array(
        'fid' => $file['fid'], 
        'title' => $file['filename'], 
        'filename' => $file['filename'], 
        'filepath' => $file['filepath'], 
        'filesize' => $file['filesize'], 
        'filemime' => $file['filemime'], 
), 
);//insert file details to node 

節點被創建,但沒有圖像和獲取消息「選定的文件502025.jpg無法保存。該文件不是已知的圖像格式。'

+1

歡迎堆棧溢出。祝賀您找到自己問題的答案。請張貼作爲一個答案([它的優良回答自己的問題(http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/) ),這讓其他人清楚這個問題有答案。如果沒有其他答案更有幫助,您也可以將自己的答案標記爲已接受。 – Gilles 2012-02-01 13:10:07

+0

更好的答案了在http://stackoverflow.com/q/5129559/1229018 – 2013-02-21 05:42:14

回答

2

更新

找到了解決辦法。

我們需要的圖像保存到文件夾的Drupal首先,通過在節點創建函數調用MODULENAME_save_image_local($網址):

$src_path = MODULENAME_save_image_local($url); 

添加此功能:

function MODULENAME_save_image_local($url) { 
    $hash = md5($url); 
    $dir = file_create_path('lastfm'); 
    if (!file_check_directory($dir)) { 
    mkdir($dir, 0775, FALSE); 
    } 
    $hash = md5($url); 
    $cachepath = file_create_path('CUSTOMFOLDERNAME/'. $hash.'.jpg'); 
    if (!is_file($cachepath)) { 
    $result = eventpig_lastfm_fetch($url, $cachepath); 
    if (!$result) { 
     //we couldn't get the file 
     return drupal_not_found(); 
    } 
    } 
    return file_directory_path() .'/CUSTOMFOLDERNAME/'. $hash.'.jpg'; 
} 
/** 
* Api function to fetch a url and save image locally 
*/ 
function MODULENAME_fetch($url, $cachepath) { 
    if (!$url) { 
    return drupal_not_found(); 
    } 
    $result = drupal_http_request($url); 
    $code = floor($result->code/100) * 100; 
    $types = array('image/jpeg', 'image/png', 'image/gif'); 
    if ($result->data && $code != 400 && $code != 500 && in_array($result->Content-Type, $types)) { 
    $src = file_save_data($result->data, $cachepath); 
    } 
    else { 
    return drupal_not_found(); 
    } 
    return TRUE; 
} 
1
+1

測試後,其證實了這種方法,我們不能保存外部圖像的URL ..必須找到一種方式,以節省外部URL圖像文件的Drupal目錄先!任何建議? – Serjas 2012-01-31 16:25:25

+0

也許你可以對wget進行系統調用? '<?php系統('wget http://example.com/file>下載文件')' – HerrSerker 2012-01-31 16:56:35

7

Drupal 7爲您所做的大部分工作提供了一個函數:system_retrieve_file()。

應該有一個文件模塊函數用於根據URL將文件保存到Drupal,但至少該函數確實存在......只是沒有任何人會看。您可以使用帶有第三個參數TRUE的system_retrieve_file()來創建託管文件(調用file_save_data())。

用法,一個簡單的例子比Jav_Rock的,但基本上是相同的,只是跳過散列路徑裝飾性:

$url = 'http://example.com/picture.png'; 
    $directory = file_build_uri('custom_directory'); 
    if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) { 
    // If our directory doesn't exist and can't be created, use the default. 
    $directory = NULL; 
    } 
    $file = system_retrieve_file($url, $directory, TRUE);