Hi everyone,
I am writing my custom module that requires a user to upload a high res-image, convert the image to a thumbnail, and then display it in the node. With the displayed image, if a user hovers over the thumbnail it creates a zoomed in version of the high resolution image.

I am learning module development and need help as to how to successfully upload an image and display it on the page, drawing from image module. As far as I know, there is a function called image_create_node_from(). The snippet from image module is as follows:

function image_create_node_from($filepath, $title = NULL, $body = '', $taxonomy = NULL) {
  global $user;

  if (!user_access('create images')) {
    return FALSE;
  }

  // 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;
}

Problem is, how do i use it?? Where in my module, called imagezoom, should I call it? Any help GREATLY appreciated!

Comments

nevets’s picture

If I was implementing this I would take a different approach. I would start with image/file field (CCK fields) and add a new display option. You can look to image cache and lightbox2 that already add display options. A bonus to this approach is it works with views.

nimrod98’s picture

I think I'm getting closer. When I click create new Image Zoom content, I managed to insert the browse button to look for a file to upload. I checked the MySQL database too and it inserts information into 'file' and 'image' tables.

But when I submit it, it still does not show up in the body of the node. Do I implement a mysql database call in hook_load()? If so what should it be like? Will this append the image to the top of the body?

nimrod98’s picture

More Progress!!

I finally make it so that the image uploaded is displayed as a thumbnail, and when clicked on it displays the larger image, which is the image uploaded.

This is the source code of what it looks like in the browser:

<a href="http://localhost/drupal6/sites/default/files/images/PICT0002_1.JPG">
<img src="http://localhost/drupal6/sites/default/files/images/PICT0002_1.thumbnail.JPG" alt="" title=""  class="image image-thumbnail " width="75" height="100" /></a>

I'd like to know, if anyone can PLEASE help me, to change the link tag so that it has a class, called Myclass. For example:

<a href="http://localhost/drupal6/sites/default/files/images/PICT0002_1.JPG" class="Myclass">
</a>

My module which themes the body of the node is as such:

function theme_imagezoom_body($node, $size) {
 $label = IMAGE_ORIGINAL;
 
 return l(imagezoom_display($node, IMAGE_THUMBNAIL), file_create_url($node->images[$label]), array('html' => TRUE));
}

So to cut things short, is there a way, if I modify file_create_url() to ADD a class to the link instead of spewing out JUST the link? Any help greatly appreciated!!

nevets’s picture

Actually you want to change the call to l() which generates the link (file_create_url generates the url, not the link).
Something like this

return l(imagezoom_display($node, IMAGE_THUMBNAIL), file_create_url($node->images[$label]), array('html' => TRUE, 'class' => 'my_class'));
nimrod98’s picture

Yes nevets thank you!! I actually made a custom function out of that l (). Who in their right mind named a function L?? That's so misleading. Reading it I didn't realize it was a custom function located in the common.inc file (i think). That did the job for me. Thanks!