php help needed

mkt

When a paradise is lost go straight to Disney™
Premium Member
Original Poster
anyone know how to fix this code?

PHP:
<?php

$fp = fopen("http://www.audiomatch.net/text/mkt.txt", "r");

$img_number = imagecreate(400,139);

$backcolor = imagecolorallocate($img_number,0,0,0);

$textcolor = imagecolorallocate($img_number,255,255,255);

imagefill($img_number,0,0,$backcolor);

$number =  "Currently Listening to: $fp";

Imagestring($img_number,10,5,5,$number,$textcolor)  ;

header("Content-type: image/png");

Imagepng($img_number);

?>
 

pisco

New Member
I would move the header line to the top of the script not necessarily required but it can't hurt).

Have you confirmed that $fp is returning a valid record?

Also add ImageDestroy($img_number) at the end.

Here is an example script that I am using to generate images from text entries in a database:

PHP:
<?php
    // Define the image type header
	header('Content-Type: image/png');
	
	// Set font
    $font = realpath($selectedttf);
	
	// Get URL variables
    $text = $_GET["text"];
    $fontsize = $_GET["fontsize"];
	$textcolor = $_GET["textcolor"];
	list($textred, $textgreen, $textblue) = explode(",", $textcolor);
	$bgcolor = $_GET["bgcolor"];
	list($bgred, $bggreen, $bgblue) = explode(",", $bgcolor);

	// Get text image bounding box and set height and width
    $textcords = imagettfbbox ($fontsize, 0, $font, $text);
    $textwidth =  $textcords[2] + 2;
    $textheight = $textcords[7] - ($textcords[7] * 2) + 7;
	
	// Create image and allocate image colors
    $im = imagecreate ($textwidth, $textheight);
    $black = ImageColorAllocate ($im, $bgred, $bggreen, $bgblue);
    $white = ImageColorAllocate ($im, $textred, $textgreen, $textblue);

	// Print text in image
    ImageTTFText ($im, $fontsize, 0, 0, $fontsize, $white, $font,$text);
	
	// Output and destroy image
    Imagepng ($im);
    ImageDestroy ($im);
?>

Hope this helps.
 

pisco

New Member
It occured to me that the problem is that fopen only opens the file to be read. To get the contents you have to do an fread of the opened file.

This will work:

PHP:
<?php

$handle = fopen("http://www.audiomatch.net/text/mkt.txt", "rb");
$contents = "";
do {
   $data = fread($handle, 8192);
   if (strlen($data) == 0) {
       break;
   }
   $contents .= $data;
} while (true);
fclose($handle);

$img_number = imagecreate(400,139);

$backcolor = imagecolorallocate($img_number,0,0,0);

$textcolor = imagecolorallocate($img_number,255,255,255);

imagefill($img_number,0,0,$backcolor);

$number =  "Currently Listening to: $contents";

 Imagestring($img_number,10,5,5,$number,$textcolor)
  ;

header("Content-type: image/png");

Imagepng($img_number);

?>
 

mkt

When a paradise is lost go straight to Disney™
Premium Member
Original Poster
I think it worked....

test.php
 

mkt

When a paradise is lost go straight to Disney™
Premium Member
Original Poster
all I need to do now is reformat the text and it's done!
 

mkt

When a paradise is lost go straight to Disney™
Premium Member
Original Poster
hmm... now I can't seem to insert the line succesfully...

any ideas on where to shove this in:

PHP:
imagettftext($img_number,10,10,10,33,$white,'cour.ttf',$number);
 

pisco

New Member
If you are using the second script I posted then you will not need the imagettftext() function. You can just use the Imagestring function to add the text to the image.

If you are using the first script then it will work with a couple of caveats. First, you must call the font with the full path to it on the drive. If you don't know the path then use the realpath() function and assign the results to a variable like I did in the second line of the first script. Second, the path cannot contain any spaces. To test just run realpath() on your font file and print out the result.

Once it works it is pretty cool. You can use almost any TrueType font you have.

Here is a test file I just completed using Oz Handicraft as the font.

http://s93264548.onlinehome.us/imgtest.htm

Hope this helps.
 

mkt

When a paradise is lost go straight to Disney™
Premium Member
Original Poster
hit me up on AIM.. I really could use your help... thanks!
 

mkt

When a paradise is lost go straight to Disney™
Premium Member
Original Poster
PHP:
 <?php 

header("Content-type: image/png"); 

$font = realpath("cour.ttf");
     
     echo $font;
 
$handle = fopen("http://www.audiomatch.net/text/mkt.txt", "rb"); 
$contents = ""; 
do { 
   $data = fread($handle, 8192); 
   if (strlen($data) == 0) { 
       break; 
   } 
   $contents .= $data; 
} while (true); 
fclose($handle); 
 
$img_number = imagecreate(400,139); 
 
$backcolor = imagecolorallocate($img_number,0,0,0); 
 
$textcolor = imagecolorallocate($img_number,255,255,255); 
 
imagefill($img_number,0,0,$backcolor); 
 
$number =  "Currently Listening to: $contents"; 
 
 Imagestring($img_number,10,5,5,$number,$textcolor) 
  ; 
 
Imagepng($img_number); 
 
?>
 

mkt

When a paradise is lost go straight to Disney™
Premium Member
Original Poster
and finally! working code (THANK YOU PISCO!!!)

PHP:
<?php 

    // Define the image type header 
    header('Content-Type: image/png'); 
      
    // Set font by inserting the neame of the fonfile between the quotes in the realpath() function call below. 
    // Note: the font will need to be in the same directory as this php script. 
    // Capitalization must match that of the fontfile name as well. 
    // I preferred the appearance of Courier Bold but the choice here is all yours. 
    // You can upload and use and TrueType font you want. You will only need to change the fontfile name below. 
    $font = realpath("cour.ttf"); 
      
    // Open and read the contents of the Audiogalaxy text file 
    $handle = fopen("http://www.audiomatch.net/text/mkt.txt", "rb"); 
    $contents = ""; 
    do { 
       $data = fread($handle, 8192); 
       if (strlen($data) == 0) { 
           break; 
       } 
       $contents .= $data; 
    } while (true); 
    fclose($handle); 
      
    // The purpose of this if/else statement is to allow you to put in your own message for when you are not listening. 
    if($contents){ 
        $text = $contents; 
    } 
    else{ 
        $text = "Absolutely Nothing"; 
    } 
      
    $fontsize = 10; 

    // Get text image bounding box and set height and width for both lines of the image 
    $atextcords = imagettfbbox ($fontsize, 0, $font, "Currently Listening to:"); 
    $atextwidth =  $atextcords[2] + 2; 
    $atextheight = $atextcords[7] - ($atextcords[7] * 2) + 7; 
      
    $btextcords = imagettfbbox ($fontsize, 0, $font, $text); 
    $btextwidth =  $btextcords[2] + 2; 
    $btextheight = $btextcords[7] - ($btextcords[7] * 2) + 7; 
      
      
    // Calculate image height and width based on bounding box values of both lines of text 
    if($atextwidth > $btextwidth){ 
        $imgwidth = $atextwidth * 1.2; 
    } 
    else{ 
        $imgwidth = $btextwidth * 1.2; 
    } 
    $imgheight = ($atextheight + $btextheight) * 1.5; 
      
    // Create image and allocate image colors 
    $im = imagecreate ($imgwidth, $imgheight); 
    $black = ImageColorAllocate ($im, 0, 0, 0); 
    $white = ImageColorAllocate ($im, 255, 255, 255); 

    // Print text in image with initial x/y positions set relative to imgsize and text height and textwidth 
    ImageTTFText ($im, $fontsize, 0, ($imgwidth-$atextwidth)/2, ($imgheight-$atextheight)/2, $white, $font,"Currently Listening to:");      
    ImageTTFText ($im, $fontsize, 0, ($imgwidth-$btextwidth)/2, ($imgheight-($atextheight+$btextheight)/1.8), $white, $font,$text); 
      
    // Output and destroy image 
    Imagepng ($im); 
    ImageDestroy ($im); 
?>

and the final result of all that coding

test2.php
 

pisco

New Member
I noticed that it is still having a problem where the song appears on a third line. To fix it replace

PHP:
$text = $contents

with

PHP:
$text = str_replace(chr(10), " - ", $contents);
 

Register on WDWMAGIC. This sidebar will go away, and you'll see fewer ads.

Back
Top Bottom