1

Is it possible to use imagecreatefrompng() with a php file that returns a dynamic png image?

eg.

<?php
 $IM = imagecreatefrompng('image.php?var=1');
?>

where image.php looks something like:

<?php
 // code to generate image
 header("content-type: image/png");
 imagepng ( $OUTPUT );
?>

At the moment I'm getting a "failed to open stream" error - can this be done? If not, are there any quick & easy workarounds? (that don't involve using image.php to save a .png file for the script to detect instead.)

Thanks,

Chris

1 Answers1

4

Query parameters ?var=1 work only when requesting a resource through http, not through the file system. To do this, you would have to specify a full URL:

<?php
 $IM = imagecreatefrompng('http://localhost/image.php?var=1');
?>

(If your PHP is configured to allow this)

However, the usually much more desirable way would be to include image.php directly and pass var to it using a normal variable. That saves you a http request that, even if made locally, spawns a new PHP process.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    Which will only work if the `php.ini` setting `allow_url_fopen` is enabled. http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen – Arc Jan 31 '11 at 16:09