text to image ?

Discussion in 'PHP' started by chandan123, Nov 8, 2009.

  1. #1
    how do i make some texts as image file on the fly ?

    i have some data in db ( text format normally a text dump ) and i want that to convert to an image and display whenever a query is made

    any libraries/class files are there for this purpose :eek:
     
    chandan123, Nov 8, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    check out php GD library or simple google search will give you ample of help..

    PHP Text to Image
     
    mastermunj, Nov 8, 2009 IP
  3. chandan123

    chandan123 Prominent Member

    Messages:
    11,586
    Likes Received:
    578
    Best Answers:
    0
    Trophy Points:
    360
    #3
    this is not just string it can contain lot of utf-codes and its multiple lines too

    the \n are going to display as \n instead of newline with such examples
     
    chandan123, Nov 8, 2009 IP
  4. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #4
    Hi, I use this small script to generate a random number and then convert it to image :
    
    <?php
    session_start();
    // generate 5 digit random number
    $rand = rand(10000, 99999);
    // create the hash for the random number and put it in the session
    $_SESSION['image_random_value'] = md5($rand);
    // create the image
    $image = imagecreate(60, 30);
    // use white as the background image
    $bgColor = imagecolorallocate ($image, 255, 255, 255);
    // the text color is black
    $textColor = imagecolorallocate ($image, 0, 0, 0);
    // write the random number
    imagestring ($image, 5, 5, 8, $rand, $textColor);
    // send several headers to make sure the image is not cached
    // taken directly from the PHP Manual
    // Date in the past
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    // always modified
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    // HTTP/1.1
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    // HTTP/1.0
    header("Pragma: no-cache");
    // send the content type header so the image is displayed properly
    header('Content-type: image/jpeg');
    // send the image to the browser
    imagejpeg($image);
    // destroy the image to free up the memory
    imagedestroy($image);
    ?>
    
    PHP:
    You can use another function (or you can call a number from your database) instead of $rand = rand(10000, 99999); of course. Then use a html <img> tag and set <img src='path_to_the_abowe_script.php'>.
     
    dsignresponder, Nov 10, 2009 IP
    chandan123 likes this.