If you are outputting a PNG directly in response to a client request it is important to check your web server configuration.
Some clients may request your images with a <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">accept header</a> of image/*. Default configurations of Apache and possibly other servers will by default NOT allow your script to run in response to this request.
Apache is specifically discussed at http://stackoverflow.com/q/19169337 but other server have been documented to have issue too.
In other words, when testing your application don't just use the web browser, consider a phone's browser and networking libraries which could send different headers.