1

Possible Duplicates:
Want to render an image without saving it to disk using PHP GD libs
The image cannot be displayed because it contains errors.

I'd like to call a function that make a image trought the img src tag. Is it possible?

I mean : instead of call <img src="path/file.php" /> I'd like to do somethings like <img src="function()" />

2
  • Technically, it is possible using data: URI's but it's a terrible idea. Go with what @Sjoerd suggests Commented Mar 11, 2011 at 16:05
  • 1
    See a possible solution at stackoverflow.com/questions/3385982/… Commented Mar 11, 2011 at 16:07

3 Answers 3

3

PHP is server side; It can generate either a base64_encoded image result which can be placed as an image, or you can point to a php script that will generate an image. But, regarding client side, it won't work.

So, you could do the following:

// the browser will make a call to your generator to render an image back
echo '<img src="myimagegenerator.php" />';

// src will be something like "data:image/png;base64,..."
echo '<img src="'.generateImage().'" />';
Sign up to request clarification or add additional context in comments.

1 Comment

@Gordon: Semantics, but done. ;-)
2

In HTML5 you can put the base64 encoded image source in an image tag. You will just need a function to return that.

function getImage($file){
      return 'data:image/gif;base64,' . base64_encode(file_get_contents($file));
}

Your img tag:

<img src="<? echo getImage('path-to-image.gif'); ?>" />

Comments

0

No.

However, you can use the URL "thispage.php?makeimage=1" and call the function and output an image if $_GET['makeimage'] contains 1.

1 Comment

-1 because it is possible with data URIs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.