Friedrich
Friedrich

Reputation: 2300

How to return an image created with php

I want to return an image when a PHP script is called.

The problem I am having is, that I have the image saved in a variable ($image).

I know I could first save the image on the server with

imagepng($image, 'image.png');

and after that return it with something like

readfile('image.png');

but I am sure there is a more elegant way to do the job, I just don't know it. Is there a way to combine these two commands or any other way, that allows me not to use a temporary file?

Upvotes: 0

Views: 41

Answers (1)

Marc B
Marc B

Reputation: 360882

imagepng($image), with no second parameter, will simply dump the raw PNG data out to the client (e.g. the browser). No need to write it to a file, then read that file back in.

This is clearly documented in the man page: http://php.net/imagepng and applies to ALL of the "save" functions in GD.

Upvotes: 2

Related Questions