#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    258
    Rep Power
    13

    Create Images from database via PHP?


    I know how to dynamically create an image via php like when someone fills out a form and that then posts to a new page which is just the image... Now I'm trying to have something where the images are dynamically created but by pulling the data from a database.

    I see a couple of issues here...:
    1) Since the page the image is created on is ONLY meant for the image, i.e.
    Code:
    Header ('Content-type: image/jpeg');
    that can't happen since if nothing else, there's going to be all different content on the page...so I'm not sure how you do that.
    2) Also, if I want to create a different image for each row in a database, how would I go about pulling the data and displaying it (I can do just this part already like one normally would to display rows of data) but now I'd need to not only be able to dynamically create an image on a page with other types of content (see #1) but now I'd need multiple image creations for each row.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    1. Images need their own URL. That can be the exact same PHP script but with some special query string (so it knows to send an image and not the regular HTML it would have sent) or it can be some other script dedicated just to these images.

    2. With that out of the way you only have to focus on creating one image at a time. If a row has multiple images then the URL needs to include something so the destination script knows which one to show. Then the original script which shows the page outputs an <img> for each image to show.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    258
    Rep Power
    13
    Originally Posted by requinix
    1. Images need their own URL. That can be the exact same PHP script but with some special query string (so it knows to send an image and not the regular HTML it would have sent) or it can be some other script dedicated just to these images.

    2. With that out of the way you only have to focus on creating one image at a time. If a row has multiple images then the URL needs to include something so the destination script knows which one to show. Then the original script which shows the page outputs an <img> for each image to show.
    Not sure I understand how to even attempt that first part (I plead php/mysql n00b )... as for the second part, each row would only have one image...the same 'skeleton' image that would be used for all rows.

    ?
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    You need a URL to put in the IMG tag, and that URL has to output an image. No HTML, only one image. As long as you follow that rule then you can do anything you want.

    So you can write a (small) PHP script that takes information from $_GET and outputs the image. What does the script need to get the image? Probably some kind of ID. So
    Code:
    <img src="/path/to/script.php?id=123" />
    PHP Code:
    <?php

    if (!isset($_GET["id"])) {
        
    // id is missing. do something, like maybe
        
    header("HTTP/1.1 404 Not Found");
        exit;
    }

    $id $_GET["id"];
    if (!
    ctype_digit($id)) {
       
    // invalid id. do something, like maybe
       
    header("HTTP/1.1 404 Not Found");
       exit;
    }

    // use the $id to look up whatever information you need to make the image
    // if there's nothing under that $id then do something, like maybe 404

    // when you're ready,
    header("Content-Type: image/jpeg");
    // and output the image data

    ?>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    258
    Rep Power
    13
    Originally Posted by requinix
    You need a URL to put in the IMG tag, and that URL has to output an image. No HTML, only one image. As long as you follow that rule then you can do anything you want.

    So you can write a (small) PHP script that takes information from $_GET and outputs the image. What does the script need to get the image? Probably some kind of ID. So
    Code:
    <img src="/path/to/script.php?id=123" />
    PHP Code:
    <?php

    if (!isset($_GET["id"])) {
        
    // id is missing. do something, like maybe
        
    header("HTTP/1.1 404 Not Found");
        exit;
    }

    $id $_GET["id"];
    if (!
    ctype_digit($id)) {
       
    // invalid id. do something, like maybe
       
    header("HTTP/1.1 404 Not Found");
       exit;
    }

    // use the $id to look up whatever information you need to make the image
    // if there's nothing under that $id then do something, like maybe 404

    // when you're ready,
    header("Content-Type: image/jpeg");
    // and output the image data

    ?>
    So I was messing around with things and I've managed to display the image per row...my issue is that it's not filling the image with the database information...here is my php script for the image
    PHP Code:
    $line1 $guest['name'];
    $line2 $guest['place'];


    // create an image of the right background and check size
    $im imagecreatefromjpeg ('images/myimage.jpg');

    $width_image ImageSX($im);
    $height_image ImageSY($im);

    // Our images need an 18 pixel margin in from the edge image
    $width_image_wo_margins $width_image - (18);
    $height_image_wo_margins $height_image - (18);

    // Work out if the font size will fit and make it smaller until it does 
    // Start out with the biggest size that will reasonably fit on our buttons
    $font_size 24;
    $font_size1 24;
    // you need to tell GD2 where your fonts reside
    putenv('GDFONTPATH=/home/usr/fonts');
    $fontname 'arial.ttf';
    $fontname1 'arial.ttf';

    do
    {
      
    $font_size--;

      
    // find out the size of the text at that font size
      
    $bbox=imagettfbbox ($font_size0$fontname$line1);

      
    $right_text $bbox[2];   // right co-ordinate
      
    $left_text $bbox[0];    // left co-ordinate
      
    $width_text $right_text $left_text;  // how wide is it?
      
    $height_text abs($bbox[7] - $bbox[1]);  // how tall is it?
       

    while ( 
    $font_size>&&
           ( 
    $height_text>$height_image_wo_margins ||
             
    $width_text>$width_image_wo_margins )
          );

    if ( 
    $height_text>$height_image_wo_margins ||
         
    $width_text>$width_image_wo_margins 
    {
      
    // no readable font size will fit 
      
    echo 'Text given will not fit on image.<br />';
    }
    else
    {
      
    // We have found a font size that will fit
      // Now work out where to put it

      
    $text_x $width_image/2.0 $width_text/2.0;
      
    $text_y $height_image/2.4 $height_text/2.4 ;

      
      
    $text_x1 $width_image/13.2 $width_text/13.2;
      
    $text_y1 $height_image/1.5 $height_text/1.5 ;
      
      
      if (
    $left_text 0)
          
    $text_x += abs($left_text);    // add factor for left overhang

      
    $above_line_text abs($bbox[3]);   // how far above the baseline?
      
    $text_y += $above_line_text;        // add baseline factor 
      
      
    $text_y -= 2;  // adjustment factor for shape of our template

      
    $white ImageColorAllocate ($im000);
      
    ImageTTFText ($im$font_size0$text_x$text_y$white$fontname,
                    
    $line1);
      
    ImageTTFText ($im$font_size0$text_x1$text_y1$white$fontname,
                    
    $line2);

    Header ('Content-type: image/jpeg');
      
    ImageJpeg ($im);
    }

    ImageDestroy ($im);
    ?> 
    Now I then call it with
    Code:
    <img src="myimage.php" alt="myimage" width="200 px" height="200 px">
    .

    On my script on my main page where I'm calling it, I have already queried my database and have an array so for instance if I just wanted to spit out the text without the image script I have
    PHP Code:
    <? echo $guest['name']; ?><br>
    <? echo $guest['place']; ?>
    and it lists each row...so if you look at the very beginning of my image script, I tried to reference those with the $line1 and $line2 but it's not displaying the text so I obviously am missing something and I'm not sure if it's in the image script or my main page script?
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    Originally Posted by mytwocents
    On my script on my main page where I'm calling it,
    No. You're not calling it from that PHP script. That script finished running and is over. There's nothing left of it. No variables. $guest is long gone.

    myimage.php has to start all over again. You need to tell it where it can find the information it needs. For example, the ID number thing I mentioned.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    258
    Rep Power
    13
    Originally Posted by requinix
    No. You're not calling it from that PHP script. That script finished running and is over. There's nothing left of it. No variables. $guest is long gone.

    myimage.php has to start all over again. You need to tell it where it can find the information it needs. For example, the ID number thing I mentioned.
    OK...yes, I see what you're saying... thanks for bearing with me here

    So now I'm calling it with:
    Code:
    <img src="myimage.php?id=<? echo $guest['id']; ?>" alt="image" width="752 px" height="553 px">
    And now I've got the php snippet like you suggested in the image.php file... but I'm not sure how to pull the rest of the fields into the image...so far all I can get it to print is the id number.

    EDIT: Initially it was only displaying the image with the id for the first row...I've now managed to figure that part out but the rest is still ?
    Last edited by mytwocents; January 28th, 2013 at 10:29 PM.
  14. #8
  15. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    The first script must have gotten that ID from somewhere, right? Use that in the second script to find the rest of the information.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    258
    Rep Power
    13
    Originally Posted by requinix
    The first script must have gotten that ID from somewhere, right? Use that in the second script to find the rest of the information.
    Well I've tried a few different versions of this:

    PHP Code:
    $name mysql_query("select name from mytable where id = $id"); 
    and I try to print name, I get nothing...?

IMN logo majestic logo threadwatch logo seochat tools logo