Cita: The imagepng() outputs a GD image stream (image) in PNG format to standard output (usually the browser) or, if a filename is given by the filename it outputs the image to the file.
Es decir, si le pasas un nombre de fichero en lugar de mostrorlo lo guarda en el fichero.
Saludos.