You need to increase your script memory by using this:
You need to use this line at the top of file.
By increasing memory_limit your script will take unlimited memory usage of server.
If you have access on php.ini file than you can increase limit from php.ini file otherwise you need to add this line.
If you think unlimited memory will be effected on other areas than you can set custom value as:
ini_set('memory_limit', '512M'); //increase size as you need