2012-01-31 125 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);