Fix gmagick memory leak

We need to unset gmagick instance to free memory. And unset layers instance because it contains reference to gmagick too.
Maybe we should add a method on the API interface to explicitly free memory. This way users won't need to do `instanceof` checks etc. to conditionally free the resources...?

//cc @romainneutron