I made some modifications on a site that used to handle image uploads with Node Images module, but I replaced it with CCK+FileField+ImageField combo. So I needed to migrate somehow the images from the Node Images module to a CCK image field, but I found nothing. So, to do this I wrote a little PHP snippet that does the work. The script copies the proper database records from the node_images table to the right place in the content_field_image and the files table.

I hope it might come in handy for someone. Use with high caution (backup your database!) and do the appropriate modifications to your site!

<?php
// access to the db server and database
$db_password = 'PASSWORD';
$db_user = 'USER';
$db_host = "HOST"; 
$db_name = "DATABASE";

// show error messages if something goes wrong
function error()
{
   if (mysql_error())
      die("Query failed: " . mysql_errno() . " : " . mysql_error());
}

// connect to the server
if (!($contact=mysql_connect($db_host, $db_user, $db_password))) error();
if (!mysql_select_db($db_name, $contact)) error();

// get the next auto_increment value from the files table
$next_increment = 0;
$status = "SHOW TABLE STATUS LIKE 'files'";
$statusresult = mysql_query($status) or die error();
$row = mysql_fetch_assoc($statusresult);
$next_increment = $row['Auto_increment'];

// get the proper fields from the node_images table which needs to be inserted in the files table
$result = mysql_query("SELECT uid, filename, filepath, filemime, filesize, status, timestamp FROM node_images") or die error();
while($row = mysql_fetch_array($result)) {
        $files_table[] = array(
			"uid" => $row['uid'],
			"filename" => $row['filename'],
			"filepath" => $row['filepath'],
			"filemime" => $row['filemime'],
			"filesize" => $row['filesize'],
			"status" => $row['status'],
			"timestamp" => $row['timestamp']
			);
}

// set the proper data into the files table
foreach($files_table as $f){
  mysql_query("INSERT INTO files VALUES ('','$f[uid]','$f[filename]','$f[filepath]','$f[filemime]','$f[filesize]','$f[status]','$f[timestamp]')");
}

// get the proper fields from the node_images table which needs to be inserted into our cck image table
$result = mysql_query("SELECT nid, weight, list, description FROM node_images") or die error();
while($row = mysql_fetch_array($result)) {
        $cck_table[] = array(
			"nid" => $row['nid'],
			"weight" => $row['weight'],
			"list" => $row['list'],
			"description" => $row['description']
			);
}

// set the proper data into our cck image field table
foreach($cck_table as $i){
  $length = strlen($i['description']); // get the lenght of the description for the images alt attribute
  $i['description'] = 'a:2:{s:3:"alt";s:'.$length.':"'.$i['description'].'";s:5:"title";s:0:"";}'; // build the field_image_data record
  mysql_query("INSERT INTO content_field_image VALUES ('$i[nid]','$i[nid]','$i[weight]','$next_increment','$i[list]','$i[description]')");
  $next_increment++;
}
?>

Comments

quicksketch’s picture

sgabe, this issue is featured on the project page of ImageField #201983: How To: Migrate from Image.module to ImageField Documentation Project. Could you update that issue with your approach?

quicksketch’s picture

Oh, apologies, I didn't see this was Node Images project, which is separate from Image module. Sorry about that! Thanks I'll add this to the ImageField handbook once I add one. :)

quicksketch’s picture

Status: Needs review » Fixed

I added a handbook page for migrating from other modules to FileField (http://drupal.org/node/432852). This page is added as a child (http://drupal.org/node/432858). Feel free to edit it as necessary (all users can edit handbook pages on Drupal.org).

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.