View Full Version : imagejpeg() question
james438
05-04-2011, 03:32 AM
How can I use the following to display multiple images on a page as well as text here or there on the page?
imagejpeg($image_p, null, 100);
djr33
05-04-2011, 03:50 AM
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:
<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_URI_scheme#Inclusion_in_HTML_or_CSS_using_PHP
http://www.websiteoptimization.com/speed/tweak/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
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:
<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 :)
james438
05-04-2011, 05:28 AM
I'm probably on the wrong track, but are you saying that the image is retrieved as raw data and then converted back into an image? Is this processor heavy? It's ok if it is. Just curious.
djr33
05-04-2011, 05:51 AM
A PHP GD Image object is a particular type of data, yes. That can be generated in a few ways, such as reading from an existing image or 'drawing' on the image using various functions.
Then it can be output as a file, output as raw data to the browser (as a dynamic 'image file', using jpeg-type headers), or it can be saved (using an output buffer) to a variable and, for example, output as a data-type URI and placed directly into HTML.
Think of PHP as a generating language-- it usually generates HTML, but it can generate other text formats like CSS or JS, with the proper headers of course, and even non-text formats like images, audio or flash. But when it is not generating HTML, it must act as a separate file, embedded just as a normal external file would be.
james438
05-04-2011, 06:02 AM
That makes sense. There is some good info here for me to play around with now.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.