In my current rails project I need to upload photos and save some exif data taken from them. I use attachment_fu as uploading system that let me choose which image processor to use. Using rmagick and mini_magick I can extract exif data with the following code:

# rmagick
image = Magick::ImageList.new(filename).first
puts image['EXIF:Model'] # The camera model used to take the picture

# mini_magick
image = MiniMagick::Image.from_file(filename)
puts image["EXIF:Model"]

The problem is that I can’t do the same thing with image_science, because it has no methods that return exif data, so I want to add a method to the ImageScience class to do that.
Looking the FreeImage documentation I found some helpful functions, FreeImage_GetMetadata and FreeImage_TagToString. With these 2 functions I’m able to get an exif tag and convert it to a readable string. Each one of the available tags belongs to one of the following meta models:

FI_ENUM(FREE_IMAGE_MDMODEL) {
  FIMD_NODATA         = -1,
  FIMD_COMMENTS       = 0,	// single comment or keywords
  FIMD_EXIF_MAIN      = 1,	// Exif-TIFF metadata
  FIMD_EXIF_EXIF      = 2,	// Exif-specific metadata
  FIMD_EXIF_GPS       = 3,	// Exif GPS metadata
  FIMD_EXIF_MAKERNOTE = 4,	// Exif maker note metadata
  FIMD_EXIF_INTEROP   = 5,	// Exif interoperability metadata
  FIMD_IPTC           = 6,	// IPTC/NAA metadata
  FIMD_XMP            = 7,	// Abobe XMP metadata
  FIMD_GEOTIFF        = 8,	// GeoTIFF metadata
  FIMD_ANIMATION      = 9,	// Animation metadata
  FIMD_CUSTOM         = 10	// Used to attach other metadata types to a dib
};

Ok, now I can extract the model of the camera:

FreeImage_GetMetadata(FIMD_EXIF_MAIN, bitmap, "Model", &tag);
printf(FreeImage_TagToString(FIMD_EXIF_MAIN, tag, NULL));

As you can see, I need to pass the model of the “Model” tag. But if I don’t know which model to use, I can loop through all of them until the returned value of the FreeImage_GetMetadata function is not NULL:

for(model = 0; model < 11; model++) {
  if(FreeImage_GetMetadata(model, bitmap, tagName, &tag))
    return rb_str_new2(FreeImage_TagToString(model, tag, NULL));
}

Finally I can write a ruby module that extends ImageScience and adds the ability to get an exif tag:

module ImageScienceExifData

  def [](key)
    if key =~ /^EXIF:(\w+)?/
      get_exif($1)
    end
  end

  inline do |builder|
    if test ?d, "/opt/local" then
      builder.add_compile_flags "-I/opt/local/include"
      builder.add_link_flags "-L/opt/local/lib"
    end
    builder.add_link_flags "-lfreeimage"
    builder.add_link_flags "-lstdc++" # only needed on PPC for some reason. lame
    builder.include '"FreeImage.h"'

    builder.prefix <<-"END"
      #define GET_BITMAP(name) FIBITMAP *(name); Data_Get_Struct(self, FIBITMAP, (name)); if (!(name)) rb_raise(rb_eTypeError, "Bitmap has already been freed")
    END

    builder.c <<-"END"
      VALUE get_exif(char *tagName) {
        GET_BITMAP(bitmap);
        FITAG *tag = NULL;
        const char *value;
        int model;

        for(model = 0; model < 11; model++) {
          if(FreeImage_GetMetadata(model, bitmap, tagName, &tag))
            return rb_str_new2(FreeImage_TagToString(model, tag, NULL));
        }

        return Qnil;
      }
    END

  end
end
ImageScience.send(:include, ImageScienceExifData)

ImageScience.with_image(filename) do |img|
  puts img["EXIF:Model"]
end

The output with the picture I used is the following :

NIKON
COOLPIX S3
2006:12:10 12:09:17

It doesn’t work with all the exif names but for now it’s ok for my needs. The next step is to add the code above in my rails application and use it with attachment_fu. I’ll write another post about that soon.

Some weeks ago Casper Fabricius sent me a patch for the Radiant Newsletter extension. He added a statistics system to track how many times sent emails are opened. I have finally found the time to apply it and make a commit to my repository. Thank you very very much for your work Casper! I’ll write an article about this extension as soon as possible.

I love to use prototype and scriptaculous to create Rich Internet Application, and I love all the built-in effects and libraries of script.aculo.us.
Today I was working with Sortable.create to provide a simple way to reorder images inside a gallery. I started using the same code used in the SortableListsDemo page of the official wiki. Each time the list is updated, the serialized list is sent to the server. The server receives the list and updates all the images positions. It’s pretty simple and works fine, but the problem is that the server executes a query for each image in the gallery. This means that if I have 100 images, 100 queries will be run. It’s a hell for my server! The best way to improve this action is to send to the server more detailed information, such as the dragged image, the old position and the new position. In this way I can execute only 2 queries for each movement. To do that I wrote a simple javascript class called LiteSortable that acts like the Sortable.create method. It receives the same parameters, and the same callbacks: onUpdate, onChange. The only change is that these 2 callbacks will receive more information about the movement, and I can improve the previous client side code with:

function sort(list, element, id, old_position, new_position) {
  new Ajax.Request('/admin/gallery_item/sort/', {
    evalScripts:true,
    parameters: {
      id: id,
      old_position: old_position,
      new_position: new_position
    }
  });
}

document.observe('dom:loaded', function() {
  new LiteSortable('list', {
    onUpdate: sort
  });
});

And the server code become like the following:

def sort
  old_position, new_position = params[:old_position].to_i, params[:new_position].to_i
  @item = GalleryItem.find(:first, :conditions => ["id = ? AND position = ?", params[:id], old_position])
  if @item
    x, y, z = old_position < new_position ? ["-", "<", ">"] : ["+", ">", "<"]
    GalleryItem.update_all("position = (position #{x} 1)", ["parent_id IS NULL AND gallery_id = ? AND position #{y}= ? AND position #{z}= ?", @item.gallery_id, new_position, old_position])
    @item.update_attribute('position', new_position)
  else
    @error = true
  end
end

If @error is true, it means that the dragged image it’s been deleted or moved by somone else.
If you want to use LiteSortable you can check out the entire project with:

svn co http://dev.gravityblast.com/svn/projects/js/LiteSortable/

…or save download the lite_sortable.js file from here.

I’m going to move all my projects to subversion. Now I’m moving the Gallery extension.The subversion repository is here:

http://dev.gravityblast.com/svn/projects/radiant/extensions/gallery/

You can check it out with

svn co http://dev.gravityblast.com/svn/projects/radiant/extensions/gallery/

The new version uses attachment_fu, and the filesystem organization is changed. In order to use the new version with galleries created by old versions of the Gallery extension you must run this rake task:

rake RAILS_ENV="production" radiant:extensions:gallery:version_0_7_0:upgrade_filesystem_structure

I suggest making a backup of all your pictures before running the above rake task. Let me know if everything works correctly!

CopyMove 1.9 is out!

November 29th, 2007

Now the CopyMove extension uses subversion, and you can check it out from its repository with:

svn co http://dev.gravityblast.com/svn/projects/radiant/extensions/copy_move/

Thanks to Benny Degezelle now it works with the shards extension. This is the best way to hack the Radiant admin UI! Benny also helped me updating the rake tasks and rewriting the CopyMove html structure so it is similar to the other admin screens…Thanks Benny :)I also moved all the code in a separated controller, wrote a complete test suite, and added the ability to choose the status for the copied pages.

GravityBlast respositories

November 19th, 2007

UPDATE: The new repository is based on subversion, and you can find it here.

I’ve just moved my repositories from darcs.bigchieflabs.com to darcs.gravityblast.com. Here you can find my projects, primarily extensions for radiant like Gallery, CopyMove, DefaultPageParts, Dynamic, Subscriber, Newsletter

Gallery extension 0.6.1 is out! Many people ask me how to implement a full gallery system, with nested galleries (or album of galleries), and final page for an image. I implemented some new features and here you can see a demo:

Gallery extension 0.6.1

I also fixed some javascript bugs in the import page. Now it should work also with safari! Let me know!

Gallery extension 0.5.0

May 31st, 2007

Radiant Gallery extension 0.5.0 is just released! Now you can set set name and description for each gallery and gallery item.

radiant-gallery-extension-0_5_0.jpg

I wrote some example in the README file, check it out!

CopyMove 1.8.3

May 26th, 2007

I fixed some bugs in the CopyMove extension. Now you can copy the whole tree of a page under one of its children…you can also duplicate the hompage..if you already use this extension, download the latest release!

I changed the name of my Radiant duplicate extension…now it’s called CopyMoveCheck it out!!!