Improved the script of img function