[英]Programmatically save image from external url - drupal
I am trying to create a node using last.fm. 我正在尝试使用last.fm创建一个节点。
$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
Now the node is created, but no image and getting the message 'The selected file 502025.jpg could not be saved. 现在创建了该节点,但是没有图像并显示消息“无法保存所选的文件502025.jpg。 The file is not a known image format.' 该文件不是已知的图像格式。
Drupal 7 provides a function for much of what you are doing: system_retrieve_file(). Drupal 7为您正在执行的大部分工作提供了一个功能:system_retrieve_file()。
There ought to be a file module function for saving a file to Drupal based on a URL, but at least the function does exist... just not where anyone will look. 应该有一个文件模块功能,用于基于URL将文件保存到Drupal,但至少该功能确实存在……只是没有人会看到。 You can use system_retrieve_file() with a third parameter of TRUE to create a managed file (calling file_save_data()). 您可以将system_retrieve_file()与第三个参数TRUE一起使用来创建托管文件(调用file_save_data())。
Usage, a simpler example than Jav_Rock's but substantially the same, just skipping the hash path fanciness: 用法,这是一个比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);
Found the solution. 找到了解决方案。
We need to save image to drupal folder first, by calling MODULENAME_save_image_local($url) in node creation function: 我们需要先通过在节点创建函数中调用MODULENAME_save_image_local($ url)将图像保存到drupal文件夹:
$src_path = MODULENAME_save_image_local($url);
add this function: 添加此功能:
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;
}
Maybe it depends on url_fopen? 也许这取决于url_fopen?
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen http://www.php.net/manual/zh/filesystem.configuration.php#ini.allow-url-fopen
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.