This is a 4.7 friendly version of the image upload method here: http://drupal.org/node/30944. 4.7 enables much simpler code, making image.module do most of the work.
My task was to add an image upload field to a custom 'news' node - a variation on the standard story node. The function names here use those from my module (news), but these will of course need to be changed to your module references (i.e. mymodule_insert() or whatever).
STEP 1: Add the file upload field to the node's form
Insert the following line into hook_form()
$form['image'] = array('#type' => 'file', '#title' => t('Image'), '#description' => t('Click "Browse..." to select an image to upload.'));
STEP 2: Handle the submitted form's new image field using the image.module.
Create or edit hook_update() and hook_insert(). These use functions from image.module to upload the image and create the 'preview' and 'thumbnail' versions, then associate the image files with this node.
function news_update(&$node) {
if(function_exists('image_prepare') && function_exists('image_update')){
image_prepare($node, 'image');
image_update($node);
}
}
function news_insert(&$node) {
if(function_exists('image_prepare') && function_exists('image_insert')){
image_prepare($node, 'image');
image_insert($node);
}
}
STEP 3: Load the node's associated image data into $node