Attaching image files to nodes programmatically in Drupal 7
Solution 1
Here is an example code using which you can use with node_save
$filepath = drupal_realpath('misc/druplicon.png');
// Create managed File object and associate with Image field.
$file = (object) array(
'uid' => 1,
'uri' => $filepath,
'filemime' => file_get_mimetype($filepath),
'status' => 1,
);
// We save the file to the root of the files directory.
$file = file_copy($file, 'public://');
$node->field_image[LANGUAGE_NONE][0] = (array)$file;
`
Solution 2
An easier way:
$filename = 'image.txt';
$image = file_get_contents('http://www.ibiblio.org/wm/paint/auth/gogh/gogh.white-house.jpg');
$file = file_save_data($image, 'public://' . $filename, FILE_EXISTS_RENAME);
$node->field_image = array(LANGUAGE_NONE => array('0' => (array)$file));
Solution 3
This is what worked for me:
$file_temp = file_get_contents('public://someimage.jpg');
// Saves a file to the specified destination and creates a database entry.
$file_temp = file_save_data($file_temp, 'public://' . 'someimage.jpg', FILE_EXISTS_RENAME);
$node->field_page_image = array(
'und' => array(
0 => array(
'fid' => $file_temp->fid,
'filename' => $file_temp->filename,
'filemime' => $file_temp->filemime,
'uid' => 1,
'uri' => $file_temp->uri,
'status' => 1,
'display' => 1
)
)
);
Solution 4
Here's one extra bit that tripped me up for a while: this will attach the image to the node, and if you're adding the image then you're okay. However, if you're updating an image, and you care about displaying it on a page, then one extra step is needed before calling node_save():
image_path_flush($node->field_image['und'][0]['uri']);
This will regenerate all of that image's styles.
Solution 5
$node->field_image[LANGUAGE_NONE][0] = (array)$file;
I tried this with a multilingual site. It failed fairly... but horribly. I had to specify the language in question. Simply put, this worked instead:
$node->field_image['en'][0] = (array)$file;
Without it, the attached file was viewable in the 'view' screen but not in the 'edit' screen.
Related videos on Youtube
jantimon
https://github.com/jantimon/html-webpack-plugin Follow me: @jantimon
Updated on January 09, 2020Comments
-
jantimon over 4 years
Is it possible to add an image to a node programmatically?
-
feedbackloop about 12 years+1. For whatever reason, I also had to add
'display' => 1
to the $file array, or else I got a constraint violation that field_doc_file_display was NULL. This was for a file rather than an image, which might be the difference. -
Shyghar over 10 yearsThere is a way to add Alt or Title to the image whit this method?
-
AgA over 10 yearsIf the file is already referenced by a node, will I need to use file_copy to attach it to another node?
-
DragonYen almost 10 yearsI also had to add "'display' => 1" to get this to work.
-
Peter Lozovitskiy almost 10 yearsIf you don't want to copy file and just use already existent file (image) you may use $file = file_save($file); instead of $file = file_copy($file, 'public://');
-
rineez almost 9 yearsDrupal community documentation gives slightly different way for doing this. drupal.org/node/2301425 . It says
file_usage_add($file, 'file', 'node', $node->nid);
must also be done while doing this. -
rineez almost 9 yearsYou may also want to unlink the temporary file manually, if that is no longer needed. Otherwise it will accumulate to become a huge waste of storage space over time.
-
Brian over 6 yearsThere is no temporary file in this case. The image is being downloaded to memory only, then stored in its target destination.