Use imagettftext() to print several things?

Discussion in 'PHP' started by Mysmasken, Jun 5, 2007.

  1. #1
    Can I have imagettftext() show more than one picture on a site?
    This is what my code looks like, and I just wanted to try the while() to see if it would print the picture twice with different fonts, but it won't.

    This is what comes up with the code I have.




    <?php
    // Requires config.php
    require('admin/config.php'); 
    
    //connect to mysql
    //change user and password to your mySQL name and password
    mysql_connect($host,$username,$password);
    	
    //select which database you want to edit
    @mysql_select_db($database) or die( "Unable to select database");
    //select the table
    $result = mysql_query("select * from publicfont WHERE letter='A' ORDER BY letter DESC LIMIT 2");
    $number = mysql_num_rows($result); 
    
    
    
    ### Declare this script will be displayed as a PNG image.
    header("Content-type: image/png");
    
    //grab all the content
       $n = 2;  
       while($n>0){  
    $r = mysql_fetch_array($result);
       $font=$r["filename"];
       $text=$r["fontname"];
    
    
    
    ####################### BEGIN USER EDITS #######################
    $imagewidth = 700;
    $imageheight = 150;
    $fontsize = "40";
    $fontangle = "0";
    $font = "fonts/" . $font;
    $text = $text;
    $backgroundcolor = "003366";
    $textcolor = "FFCC66";
    ######################## END USER EDITS ########################
    
    ### Convert HTML backgound color to RGB
    if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $backgroundcolor, $bgrgb ) )
    {$bgred = hexdec( $bgrgb[1] );   $bggreen = hexdec( $bgrgb[2] );   $bgblue = hexdec( $bgrgb[3] );}
    
    ### Convert HTML text color to RGB
    if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $textcolor, $textrgb ) )
    {$textred = hexdec( $textrgb[1] );   $textgreen = hexdec( $textrgb[2] );   $textblue = hexdec( $textrgb[3] );}
    
    ### Create image
    $im = imagecreate( $imagewidth, $imageheight );
    
    ### Declare image's background color
    $bgcolor = imagecolorallocate($im, $bgred,$bggreen,$bgblue);
    
    ### Declare image's text color
    $fontcolor = imagecolorallocate($im, $textred,$textgreen,$textblue);
    
    ### Get exact dimensions of text string
    $box = @imageTTFBbox($fontsize,$fontangle,$font,$text);
    
    ### Get width of text from dimensions
    $textwidth = abs($box[4] - $box[0]);
    
    ### Get height of text from dimensions
    $textheight = abs($box[5] - $box[1]);
    
    ### Get x-coordinate of centered text horizontally using length of the image and length of the text
    $xcord = ($imagewidth/2)-($textwidth/2)-2;
    
    ### Get y-coordinate of centered text vertically using height of the image and height of the text
    $ycord = ($imageheight/2)+($textheight/2);
    
    ### Declare completed image with colors, font, text, and text location
    imagettftext ( $im, $fontsize, $fontangle, $xcord, $ycord, $fontcolor, $font, $text );
    
    ### Display completed image as PNG
    imagepng($im);
    
    ### Close the image
    imagedestroy($im);
    
    
          	   $n = $n-1;
    	       }
    ?> 
    
    PHP:
     
    Mysmasken, Jun 5, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    You are using PHP to generate an image, so technically, you can't have two images in one. You can have the text twice on the same picture but I don't think that is what you want.

    Alternatively, you can loop but save the images to filesystem or somewhere and then retrieve the 2 images.
     
    krt, Jun 5, 2007 IP
  3. Mysmasken

    Mysmasken Active Member

    Messages:
    247
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Mysmasken, Jun 5, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Each one of those is a separate image. So, say you have a PHP script that generates an image like the one in your first post, what you can do is call that script repeatedly with paramaters, e.g. generate_image.php?font=Verdana

    Although in the case of urbanfonts.com, they probably have cached versions of the images stored statically anyway.
     
    krt, Jun 5, 2007 IP