You are here

function image_create_node_from in Image 6

Same name and namespace in other branches
  1. 5.2 image.module \image_create_node_from()
  2. 5 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.

$keep_original: Boolean to indicate whether the original file should be deleted

Return value

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

3 calls to image_create_node_from()
ImageTestCase::testImageCreateNodeFrom in tests/image.test
Verify image_create_node_from() works like regular image node creation.
image_attach_validate in contrib/image_attach/image_attach.module
Image attach validation handler for node edit form.
_image_import_batch_op in contrib/image_import/image_import.pages.inc
Batch operation callback for image import.

File

./image.module, line 1057

Code

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

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

  // Ensure the file is within our size bounds.
  if ($image_info['file_size'] > variable_get('image_max_upload_size', 800) * 1024) {
    form_set_error('', t('The image you uploaded was too big. You are only allowed upload files less than %max_size but your file was %file_size.', array(
      '%max_size' => format_size(variable_get('image_max_upload_size', 800) * 1024),
      '%file_size' => format_size($image_info['file_size']),
    )), 'warning');
    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);

  // By default, remove the original image now that the import has completed.
  if ($keep_original !== TRUE) {
    file_delete($original_path);
  }
  return $node;
}