Category Archives: Development

Ruby on Rails Image Manipulation with ImageMagick and Mini-Magick on OS X

I’m working on a Ruby on Rails project that requires users to be able to upload images. At the same time, we want to resize images so that they appear as we want them to on site. To do this, I found that the best solution would be to use the Ruby Gem Mini-Magick which uses ImageMagick. The benefits of using Mini-Magick is that its lighter weight that just using RMagick. So away I went…

I develop my Rails applications on OS X, so I thought I might have most of the stuff already that I’d need since XCode was installed. I had ImageMagick installed from a previous PHP project I had worked on, so I figured I’d just install the Ruby Gem Mini-Magick.. I did and hooked it up to my application. When running it though, it didn’t work. Bummer. I kept getting cryptic errors like:

ImageMagick command (identify "/tmp/minimagick99445-0.jpg") failed: Error Given 256

Great, what does that mean? So I did some sleuthing on Google and didn’t have much luck for about an hour or so. Then I came across this post at MattKing.org. How could I have missed this? A no brainer that you need to have the image libraries installed before compiling ImageMagick (and thus mini-magick).

So with out further ado, here are the steps you need to perform to get this to work!

Step One – Download Image Libraries

Before compiling and installing ImageMagick, you need to have all of the image libraries installed for the image types you want to manipulate. In my case, I wanted to support JPG, GIF, PNG, and TIFF. So I had to install the following libraries:

Once you have those downloaded, you can go on to Step 2

Step Two – Install Image Libraries

Now that you have all of the image libraries downloaded, its time to install them. Extract them using tar and install them!

JPEG

tar xzvf jpegsrc.v6b.tar.gz
cd jpeg-6b
./configure
make
sudo make install

LibPNG

tar xzvf libpng-1.2.24.tar.gz
cd libpng-1.2.24
./configure
make
sudo make install

GifLib

tar xzvf giflib-4.1.4.tar.gz
cd giflib-4.1.4
.configure
make
sudo make install

LibTiff

tar xzvf tiff-3.8.2.tar.gz
cd tiff-3.8.2
.configure
make
sudo make install

Step Three – Download & Install ImageMagick

With all of the image libraries installed, you can download ImageMagick. I installed mine to the /usr/ directory, but I think /usr/local/ would work as well.

tar xzvf ImageMagick-6.3.8-9.tar.gz
cd ImageMagick-6.3.8
./configure --prefix=/usr/
make
sudo make install

Step Four – Install ImageMagick

You’re almost there! Now that you have all of the image libraries installed and ImageMagick installed, you can go ahead and install the Ruby gem Mini-Magick

sudo gem install mini_magick

One note that I found very helpful on Matt King’s blog post was to check to make sure the configure script for ImageMagick could find the image libraries you wanted to be able to use. Check the output at the end of the configure script and make sure yes is present next to each image library you want to use.

You should be ready to rock now! To use mini-magick, insert the following lines into your environment.rb file:

require 'rubygems'
gem 'mini_magick'
require 'mini_magick'

Now that you’re all set up, you can load up a file and do whatever you want to it using Mini-Magick. Perhaps something like this!

filename = "path to your file"
fileout = "path to output file"
image = MiniMagick::Image.from_file(filename)
image.resize "120x120"
image.write(fileout)

There you go! If something doesn’t work, the first thing I’d check is to make sure all of the libraries and ImageMagick were installed properly. Also check file permissions in case for some reason the directory you’re trying to save your file is locked down so the web server user can’t write to it.

One more side note before I finish. I was able to use these exact steps to install ImageMagick and Mini-Magick on my Debian server which I’m using for my public test environment. So, that cool thing about using these steps installing everything from source allows you to duplicate on other *NIX systems.

Escalate Retail’s Ecometry Support For Endicia (USPS)

Endicia

Escalate Retail

We recently wanted to get a better handle on shipping packages for YumDrop via USPS. Ecometry has support for USPS, but only using what they call an Indicia Permit stamp. The problem with this is the minimums the USPS requires you to meet in order to use this. You have to ship at least 50lbs or 200 pieces to even qualify to ship this way. You also have to go through a lengthy verification process and drop the packages off yourself. Not ideal.

So some reps from USPS came down to visit and suggested we use Endicia to ship via USPS. Its a low cost, easy to use solution. It’s a monthly subscription program starting at $9.95 and topping out at $34.95 per month. There are no meters to rent and it can run on any computer. All of the software is included with the monthly fee.

Cool! Next step, Ecometry integration. Well, no surprise, there isn’t one from Ecometry directly. So I called our USPS rep and talked to him about solutions. He said he’d call Endicia and see what we could do. Turns out Endicia has an import/export product called Endicia Galaxy that can pull data into Endicia and push it back out to Ecometry. Even better, you can use a bar code scanner to basically scan the barcode on a collate and it will auto-populate the shipping address fields in the Endicia DAZzle postage printing software.

It all basically works through and ODBC connection that you set up as part of the Endicia Galaxy installation program. Galaxy can pull the order and shipment information straight from Ecometry when you scan the package so that it can print the label automatically for you. Really cool!

The one gotcha here is that Galaxy can’t read from multiple tables, which is how Ecometry stores its data. What you can do is set up a SQL View to pull the data from. I found this document in the Ecometry Google Group thread on how to set this stuff up. It’s from Scott Lohmann of republicoftea.com, so I want to give him credit there. You can basically follow this to the T, however…

I did encounter one problem. Scott’s example view SQL code only pulls information from the CUSTOMER’s table. The issue here is if your order has a different ship-to address than bill-to address, then the wrong address gets printed on the label. Laura Smith from swell.com pointed me in the right direction. After you ship confirm a package, it will show up in the MANIFESTENTRIES table. In here will be all of the information regarding the package and where its going. So you can pull the information right from there! You can download a copy of the SQL code for this view here.

Now I’m left with a couple of issues to sort out. First, I want to post the actual shipping cost and any tracking information back to Ecometry. I think you can do this through a SQL view as well, I just haven’t figured it out. Also, it looks like ESM keeps a connection to the shipping scale open, so Galaxy can’t pull the package weight in directly. I have to enter it manually. And lastly, when you scan the package barcode, you have to remove the last digit. Its not part of the order number, but some flag Ecometry puts on the end there. A little annoying, but workable.

All in all, I’d say this works great. Way better than doing it by hand through usps.com or printing postage from a postage meter. You can just generate the labels as you ship confirm packages.

Setting Up Ecometry Shipping Methods

Setting up Ecometry shipping methods is something that trips me up every time I have to do it. Honestly, I think the problem is that there are just too many steps that aren’t clear enough. If you miss one thing, you’re hosed. Too bad Ecometry can’t (or isn’t able to) fix this obvious usability issue for whatever reason. They have more documentation than they used to via their support portal, but it still isn’t as good as it could be (it looks like a glorified README and not indexed by Google, thus not available on the world wide web, where information belongs).

So I’m going to do the best thing I can, and go over exactly how to do it here. This way I will have it documented and anyone else out there in Ecometry land can use it for reference. For the purpose of this example, I’m setting up a miscellaneous (MX) ship method. These same steps can really be followed for any other ship method group, i.e. UPS (UP), FedEx (FX), USPS (PS), etc. Anyway, here goes…

You’ll probably want to just do all of your data entry for the shipping stuff in VisualLink. Gather all of the company/division pairs that you want to set up. Remember, you’ll want to include company 00 and division 00 in your setup as well, just for defaults. Ecometry suggests this in case “something goes wrong” and a ship method cannot be found for your company/division pair. So, if I’m setting up a new MX ship method, say 07, we’d have the following for a company/division 03/31:

Company Division Ship Method
00 00 07
03 00 07
03 31 07

With this data in visual link, activate Warehouse Management under Warehousing and Shipping. Go into Ship Methods Entry and enter the information for the company/division/ship method combinations like you see in the table above. Once you’re done, we can move on to the next step.

As a side note, in our setup, I used ship method 07 for USPS Express Mail so we could integrate Endicia with one of our companies (more on that in a future post).

Now you can go into Shipper Data Entry. This is the tricky part. You need to set up the shipper data for each company/division/warehouse/ship method group (you’ll also have to include company division 00/00 as fall back defaults). If you already have miscellaneous ship methods set up, this part is probably done already, as it was for me. If not, you need to provide any shipping account, manifest, or server information required to set up the shipping method. So it would be best to have that all in front of you before you begin.

One other point of note is that even if you don’t use warehouse 01, you need to set it up for each ship method. We’re using warehouse 03 in our example here. So we’d have to set up warehouse 01 and warehouse 03.

Anyway, if I were to have to set this up, I would have used the following table of values to set up my shipper data:

Company Division Warehouse Ship Method Group
00 00 01 MX
03 00 01 MX
03 31 01 MX
00 00 03 MX
03 00 03 MX
03 31 03 MX

For a miscellaneous ship method group, you can just enter 1 for the Carrier ID and Manifest Numbers. For FedEx, you’d want to enter all of your FedEx account and server information (sometimes you have to enter P in the Carrier ID field to get the right fields to come up.). I’m sure UPS and other carriers are similar to FedEx.

Now, assuming you have all of that set up right, its time to set up Postage and Handling routines. Get our of Warehouse and Shipping and go back to the VisualLink start screen. Under Advertising and Sales, you want to get into Advertising Control. Once there, get into P & H Management. I’ve always wondered why this stuff is here in this module, but I think it has to do with the fact that P&H routines are tied to advertising offer codes.

You’re in P&H Management, so type in your company name. Its time to set up P&H routines, set up shipping zones, and zone charges.

At the P&H Management screen, type in your company then hit enter. Then active P&H Routines at the bottom of the screen.

P&H Routine Ship Method
00 07
07 07

where 07 is actually the P&H routine I already had set up for my company, but I needed to add my new ship method, 07. Save your changes and go back to the main P&H Management screen.

Now you need to set up Sec Ct. Zone values. Click on that at the bottom of the screen. For this ship method, 07, I need to set up values of 001 Z9Z 01, where the last 01 is the zone. Basically every location will be in zone 01 for this ship method. It took a while for these values to save in Visual Link, so be patient if you think that the program has hung up on you.

Now that you’re back at P&H Management again, you need to set up the Zone Charges for the ship method. Even if you don’t use weight zone charges, they need set up for the ship method to work. In my case, I had to set up 4 sets:

P&H Routine Ship Method Zone Page
00 07 01 00
00 07 01 01
07 07 01 00
07 07 01 01

Basically the table above means I’m setting up the weight zone charges for Page 0 & 1 for Zone 1 for my ship method 07 and P&H routines 00 and 07. Sound confusing? It is.

In my case, I just entered 0 for the first few values of each set because I wasn’t using weight zone charges. If you are, you’ll want to put actual charges in for those values.

That’s about it! Test out your ship methods. If you get errors, go back and check your work. If you get “Invalid Ship Method”, you probably forgot to set up a ship method properly or the proper P&H routine for that ship method.

Any questions or comments, fee free to do so. If you think I have something wrong here, let me know so I can fix it!