function social_content_get_image_file in Social Content 7
Fetches an external image and saves it to the files_managed table.
Helper function, used by modules implementing social content types.
Parameters
string $url: The url for where to grab the image from.
string $field_name: Machine name of the field this image will be attached to. Required to work out the uri_scheme.
string $min_resolution: (Optional) The min_resolution to check for.
Return value
object|bool Returns a file object ready to be attached to a field (or FALSE).
3 calls to social_content_get_image_file()
- social_content_facebook_post_callback in modules/
facebook/ social_content_facebook.module - social_content_instagram_post_callback in modules/
instagram/ social_content_instagram.module - social_content_twitter_post_callback in modules/
twitter/ social_content_twitter.module
File
- ./
social_content.module, line 429 - Social Content module.
Code
function social_content_get_image_file($url, $field_name, $min_resolution = NULL) {
if (!$url) {
return FALSE;
}
$result = drupal_http_request($url);
if ($result->code != 200) {
return FALSE;
}
// Find the image extension
// Facebook has generic /graph/picture urls that redirect.
// So look for a redirect url first in the response.
if (isset($result->redirect_url)) {
$path_info = pathinfo($result->redirect_url);
}
else {
$path_info = pathinfo($url);
}
// TODO: Create a nice filename from the content type.
$filename = $path_info['basename'];
$field_info = field_info_field($field_name);
$field_uri_scheme = $field_info['settings']['uri_scheme'];
$file = file_save_data($result->data, $field_uri_scheme . '://' . $filename, FILE_EXISTS_RENAME);
if (!$file) {
return FALSE;
}
if ($min_resolution) {
$validators = array(
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array(
0,
$min_resolution,
),
);
if (file_validate($file, $validators)) {
return FALSE;
}
}
// Add additional fields (required for adding to a file field).
return array(
'fid' => $file->fid,
'filename' => $file->filename,
'filemime' => $file->filemime,
'uid' => 1,
'uri' => $file->uri,
'status' => 1,
'display' => 1,
);
}