Including script in random image

  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

I have the following script.

PHP Code: [ Select ]
 
<? header(“Content-type: image/jpeg”);
 
$im = imagecreate(50,100);
 
$bg = imagecolorallocate($im,255,255,255);
 
$fg = imagecolorallocate($im,100,120,130);
 
$borc = imagecolorallocate($im,0,0,0); imagefill($im,0,0,$bg);
 
imagerectangle($im,0,0,99,99,$borc);
 
imagestring($im,1,20,45,”Hello World”,$fg);
 
 imagejpeg($im); imagedestroy($im);
 
?>
  1.  
  2. <? header(“Content-type: image/jpeg”);
  3.  
  4. $im = imagecreate(50,100);
  5.  
  6. $bg = imagecolorallocate($im,255,255,255);
  7.  
  8. $fg = imagecolorallocate($im,100,120,130);
  9.  
  10. $borc = imagecolorallocate($im,0,0,0); imagefill($im,0,0,$bg);
  11.  
  12. imagerectangle($im,0,0,99,99,$borc);
  13.  
  14. imagestring($im,1,20,45,”Hello World”,$fg);
  15.  
  16.  imagejpeg($im); imagedestroy($im);
  17.  
  18. ?>


Now I have the following script to get information out of a database

PHP Code: [ Select ]
<?
 
include('inc/config.php'); ?>
 
 
 
<b>Last Song played, Author, and When Played</b>
 
 
 
<?
 
 
 
$result = mysql_query("SELECT * FROM `historylist` ");
 
 
 
echo "<table>";
 
while ($row=mysql_fetch_assoc($result)) {
 
 
 
$date_played=$row['date_played'];
 
$title=$row['title'];
 
$artist=$row['artist'];
 
 
 
echo "<tr><td>
 
$date_played - $name</td>
 
</tr>";
 
 
 
}
 
 
 
echo "<table>";
 
 
 
?>
  1. <?
  2.  
  3. include('inc/config.php'); ?>
  4.  
  5.  
  6.  
  7. <b>Last Song played, Author, and When Played</b>
  8.  
  9.  
  10.  
  11. <?
  12.  
  13.  
  14.  
  15. $result = mysql_query("SELECT * FROM `historylist` ");
  16.  
  17.  
  18.  
  19. echo "<table>";
  20.  
  21. while ($row=mysql_fetch_assoc($result)) {
  22.  
  23.  
  24.  
  25. $date_played=$row['date_played'];
  26.  
  27. $title=$row['title'];
  28.  
  29. $artist=$row['artist'];
  30.  
  31.  
  32.  
  33. echo "<tr><td>
  34.  
  35. $date_played - $name</td>
  36.  
  37. </tr>";
  38.  
  39.  
  40.  
  41. }
  42.  
  43.  
  44.  
  45. echo "<table>";
  46.  
  47.  
  48.  
  49. ?>


How would I combine the two scripts to make an image of what I last played?

Basically what I want the image to be is this

Quote:
|---------------------------------------------------|
|Song Last Played: <enter song last played> |
|---------------------------------------------------|


that is close enough



Also can you fix the first script. theres something wrong. and can you tell me or show me a link on how to retreve the last mysql field from a table?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

what happened to RichB's post.

well here is almost the final product

http://www.your4rums.zinix.net/song/song2.php
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I was going to edit it to make it more clear and messed it up, so I deleted it and was called away from the computer before I could redo it. Anyway the advice was to keep the image script in a separate file and pass the title in the url of an img tag which I see you've done. I didn't think of it before, but you should encode the title before passing it in case characters like an ampersand occur in the title which could mess things up:

Code: [ Select ]
$title=$row['title'];
$title=urlencode($title);
  1. $title=$row['title'];
  2. $title=urlencode($title);
  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

thanks now can you give me a hint on my next question.

Question:

Ok I have no idea how to do this so

How do I select the last field of a row.

It is on auto increasement if that helps. I will supply more info if needed
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I'm not sure I understand. You can select any fields that you want in your query. Based upon your first post I'm wondering if you mean you want to select the just the last row in the table - the one with the highest autoincrement value..

In other words: you want to have only one image created with the most recent song played.
  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

correct. but thats what I ment
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Ok, then I think you want to do something like this:

PHP Code: [ Select ]
$result = mysql_query("SELECT * FROM `historylist` ORDER BY id desc LIMIT 0,1")


That should select only the record with the highest autoincrement field, which should be the last record entered. You may have to change id to match whatever you named your autoincrement field.
  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

PHP Code: [ Select ]
<?
 
include('inc/config.php');
 
$result = mysql_query("SELECT * FROM `historylist` ORDER BY ID desc LIMIT 0,1")
 
while ($row=mysql_fetch_assoc($result)) {
 
 
 
$title=$row['title'];
 
$title=urlencode($title);
 
 
 
echo "<tr><td>
 
<img src=\"image.php?title=$title\" width=\"300\" height=\"50\" alt=\"last played\">
 
</td>
 
</tr>";
 
 
 
}
 
 
 
echo "</table>";
 
 
 
?>
  1. <?
  2.  
  3. include('inc/config.php');
  4.  
  5. $result = mysql_query("SELECT * FROM `historylist` ORDER BY ID desc LIMIT 0,1")
  6.  
  7. while ($row=mysql_fetch_assoc($result)) {
  8.  
  9.  
  10.  
  11. $title=$row['title'];
  12.  
  13. $title=urlencode($title);
  14.  
  15.  
  16.  
  17. echo "<tr><td>
  18.  
  19. <img src=\"image.php?title=$title\" width=\"300\" height=\"50\" alt=\"last played\">
  20.  
  21. </td>
  22.  
  23. </tr>";
  24.  
  25.  
  26.  
  27. }
  28.  
  29.  
  30.  
  31. echo "</table>";
  32.  
  33.  
  34.  
  35. ?>


how come that code doesn't work. I get error on line 4 unexpected while
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I forgot to put a semicolon at the end of the example select line. Sorry about that. You don't really need the while loop for that since it will only loop once, but you do need to put the semicolon at the end of the line above.
  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

thanks, it now works. the only problem is how I get it to show in my sig
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Er, I had no idea that's where you were going with it. I thought you were going to incorporate it into your own page. Let me look into it and see what I can come up with. The database code will have to go in with the image script after all, and php files generating images don't seem to work directly, but I think I know a way around that.
  • darkermoon
  • Expert
  • Expert
  • User avatar
  • Posts: 542
  • Loc: Riverdale, MD

Post 3+ Months Ago

I made php parse .jpg files, lmao which, allowed me to have a sig that is actually php.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I found a tutorial on this, but I'm still having trouble getting the image to display. It keeps asking if I want to download instead of displaying the image.
  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

the reason I wanated to post it in my sig is because I am currently running a part time radio station and I want show what is being played. Thanks for the help so far
  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

since I figured out what I am going to do which is follow those directions. can you help me change from what I currently have to include the image I make insted of making one please. Thanks for all the help

heres what I have

PHP Code: [ Select ]
<?php
 
// tell the user's browser that it is an image
 
header("Content-type: image/png");
 
 
 
// load the background
 
$image = imagecreatefrompng("blank.png");
 
 
 
// define the color black (the colour of the text)
 
$clr_black = imagecolorallocate($image, 0, 0, 0);
 
 
 
// Now let's add the text
 
// You are going to need to pull your own text from a database
 
// of your own.
 
$result = mysql_query("SELECT * FROM `historylist` ORDER BY ID desc LIMIT 0,1")
 
   
 
// draw heading
 
 
 
imagestring($image,"Last Played Song", $clr_black);
 
 
 
while ($row = mysql_fetch_array($result))
 
 
 
$title=$row['title'];
 
$title=urlencode($title);
 
 
 
{
 
 
 
    // draw the last 5 tutorial titles
 
    imagestring($image, $row['title'], $clr_black);
 
}
 
 
 
// and now... we display the image
 
imagepng($image);
 
imagedestroy($image);
 
?>
  1. <?php
  2.  
  3. // tell the user's browser that it is an image
  4.  
  5. header("Content-type: image/png");
  6.  
  7.  
  8.  
  9. // load the background
  10.  
  11. $image = imagecreatefrompng("blank.png");
  12.  
  13.  
  14.  
  15. // define the color black (the colour of the text)
  16.  
  17. $clr_black = imagecolorallocate($image, 0, 0, 0);
  18.  
  19.  
  20.  
  21. // Now let's add the text
  22.  
  23. // You are going to need to pull your own text from a database
  24.  
  25. // of your own.
  26.  
  27. $result = mysql_query("SELECT * FROM `historylist` ORDER BY ID desc LIMIT 0,1")
  28.  
  29.    
  30.  
  31. // draw heading
  32.  
  33.  
  34.  
  35. imagestring($image,"Last Played Song", $clr_black);
  36.  
  37.  
  38.  
  39. while ($row = mysql_fetch_array($result))
  40.  
  41.  
  42.  
  43. $title=$row['title'];
  44.  
  45. $title=urlencode($title);
  46.  
  47.  
  48.  
  49. {
  50.  
  51.  
  52.  
  53.     // draw the last 5 tutorial titles
  54.  
  55.     imagestring($image, $row['title'], $clr_black);
  56.  
  57. }
  58.  
  59.  
  60.  
  61. // and now... we display the image
  62.  
  63. imagepng($image);
  64.  
  65. imagedestroy($image);
  66.  
  67. ?>


that even close to correct
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Taking your old image script and combining it with the database query and making it a png like in the example on the tutorial page I came up with this:

PHP Code: [ Select ]
<?php
 
header("Content-type: image/png");
 
$im = imagecreate(300,50);
 
$fg = imagecolorallocate($im,100,120,130);
 
$bg = imagecolorallocate($im,255,255,255);
 
$borc = imagecolorallocate($im,0,0,0);
 
imagefill($im,0,0,$bg);
 
imagerectangle($im,0,0,299,49,$borc);
 
 
 
include("inc/config.php");
 
$query = "SELECT * FROM `tutorials` ORDER BY date DESC LIMIT 0,1";
 
$result = mysql_query($query);
 
$row = mysql_fetch_assoc($result);
 
$title = $row['title'];
 
 
 
$text = "Song Last Played - $title";  
 
 
 
$font = 3;
 
$x_pos = 5;
 
$y_pos = 15;
 
 
 
imagestring($im, $font, $x_pos, $y_pos, $text, $fg);
 
imagepng($im);
 
imagedestroy($im);
 
?>
  1. <?php
  2.  
  3. header("Content-type: image/png");
  4.  
  5. $im = imagecreate(300,50);
  6.  
  7. $fg = imagecolorallocate($im,100,120,130);
  8.  
  9. $bg = imagecolorallocate($im,255,255,255);
  10.  
  11. $borc = imagecolorallocate($im,0,0,0);
  12.  
  13. imagefill($im,0,0,$bg);
  14.  
  15. imagerectangle($im,0,0,299,49,$borc);
  16.  
  17.  
  18.  
  19. include("inc/config.php");
  20.  
  21. $query = "SELECT * FROM `tutorials` ORDER BY date DESC LIMIT 0,1";
  22.  
  23. $result = mysql_query($query);
  24.  
  25. $row = mysql_fetch_assoc($result);
  26.  
  27. $title = $row['title'];
  28.  
  29.  
  30.  
  31. $text = "Song Last Played - $title";  
  32.  
  33.  
  34.  
  35. $font = 3;
  36.  
  37. $x_pos = 5;
  38.  
  39. $y_pos = 15;
  40.  
  41.  
  42.  
  43. imagestring($im, $font, $x_pos, $y_pos, $text, $fg);
  44.  
  45. imagepng($im);
  46.  
  47. imagedestroy($im);
  48.  
  49. ?>


You don't need to urlencode anything - that was just when you were passing the title as part of an url in an image tag. Try it first as an ordinary .php file and see if it displays the image in the browser. Then try following the instructions in the tutorial by renaming it to a .png file and making an .htaccess file that will make the server process .png files in that directory for php.
  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

The image “http://www.your4rum.info/song/test.php” cannot be displayed, because it contains errors.

what could that mean
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Oops, I used the select from the tutorial instead of yours, replace:

PHP Code: [ Select ]
$query = "SELECT * FROM `tutorials` ORDER BY date DESC LIMIT 0,1";


with:

PHP Code: [ Select ]
$query = "SELECT * FROM `historylist` ORDER BY id DESC LIMIT 0,1";
  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

i should check over the code just incase from now on. even though u know more then i do i really should

where does it create the image size here?

PHP Code: [ Select ]
$im = imagecreate(300,50);


and is that the only place
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Yes, that is the size of the image. Although it is the only place that the size is created keep in mind that you are drawing a border just inside the image, and if you change the size without changing the border to match it may not be obvious that the change has taken place because your image has a white background on the white background that most web browsers will display an image, so you will only be seeing the border as the edge.

For example if you wanted it to be the maximum height and width for an Ozzu sig you could change the image dimensions:

$im = imagecreate(468,100);

and change the border dimensions:

imagerectangle($im,0,0,467,99,$borc);

..drawing the border one pixel less than the image size as in the original script you started with.
  • darkermoon
  • Expert
  • Expert
  • User avatar
  • Posts: 542
  • Loc: Riverdale, MD

Post 3+ Months Ago

you could create a redirect from some random .jpg file that doesn't exist and rewrite the URL to the PHP file.. that's what I'm doing as of now.. it works better. I know this can be done in Cpanel..
  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

thanks for the help
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

darkermoon's suggestion of using rewrite worked real well for me.

I put this in my .htaccess file in the directory with the image.php script:

Code: [ Select ]
RewriteEngine On
RewriteRule sig.png image.php
  1. RewriteEngine On
  2. RewriteRule sig.png image.php


and the I put a link to sig.png in my signature:

http://gotrivia.com/test/sig.png

In other words sig.png doesn't actually exist and my image script file is named image.php, but whenever a request is made for the nonexistent sig.png the server rewrites it to image.php and the dynamic image is returned.
  • darkermoon
  • Expert
  • Expert
  • User avatar
  • Posts: 542
  • Loc: Riverdale, MD

Post 3+ Months Ago

glad I could help :) and, as it appears, your server isn't as flaky as mine and therefore, has no issues :)
  • whatlikesit12345
  • Guru
  • Guru
  • User avatar
  • Posts: 1211

Post 3+ Months Ago

well im just going to stick with what you gave me, it works.
  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

This is kinda neat. I was thinking about this a long time ago but never really got to it.....maybe I will do the same thing and instead just allow for random sigs!

Post Information

  • Total Posts in this topic: 26 posts
  • Users browsing this forum: No registered users and 91 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.