Does anyone have an example that can create everything that is need to make a new node work properly with imagefield when using Drupal 5?

This works with the Image Module but not imagefield.

global $user;

$node = new StdClass();

$node->type = 'image';
$node->uid = 20;
      
$node->status = 1;
$node->promote = 1;
       
$node->title = 'This is the title for the content';
$node->body = 'The body of the node';
$node->teaser = 'The teaser of the node';
$node->comment = 2;

$node->images = array();

$node->file = $node->images['_original'] = "files/images/temp/lastmail.jpg";
_image_build_derivatives($node, TRUE);
$node->new_file = TRUE;

node_save($node);

I attempted to make it the CCK way but no luck so far.

            $node->field_img = array
            (
            0 => array
                (
                    'fid' =>  "upload", 
                    'title' => $file,    
                    'alt' => $file,    
                    'nid' => $node->nid,    
                    'filename' => $file,    
                    'filepath' => $dir.'/'.$file, 
                    'filemime' => 'image/jpeg',
                    'filesize' => filesize($dir.'/'.$file),      
                )
            ); 

Comments

uwe mindrup’s picture

I had the same problem and I did the "cck-way" in the following manner:

<php 
$node['field_bookcover'] = array  // Book-Cover
    (
        0 => array
            (
                'fid' => 'upload',
                'filename' => $imagename,
                'filepath' => $dir . '/' . $imagename,
                'filemime' => 'image/jpeg',  // the appropriate mimetype
                'filesize' => filesize($dir . '/' . $imagename)
            )
    );
?>

I put this code just before 'node_save($node)'.

quicksketch’s picture

Status: Active » Closed (duplicate)

drupal_execute is the usual way to insert data in a node form programmatically, unfortunately the file handling needs some additional help. See http://drupal.org/node/112802.

toxicdesign’s picture

@Uwe Mindrup

I dont understand how you can do it that way when $node needs->to->be->an->abject? Not array?

I'm still stumped on this...I think I'm going to just insert the data with a db_query instead of fighting with this node_save vs imagefield battle.

jmlavarenne’s picture

Before you save the node, you do this:

$node = (Object)$node;
voipfc’s picture

If you want to add more images to an existing node, how will you go about it?

jmlavarenne’s picture

You could do node_load(), add the images as suggested, node_save().

Artem Beloglazov’s picture

test variant:

$node = new StdClass();
$node->is_new = true; // if node is new
$node->uid = 1;
$node->type = 'node_with_img'; //Your content type
$node->changed = NULL;
$node->title = 'testTitle';
$node->body = 'testBody';	
$node->format = 1;
$node->log = NULL;
$node->name = 'admin';
$node->date = NULL;
$node->status = 1;
$node->promote = 0;
$node->sticky = 0;
$node->revision = 0;
$node->preview = 'testPreview';
$node->op = 'testOp';
$node->submit = 'testSubmit';
$node->menu= Array(
	'title'=>NULL,
	'description'=>NULL,
	'pid'=>1,
	'path'=>NULL,
	'weight'=>0,
	'mid'=>0,
	'type'=>6);
$node->field_img_upload = NULL;
$node->upload = NULL;
$file = '/your/path'; // Your filepath with write chmod 
$node->field_img= Array(
	0=>Array (
		'flags'=>Array(
			'delete'=>0
			),
		'alt'=>basename($file),
		'title'=>basename($file),
		'filename'=>basename($file),
		'filepath'=>$file,
		'filemime'=>'image/jpeg',
		'filesize'=>filesize($file),
		'fid'=>'upload',
		)
	);
$node->teaser = 'testTeaser';
$node->validated =	true;		
node_save($node);