Juri's script results in resized but unclear image, the solution is to use imagecopyresampled() instead of imagecopyresized.

Hope that helped :)

More...