3 comments

@leymannx 2019-07-27 16:48:59

Drupal 8

For Drupal 8 it would go like following. First add the file and get a file ID. Then create the node and attach the given file ID.

$data = file_get_contents(__DIR__ . '/images/my_image.jpeg');
$file = file_save_data($data, 'public://my_image.jpeg');

$node = \Drupal\node\Entity\Node::create([
  'type'             => 'page',
  'title'            => 'Foobar',
  'field_my_image' => [
    'target_id' => $file->id(),
    'alt'       => 'Lorem ipsum',
    'title'     => 'Dolor sit amet',
  ],
]);

$node->save();

@Yuseferi 2012-09-20 05:02:13

Let suppose your image fields in field_body_images ,

first load your node by node_load and record your image in file tables and add it to your node image field I hope this sample help you to achieve :

$n=  node_load($nid);
$file = new stdClass();      
$file->filename =$file_name;
$file->filemime =file_get_mimetype($localimagepath.$file_name);
$file->filesize = @filesize(file_create_path($localimagepath.$file_name));
$file->uid = $user->uid;
$file->status = 1;
$file->timestamp = time();
$file->list=1;
$file->data=array('alt'=>'','title'=>$n->title);
drupal_write_record('files', $file);
$record->fid=$file->fid;
$n->field_body_images[]=(array)$file;
node_save($n);

@paul-m 2013-01-01 18:31:58

There's some code to demonstrate this in devel_generate: drupalcontrib.org/api/drupal/… Devel_generate of course generates dummy content for Drupal sites, including images. It's part of the Devel package: drupal.org/project/devel

@Neograph734 2015-12-13 11:24:54

Do not forget to add file_usage_add($file, 'file', 'node', $n->nid); in the end to prevent the file from accidental deletion.

@Yuseferi 2015-12-13 13:12:53

@Neograph734 it will add when node_save getting triggered.

@Yuseferi 2015-12-13 13:14:46

@Neograph734 you're welcome ;). tnx for your attention

@NenadP 2012-09-19 18:33:32

May be not exactly what you need, but why not just add a reference to an image in a field, like your example 2 suggests ? And then in content template file render that field as an image.

In node creation stream:

$node->field_image['und'][0]['value'] = "/path_to_image/image.jpg";

In content type:

<?php
global $base_url;
$image_source_link=$base_url . $node->field_image['und'][0]['value'];
?>
<img src="<?php print($image_source_link); ?>" />

Related Questions

Sponsored Content

1 Answered Questions

Creating nodes from json including image field upload

  • 2015-08-30 08:27:27
  • Ashkar A.Rahman
  • 660 View
  • 1 Score
  • 1 Answer
  • Tags:   7 media nodes

1 Answered Questions

Duplicate entry/Contraint violation 1062 in file_managed on browser crash

  • 2014-10-20 10:42:07
  • AgA
  • 1257 View
  • 0 Score
  • 1 Answer
  • Tags:   7 media

1 Answered Questions

[SOLVED] REST Export Views and images alternative text

  • 2019-05-17 15:00:35
  • MrUpsidown
  • 102 View
  • 0 Score
  • 1 Answer
  • Tags:   views 8 nodes json

1 Answered Questions

[SOLVED] How to solve the error while uploading image to nodes?

  • 2015-11-19 17:45:54
  • herci
  • 208 View
  • 1 Score
  • 1 Answer
  • Tags:   7 media

1 Answered Questions

[SOLVED] How to resize an image programmatically when creating node?

  • 2012-05-23 15:30:35
  • dm03514
  • 1487 View
  • 3 Score
  • 1 Answer
  • Tags:   7 media

Sponsored Content