imagejpeg() creates an image [file], just like a .jpg file. So you can save this to a file and load multiple files into a page, or you can output them directly using a php script, one php script per image loaded. These PHP scripts (with the proper headers and only image data ouput) then act like .jpg files-- you can't have any HTML in them.
So your main file will use something like this:
Code:
<img src="/myphpimg.php?somevar=someval" />
The best way will be to output the images directly rather than saving them as files since that might not work out with timing in the browsers, and you'd have to dynamically select a lot of files. (Of course in some cases, such as a thumbnail gallery, that can be a better approach, though.) Note that any method not involving saving directly to a file or outputting directly to the user (such as storing the image data in a var for any reason, maybe as an email attachment) requires an output buffer; there's no way around this as far as I know.
There is one strange way to actually output the image directly into the html and that's using a data URI image:
http://en.wikipedia.org/wiki/Data_UR..._CSS_using_PHP
http://www.websiteoptimization.com/s...inline-images/
I recently wanted to do this because I was generating a number of very small image files (graphs) to go with dynamic HTML content. (I was creating a results page for a survey, not something that would be loaded by others.) If you do use this, be aware of bandwidth, etc. It's not the best solution unless you have very small images that are always generated dynamically. (Generally speaking, caching is always a good idea anyway, if the images might not change.)
Here's some code from my project:
PHP Code:
<?php
function data_uri($data, $mime) {
$base64 = base64_encode($data);
return ('data:' . $mime . ';base64,' . $base64);
}
$graph = imagecreatetruecolor(600,300);
///...generate graph code removed...///
ob_start();
imagepng($graph);
$graphimg = ob_get_clean();
imagedestroy($graph);
?>
<img src="<?php echo data_uri($graphimg,'image/png'); ?>" alt="Graph" />
That will generate HTML like the following:
Code:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEU..*..ElFTkSuQmCC" alt="Graph" />
<!-- *a lot of extra image data code removed above * -->
Finally, be aware that the order of the arguments in the final URI (the data and encoding) actually are required to be in two different orders varying by browser. So that will work in Firefox at least, but it won't necessarily work for everyone. In short, don't use that unless you have a really good reason. But it's fun to play with
Bookmarks