c_raethke Posted January 19, 2010 Share Posted January 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188958-making-image/ Share on other sites More sharing options...
oni-kun Posted January 19, 2010 Share Posted January 19, 2010 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188958-making-image/#findComment-997736 Share on other sites More sharing options...
c_raethke Posted January 19, 2010 Author Share Posted January 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188958-making-image/#findComment-997742 Share on other sites More sharing options...
oni-kun Posted January 19, 2010 Share Posted January 19, 2010 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.. Quote Link to comment https://forums.phpfreaks.com/topic/188958-making-image/#findComment-997744 Share on other sites More sharing options...
c_raethke Posted January 19, 2010 Author Share Posted January 19, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/188958-making-image/#findComment-997750 Share on other sites More sharing options...
oni-kun Posted January 19, 2010 Share Posted January 19, 2010 Refer to my last post, I edited it to read from the file if no GET is defined, or read from the GET itself. Quote Link to comment https://forums.phpfreaks.com/topic/188958-making-image/#findComment-997751 Share on other sites More sharing options...
oni-kun Posted January 19, 2010 Share Posted January 19, 2010 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. At the bottom left of this thread should be a 'topic solved' button, You should press it to declare it solved. Quote Link to comment https://forums.phpfreaks.com/topic/188958-making-image/#findComment-997754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.