Results 1 to 5 of 5

Thread: imagejpeg() question

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default imagejpeg() question

    How can I use the following to display multiple images on a page as well as text here or there on the page?
    Code:
    imagejpeg($image_p, null, 100);
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    james438 (05-04-2011)

  4. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    That makes sense. There is some good info here for me to play around with now.
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •