Results 1 to 15 of 15

Thread: PHP/GD Dynamic Image

  1. #1
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts

    PHP/GD Dynamic Image

    I have a 480x60 (banner) which i would like to print random text strings on.

    Basically im wanting to create an image which reads 'random quotes' from a text file.

    Possible? How?

    also, does anyone have a link to one of those dyna-images where you put your name/message and it writes it on the image... like the sadam/soldier one back 'then' or the ones where 'you' love a fit bird holding a sign?

  2. #2
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts
    Code:
    <?PHP
    error_reporting(E_ALL ^ E_NOTICE);
    
    /*
    simplepng.php
    Generates a 800 x 300 pixel png of the color passed in parameter 'color'
    */
    
    // Step 1. Create a new blank image
    $im = imageCreate(800,300);
    
    imageLine($im,25,20,125,80,255);
    imageRectangle($im,0,0,599,299,255);
    imagestring($im, 5, 0, 0, "Hello world!", 255);
    
    // Step 2. Set background to transparent
    $background = imageColorAllocate ($im, 0, 0, 0);
    $background = imageColorTransparent($im,$background);
    
    // Step 3. Send the headers (at last possible time)
    header('Content-type: image/png');
    
    // Step 4. Output the image as a PNG
    imagePNG($im);
    
    // Step 5. Delete the image from memory
    imageDestroy($im);
    ?>
    Try that. I've put a few lines that it can draw in there too. This is basically the same as an example on a site somewhere. I modified it for my own purposes, but this is pretty much what was on the site. I don't yet know how to put an image beneath the text and stuff, as I having needed to find out for my purposes, but that should be a start .

    EDIT - found the site: http://www.nyphp.org/content/present...Dintro/gd9.php
    Last edited by ajbrun; 03-05-2006 at 06:00 PM.

  3. #3
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    this looks like a good place to start, however i cant seem to get it working

  4. #4
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts
    Code:
    <?php
    Header ("Content-type: image/png");
    $img_handle = imageCreateFromPNG("bannerboy.png");
    $color = ImageColorAllocate ($img_handle, 100, 100, 100);
    $ip = $_SERVER['REMOTE_ADDR'];
    ImageString ($img_handle, 3, 10, 9,  "Your IP: $ip", $color);
    ImagePng ($img_handle);
    ImageDestroy ($img_handle); 
    ?>
    I copied the image and saved it in the same directory as the php file is. The page can be found here for as long as my PC is on:

    http://ajbrun.homeip.net/ip_image.php

    Let me know how you get on .

  5. #5
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    yeah that works, but when i try to <img src="ip_image.php"> it no worky.

    now to get the blighter reading from Text, and/or reading from user entry text box :s


    http://www.vini.co.uk/dyn/image.php
    Last edited by Vini; 04-05-2006 at 02:50 PM.

  6. #6
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts


    Wel if it works here, then I don't see why it won't work in HTML tags...

  7. #7
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    anyone able to push me onto the next level, re: reading from mysql/text file.. and allowing people to add their own on the fly comments?

  8. #8
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts
    Yep.

    I'm not entirley sure why you'd want to do this kind of thing from a mysql database really. I'd either have text embedded into the php file itself, or have it on a separate txt file and use a php include. That will then act as if it really is embedded into the file. You'd then probably want to get it to find a random text string out of the list and put it into a variable to be used.

    It's even easier to get people to add their own comments also:

    $text = $_GET['text'];

    then in the URL, you'd do - picture.php?text=hello

    That will then store the string "hello" into the text variable which you can use. If someone has chosen to use a custom string, you won't want a random variable out of the list overwriting it, so if I were you, I'd do this:

    Code:
    //Puts the chosen text variable from the URL into $text if it's been chosen
    $text = $_GET['text'];
    
    //if the $text variable is NOT set
    if(!isset($text))
    {
    
    //code to pick a random string out of a list either in an include file or embedded and put the string into $text
    } else {
    //code to take the $text variable and output it onto the chosen picture etc
    }
    I've not looked at how to find a random string out of a list, but I'd guess it'd be similar to java where you put the strings into an array, then do something like arrayname(rand). www.php.net is very good for this .

  9. #9
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    having read, ive come up with:

    Code:
    <?php
    
    $file = $path . "quotes.txt"; // The file that stores the quotes. Path: relative to page that includes this one.
    
    $filecontent = fopen($file, "r"); // Open the file to read
    $content_quotes = fread($filecontent, filesize($file)); // Read the file into the variable $content_quotes
    fclose($filecontent); // Close the file
    
    if( $content_quotes != "" ) { // Check if the file is empty
    	$quotes = explode("\n", $content_quotes); // Split the quotes in an array divided by linebreak (\n)
    	$numbers_of_quotes = count($quotes); // Count the amount of quotes
    
    	$quote_to_show = rand(0, $numbers_of_quotes - 1); // Generate which quote to show
    	$quote_display = explode("::", $quotes[$quote_to_show]); // Split the quote and the author of the quote in an array
    
    header("Content-type: image/png");
    $string = $_POST['$quote_display[0]'];
    $im    = imagecreatefrompng("idudesays.png");
    $colour = imagecolorallocate($im, 100, 100, 100);
    imagestring($im, 3, 90, 23, $string, $colour);
    imagepng($im);
    imagedestroy($im);
    
    ?>
    but it errors out. Parse error: parse error, unexpected $ in /home/www/viniZVin/dyn/image.php on line 24

  10. #10
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts
    You haven't closed your IF have you?

  11. #11
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    Quote Originally Posted by ajbrun
    You haven't closed your IF have you?

    i dunno

    how would i do that.

  12. #12
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    holy fudging panty liner..

    http://www.vini.co.uk/dyn/
    Last edited by Vini; 08-05-2006 at 09:44 AM.

  13. #13
    Senior Member ajbrun's Avatar
    Join Date
    Apr 2004
    Location
    York, England
    Posts
    4,840
    Thanks
    4
    Thanked
    25 times in 13 posts
    404 not found

  14. #14
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    Quote Originally Posted by ajbrun
    404 not found

  15. #15
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    going on from this, i now want to have an admin 'area' where the user can see all the lines in the quotes.txt file and edit/delete them.

    it should be fairly simple, but i made a right hash.

    Code:
    <?
    if($_POST['Submit']){
    $open = fopen("quotes.txt","w+");
    $text = $_POST['update'];
    fwrite($open, $text);
    fclose($open);
    echo "File updated.<br />";
    echo "File now reads:<br />";
    $file = file("quotes.txt");
    foreach($file as $text) {
    echo $text."<br />";
    }
    }else{
    $file = file("quotes.txt");
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo "<textarea Name=\"update\" cols=\"70\" rows=\"25\">";
    foreach($file as $text) {
    echo $text;
    }
    echo "</textarea>";
    echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
    </form>";
    }
    ?>
    puts them in a text box. ideally i want them to be like:

    quote line 1 [X] [e!]
    quote line 2 [X] [e!]
    quote line 3 [X] [e!]
    quote line 4 [X] [e!]


    ideas?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help on rescue Dynamic Volume
    By arthurleung in forum Software
    Replies: 1
    Last Post: 28-02-2006, 10:16 AM
  2. Dynamic Image..
    By XA04 in forum Software
    Replies: 0
    Last Post: 30-05-2005, 03:06 PM
  3. Image problems in IE (probably CSS related)
    By Dorza in forum Software
    Replies: 4
    Last Post: 08-05-2005, 11:50 PM

Posting Permissions

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