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
https://forums.phpfreaks.com/topic/188958-making-image/
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
https://forums.phpfreaks.com/topic/188958-making-image/#findComment-997736
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
https://forums.phpfreaks.com/topic/188958-making-image/#findComment-997742
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
https://forums.phpfreaks.com/topic/188958-making-image/#findComment-997744
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
https://forums.phpfreaks.com/topic/188958-making-image/#findComment-997754
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.