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()" />

Community
  • 1
  • 1
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • Technically, it is possible using `data:` URI's but it's a terrible idea. Go with what @Sjoerd suggests – Pekka Mar 11 '11 at 16:05
  • 1
    See a possible solution at http://stackoverflow.com/questions/3385982/the-image-cannot-be-displayed-because-it-contains-errors/3386050#3386050 – Gordon Mar 11 '11 at 16:07

3 Answers3

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().'" />';
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
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'); ?>" />
Hassaan Salik
  • 413
  • 4
  • 22
konsolenfreddy
  • 9,551
  • 1
  • 25
  • 36
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.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175