Attaching image files to nodes programmatically in Drupal 7

前端 未结 8 1514
耶瑟儿~
耶瑟儿~ 2020-12-07 14:07

Is it possible to add an image to a node programmatically?

相关标签:
8条回答
  • 2020-12-07 14:12

    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;
    `
    
    0 讨论(0)
  • 2020-12-07 14:17

    Just going to paste my solution here as well, I needed to create a new node, and upload an image programmatically.

    $filepath = variable_get('file_public_path') . '/xmas_banner.jpg';
    $file_temp = file_get_contents($filepath);
    $file_temp = file_save_data($file_temp, file_default_scheme() . '://' .'xmas_banner_nl.jpg', FILE_EXISTS_RENAME);
    
    $node = new stdClass();
    $node->type = 'carousel'; // custom content type
    $node->title = 'XMAS NL';
    $node->field_banner_image[LANGUAGE_NONE][0] = (array) $file_temp;
    $node->uid = 1;
    $node->status = 0;
    $node->active = 0;
    $node->promote = 0;
    node_save($node);
    
    0 讨论(0)
  • 2020-12-07 14:20

    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
        )
      )
    );
    
    0 讨论(0)
  • 2020-12-07 14:21

    $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.

    0 讨论(0)
  • 2020-12-07 14:24

    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));
    
    0 讨论(0)
  • 2020-12-07 14:29

    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.

    0 讨论(0)
提交回复
热议问题