You are here

function image_create_node_from in Image 5

Same name and namespace in other branches
  1. 5.2 image.module \image_create_node_from()
  2. 6 image.module \image_create_node_from()

Function to other modules to use to create image nodes.

Parameters

$filepath: String filepath of an image file. Note that this file will be moved into the image module's images directory.

$title: String to be used as the node's title. If this is ommitted the filename will be used.

$body : String to be used as the node's body.

$taxonomy: Taxonomy terms to assign to the node if the taxonomy.module is installed.

Return value

A node object if the node is created successfully or FALSE on error.

1 call to image_create_node_from()
image_import_form_submit in contrib/image_import/image_import.module

File

./image.module, line 1067

Code

function image_create_node_from($filepath, $title = NULL, $body = '', $taxonomy = NULL) {
  global $user;
  if (!user_access('create images')) {
    drupal_access_denied();
  }

  // Ensure it's a valid image.
  if (!($image_info = image_get_info($filepath))) {
    return FALSE;
  }

  // Make sure we can copy the file into our temp directory.
  $original_path = $filepath;
  if (!file_copy($filepath, _image_filename($filepath, IMAGE_ORIGINAL, TRUE))) {
    return FALSE;
  }

  // Resize the original image.
  $aspect_ratio = $image_info['height'] / $image_info['width'];
  $size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio);
  if (!empty($size['width']) && !empty($size['height'])) {
    image_scale($filepath, $filepath, $size['width'], $size['height']);
  }

  // Build the node.
  $node = new stdClass();
  $node->type = 'image';
  $node->uid = $user->uid;
  $node->name = $user->name;
  $node->title = isset($title) ? $title : basename($filepath);
  $node->body = $body;

  // Set the node's defaults... (copied this from node and comment.module)
  $node_options = variable_get('node_options_' . $node->type, array(
    'status',
    'promote',
  ));
  $node->status = in_array('status', $node_options);
  $node->promote = in_array('promote', $node_options);
  if (module_exists('comment')) {
    $node->comment = variable_get("comment_{$node->type}", COMMENT_NODE_READ_WRITE);
  }
  if (module_exists('taxonomy')) {
    $node->taxonomy = $taxonomy;
  }
  $node->new_file = TRUE;
  $node->images[IMAGE_ORIGINAL] = $filepath;

  // Save the node.
  $node = node_submit($node);
  node_save($node);

  // Remove the original image now that the import has completed.
  file_delete($original_path);
  return $node;
}