Jump to content

Making image


c_raethke

Recommended Posts

Hi, I am new to PHP and I'm trying to make a script that turns a text into an image, then saves it to a file, so it always shows the last text put into it. I want it to only save the input text if there is one (the text is given at the end of the address, like blah.php?data=stuff). Here is my current code:

<?php
if(isset($_GET["data"]))
{
  $data=$_GET["data"];
  file_put_contents("./data.txt",$data);
}
$string = file_get_contents("./data.txt");
$width = strlen($data)*8;
$my_img = imagecreate( $width, 15); 
$background = imagecolorallocate( $my_img, 255, 255, 255 ); 
$text_color = imagecolorallocate( $my_img, 0, 0, 0 ); 
imagestring( $my_img, 4, 0, 0, $string, $text_color ); 
header( "Content-type: image/png" ); 
imagepng( $my_img ); 
imagecolordeallocate( $text_color ); 
imagecolordeallocate( $background ); 
imagedestroy( $my_img ); 
?>

It works, except for when I view the actual php page (blah.com/blah.php, instead of blah.com/blah.php?data=12345). When I view it with nothing after the .php, it gives me a bunch of errors, originating from the fact that it doesn't know the $data variable. Can someone help me fix this? Thanks.

Link to comment
Share on other sites

You'd need to extend the conditional statement, IE:

<?php
if(isset($_GET["data"]))
{
  $data=$_GET["data"];
  file_put_contents("./data.txt",$data);
  $string = file_get_contents("./data.txt");
  $width = strlen($data)*8;
  $my_img = imagecreate( $width, 15); 
  $background = imagecolorallocate( $my_img, 255, 255, 255 ); 
  $text_color = imagecolorallocate( $my_img, 0, 0, 0 ); 
  imagestring( $my_img, 4, 0, 0, $string, $text_color ); 
  header( "Content-type: image/png" ); 
  imagepng( $my_img ); 
  imagecolordeallocate( $text_color ); 
  imagecolordeallocate( $background ); 
  imagedestroy( $my_img ); 
} else {
   trigger_error('Data variable not defined.');
   exit;
}
?>

Link to comment
Share on other sites

Ok here is the thing. I don't want it to show an error if there is no data there. I want it to show the image with the text from the file, and if there is NEW data, then I want it to write that to the file, and use that. So would I just have to write all the code to do that in the else block?

 

EDIT: Nevermind, that didn't work. It still gave the same error messages.

Link to comment
Share on other sites

Ok here is the thing. I don't want it to show an error if there is no data there. I want it to show the image with the text from the file, and if there is NEW data, then I want it to write that to the file, and use that. So would I just have to write all the code to do that in the else block?

 

You'd have to do more than that, I'll assume data.txt will always be there, but you can do this..

<?php
if(isset($_GET["data"]))
{
  $data=$_GET["data"];
  file_put_contents("./data.txt",$data);
  $string = $_GET['data'];
} else {
   //If no GET was defined, read from data.txt
   $string = file_get_contents("./data.txt");
}
  $width = strlen($string)*8;
  $my_img = imagecreate( $width, 15); 
  $background = imagecolorallocate( $my_img, 255, 255, 255 ); 
  $text_color = imagecolorallocate( $my_img, 0, 0, 0 ); 
  imagestring( $my_img, 4, 0, 0, $string, $text_color ); 
  header( "Content-type: image/png" ); 
  imagepng( $my_img ); 
  imagecolordeallocate( $text_color ); 
  imagecolordeallocate( $background ); 
  imagedestroy( $my_img ); 
?>

 

EDIT: Fixed the code to work..

Link to comment
Share on other sites

Ok, well that got rid of the error messages, but I still need it to make an image to show based on that $string. When I add the rest of the code it gives me errors again:

</snip>

 

EDIT: Thanks, it worked!

 

No problem.  :P

At the bottom left of this thread should be a 'topic solved' button, You should press it to declare it solved.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.