Jump to content

Recommended Posts

Well from the fantastic response on my last post (0 replies, 80+ views), I've decided to browse A LOT of articles on how to do what I need to do.

And, well I got it down to the function in PHP as Eval.

 

I have my code set up like this:

 

$query="SELECT * FROM info";
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results)) 
{
$x = $row['pointtext'];
echo $x;
}

 

As you can see this, this is a simple query to grab all the rows under pointtxt.

I have only 1 at the moment which is to text it, and it echo's it fine!

It echo's this:

 

Imagettftext($im, 12, 0, $start_x, $start_y, $black, 'verdana.ttf', "text to write"); 

 

This is a PHP script for TEXT-TO-IMAGE.

 

SO then considering the echo worked, I used this code:

 

$query="SELECT * FROM info";
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results)) 
{
$x = $row['pointtext'];

header("Content-Type: image/jpeg");
$im = ImageCreateFromPng("test.png"); 
$black = ImageColorAllocate($im, 255, 255, 255);
$start_x = 10;
$start_y = 20;
eval($x);
Imagejpeg($im, '', 100);

ImageDestroy($im);
}

 

And as you can imagine, it doesn't work.

As I said in my 2 day old topic, I want it to have the ability to put more text onto the image and save it.

So, do I get help this time? PLEASE.

Does the eval function not support the TEXT TO IMAGE

got an error in the script.

What, please at least answer this time.

 

_______

 

Also forgot to mention that how the script doesn't work, is it displays the picture fine, but it doesn't place any text onto it. Or any I can see with the set coords.

 

Link to comment
https://forums.phpfreaks.com/topic/219057-eval/
Share on other sites

Your other thread probably did not receive any replies because the question did not make enough sense for anyone to know what the question was and when you started bumping it, you did not attempt to supply any further information.

 

You should not be storing php code statements in a database. You should be storing DATA. That's kind of why they are called databases.

 

The only thing that appears to be data in what you are doing is the x,y coordinate of each point that was picked. Only those two values (and any other relevant data) should be stored in your database table.

 

When you retrieve the data, you simply put each set of x,y coordinates into a Imagettftext() statement when you loop through the result set from the query.

 

Edit: Also, you can only output one content-type header, followed by the image data at one time, so the code inside your while(){} loop would never work with more than one row in your table. You would need to move most of that code outside of the while loop so that you only try to create one image with all the points on it.

 

Edit2: And frankly, this IS the same problem as in your previous thread and you should have continued it in that thread instead of starting a new thread for it.

Link to comment
https://forums.phpfreaks.com/topic/219057-eval/#findComment-1135989
Share on other sites

Please tell me you are not storing php code in a database table! If so, why? Also eval() is a function that you should really avoid due to the security risks it exposes.

 

As I explained, yes I am.

Where else could I store it? In a TXT file?

It is so I can add more stuff to the image if it needed more text on it, and so other people can do it and not just me and not manually..

And i'm aware of how bad eval is, but if you could provide an alternative way of doing this, please do.

 

@PFMaBiSmAd

As I just explained, If a user wanted to add another point to the image, he can. I'm making like a map-type of thing, you click a spot on the map, it reads it, puts all of the info in the database, and the image which contains the map reads ALL the points, and puts all the text onto the image.

 

If I was just to hold the X and Y coords (which I already am) how could I add more points (Which means add more of the Imagettftext()) without doing it manually? This is my problem, and I'm open to new suggestions. I really didn't want to use Eval but at the moment I think its my only option.

Link to comment
https://forums.phpfreaks.com/topic/219057-eval/#findComment-1135992
Share on other sites

See the Edit: in my post above.

 

Have you missed the point of looping over data and using the data in a (one) Imagettftext() statement that is in the code that is inside of your loop.

 

From your first post -

 

Imagettftext($im, 12, 0, $X, $Y, $black, 'verdana.ttf', "$text");

 

^^^ You use that line of code and simply set the $x and $y variables from the x and y values stored in the database table.

Link to comment
https://forums.phpfreaks.com/topic/219057-eval/#findComment-1135993
Share on other sites

PFMaBiSmAd has given the solution. One user can be allocated many points. This is a one to many database table relationship. The image creation should not be inside the loop. You should loop over all the points per user from the database query and create an array. Then you can use the array data to draw the final image.

Link to comment
https://forums.phpfreaks.com/topic/219057-eval/#findComment-1135995
Share on other sites

See the Edit: in my post above.

 

Have you missed the point of looping over data and using the data in a (one) Imagettftext() statement that is in the code that is inside of your loop.

 

From your first post -

 

Imagettftext($im, 12, 0, $X, $Y, $black, 'verdana.ttf', "$text");

 

^^^ You use that line of code and simply set the $x and $y variables from the x and y values stored in the database table.

 

Ok, I have done that and ended up with this:

 

<?php


//connect 
$db = mysql_pconnect($dbhost,$dbuser,$dbpass); 
mysql_select_db("$dbname",$db);

$query="SELECT * FROM info";
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results)) 
{
$x = $row['x'];
$y = $row['y'];
$text = $row['text'];
}

header("Content-Type: image/jpeg");
$im = ImageCreateFromPng("test.png"); 
$black = ImageColorAllocate($im, 255, 255, 255);
Imagettftext($im, 12, 0, $x, $y, $black, 'verdana.ttf', "$text"); 
Imagejpeg($im, '', 100);
ImageDestroy($im);
?> 

 

As you told me before to take the header stuff out of the loop.

Is this what you mean? Because I just tried, it again displays the image but doesn't write the text.

 

Woops, just made a bit of a mistake when I quoted my entire code :(

Sorry.

Link to comment
https://forums.phpfreaks.com/topic/219057-eval/#findComment-1135998
Share on other sites

The code you have posted is still logically incorrect. You are looping over the data and overwriting the single $x,$y,and $text variable. After the end of the loop, they only have the last values.

 

Logically, you are trying to accomplish this -

<?php
//connect 
$db = mysql_pconnect($dbhost,$dbuser,$dbpass); 
mysql_select_db("$dbname",$db);

$query="SELECT * FROM info";
$results = mysql_query($query);

$im = ImageCreateFromPng("test.png"); 
$black = ImageColorAllocate($im, 255, 255, 255);

while ($row = mysql_fetch_assoc($results)) 
{
$x = $row['x'];
$y = $row['y'];
$text = $row['text'];
Imagettftext($im, 12, 0, $x, $y, $black, 'verdana.ttf', "$text"); 
}
header("Content-Type: image/jpeg");
Imagejpeg($im, '', 100);
ImageDestroy($im);
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/219057-eval/#findComment-1136008
Share on other sites

The code you have posted is still logically incorrect. You are looping over the data and overwriting the single $x,$y,and $text variable. After the end of the loop, they only have the last values.

 

Logically, you are trying to accomplish this -

<?php
//connect 
$db = mysql_pconnect($dbhost,$dbuser,$dbpass); 
mysql_select_db("$dbname",$db);

$query="SELECT * FROM info";
$results = mysql_query($query);

$im = ImageCreateFromPng("test.png"); 
$black = ImageColorAllocate($im, 255, 255, 255);

while ($row = mysql_fetch_assoc($results)) 
{
$x = $row['x'];
$y = $row['y'];
$text = $row['text'];
Imagettftext($im, 12, 0, $x, $y, $black, 'verdana.ttf', "$text"); 
}
header("Content-Type: image/jpeg");
Imagejpeg($im, '', 100);
ImageDestroy($im);
?>

 

I tried your code, I got these errors:

 

Warning: imagettftext() expects parameter 5 to be long, string given in /home/****** on line 22

Warning: Cannot modify header information - headers already sent by (output started at /home/rspstop/public_html/ge/image.php:22) in /home/******* on line 24

 

And ALOT of random images and symbols appear as well.

I tried shortening the amount of rows in the database, that helped a little but the problem above still occurs.

Link to comment
https://forums.phpfreaks.com/topic/219057-eval/#findComment-1136013
Share on other sites

Did you even read the error message? It concerns the data type of one of the parameters being supplied to the Imagettftext() function call.

 

O, I just found out one of my mates just tried testing it. I found out what the problem was and fixed it.

Thanks again for your help and patients, all works good now.

Nice solution to, I knew the dangers of the Eval but it felt like my only solution, I didn't think looping  the line would work, but I don't know why I thought that haha..

 

Thanks again.

Link to comment
https://forums.phpfreaks.com/topic/219057-eval/#findComment-1136021
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.