Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
generating images with php not working... help!
Old 06-07-2010, 07:18 AM generating images with php not working... help!
ginnybrighton's Avatar
Novice Talker

Posts: 5
Name: Elaine McGovern
Trades: 0
Hi Guys,
Im new to the forum but i am at a loss and i really need some help solving this problem. I know that the answer is obvious but I just cant seem to find a solution. And, believe me, ive been at this for a week.
I am trying to build a family tree by generating an image with php with vertical and horizontal lines connecting one generation to the next. I am only starting so am just drawing the horizontal lines and already i am having problems.
I pull the generations from a mysql database and depending if that mother has had any children a series of horizontal lines will connect the mother to her children
--- John
--- Anne
Liz --- Mary
--- Bill
--- Tom
All these x, y coordinates are defined in a previous function and stored in a two dimensional array. This array is then passed to the function domake($array) which extracts the $x = $array[0][0], $y = $array[0][1] and so on until all the horizontal x and y coordinates are extracted. Each time i extract the $x and $y coordinates they get stored in $x1 and $y1 then $x2 = $x1 + 45 and $y2 = $y1. This makes the 4 coordinates required for the imageline($image, $x1, $y1, $x2, $y2, $black) function.
Now i will post my code in full:
I have attempted many different variations of my code this is the most condensed version and I think i have tracked my problem down to the contents of the array passed from the previous function and extracted here, manipulated and then drawn using the imageline($image, $x1, $y1, $x2, $y2, $black) . I say this because if i hard code in the x1, y1, x2, y2 coordinates the lines are drawn and everything looks fine. Even if the lines are in the same place imageline($image, 1, 1, 200, 1, $black) and imageline($image, 1, 1, 200, 1, $black) it still works fine. Even if i store everything in an array at the beginning of the function using
$array[0][0]=1; //x for line1
$array[0][1]=1; //y for line 1

$array[1][0]=100; //x for line2
$array[1][1]=100; //y for line2
It works fine too. So, now i am out of options, so i need someone who knows what they are doing to look at my function with fresh eyes and tell me what I am doing wrong.

PHP Code:
$myImage "";
$myWhite "";
$myBlack "";
$array "";
$width "";
$height "";
$image "";
$black ""
function 
domake($a1){
   global 
$width;
   global 
$height;
   global 
$image;
   global 
$black;
$array $a1
$count count($array);
 
   
$width 1000;
   
$height 1000;
  
$image imagecreate($width$height);
  
$black imagecolorallocate($image000);
     for (
$i 0$i <= $width; ++$i) {
      for (
$j 0$j <= $height; ++$j) {
        
$col imagecolorallocate($image255255255);
        
imagesetpixel($image$i$j$col);
      }
   }
   
    
$x = -1;
    
$y = -1;
    
    
//// get contents of array
    
for($arr=0$arr<$count$arr++){
        
$key $arr%2;
        
$x $array[$arr][0]*1;
        
$y $array[$arr][1]*1;
 
        
//if x and y are both greater than -1 make line
        
$x1 $x;
        
$y1 $y;
        
$x2 $x+450;
        
$y2 $y;
        if((
$x>=0)&&($y>=0)){
            
imageline($image$x1$y1$x2$y2$black);
        }
    }
 

    
//finish image
  
header("Content-type: image/png");
  
imagepng($image);
  
imagedestroy($image);



Thanks a million for all your help
ginnybrighton is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-07-2010, 07:21 AM Re: generating images with php not working... help!
ginnybrighton's Avatar
Novice Talker

Posts: 5
Name: Elaine McGovern
Trades: 0
Oops, i forgot to say in my post above that instead of an image a little box with an x appears on the page.
ginnybrighton is offline
Reply With Quote
View Public Profile
 
Old 06-07-2010, 10:51 AM Re: generating images with php not working... help!
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
ok, but what error does php gives you back?
Did you look at the log, did you tried to open the picture directly?
If it helps, comment the header() call, it won't switch your browser to an image rendering mode, and you will be able to see the error messages, if any.

Just thinking of it, it could be that you have a space at the beginning, before the php opening tag that corrupts the image.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-07-2010, 11:16 AM Re: generating images with php not working... help!
ginnybrighton's Avatar
Novice Talker

Posts: 5
Name: Elaine McGovern
Trades: 0
Thanks so much for your ideas. I am still finding my way so i am not sure what you mean by the log? I checked the cgierror.log - whatever that is - and there was nothing in that. Then, i checked the web.log and there was nothing in that that related to today. Not sure if that is what you meant tho.

Then i commented out the header information and as far as i could tell, it just returned garbage... but maybe it might mean something to you...

(hopefully)

Thanks a mill again


‰PNG  IHDRz}$PLTE = ”IDATxœp]‰­vcvضm64f۶m6 & =w78Ww}"›^›^›^›^›^›^›‡ ^›‡^›‡^›‡^›‡^›‡^›‡^›‡^›G^›qX7 xX7tX7|X7rX7zX7vX7~X7qX7yX7uX7 }X7sX7{X7wX7X7OpX7OxX7OtX7O|X7zn žznžznžznžznžznžznžznžznž znžznžznžznžznžznžznžznžz nžznžznžznžznžznžznžznžzn žznžznžzn^zn^zn^zn^zn^zn^ zn^zn^zn^zn^zn^zn`ݼ`ݼ `ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ` `ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ `ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ `ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ` `ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ `ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ`ݼ `ݼ`|`|`|`|`|`|`|` |`|`|`|`|`|`|`|`| `|`|`|`|`|`|`|` ? €u €u33泮泯ss   ‹‹]/^›/^›/^›/^›/^›^›^›^›^›^›^›^›^›o^›o ^›o^›o^›o^›o^›o^›o^›^›^›^› ^›^›^›^›^›^›^›^›^›^› ^›^›^›Ÿ^›Ÿ^›Ÿ^›Ÿ^›Ÿ^›Ÿ^›Ÿ^›Ÿ ^›_^›_^›_^›_^›_^›_^›_^›_^›߸^› ^›ߺ^›߾^›߹^›߽^›߻^›߿^›?^›?^›?^›? ^›?^›?^›?^›?^›^›^›^›^›^› ^›^›^›^›^›^›^›^›^›>C €!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€\:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!@ t:„B‡C€!…tIENDB`‚
ginnybrighton is offline
Reply With Quote
View Public Profile
 
Old 06-07-2010, 12:11 PM Re: generating images with php not working... help!
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
If your on Linux, Check out the logfile.

/var/log/httpd/error.log
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 06-07-2010, 12:12 PM Re: generating images with php not working... help!
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Also insure your not doing ANY! output from your code until you reach
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 06-07-2010, 12:45 PM Re: generating images with php not working... help!
ginnybrighton's Avatar
Novice Talker

Posts: 5
Name: Elaine McGovern
Trades: 0
Hi ya Lynxus,

Thanks a million for your replies. There is definately no output : by this, i assume you mean printr or echo and all that. I have some echos for testing but i have commented these out using //.

The server isnt mine so i dont think i have access to that log. I use winscp to upload my files and i cant get to the directory:

/var/log/httpd/error.log

Sorry for coming across completly thick!
ginnybrighton is offline
Reply With Quote
View Public Profile
 
Old 06-07-2010, 02:18 PM Re: generating images with php not working... help!
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
i assume you mean printr or echo and all that
Not only...
Do you have a space before the php opening tag?
Then it could be that.

That "garbage", is the binary of the png file you are creating.
If it appears with a "broken" image, then it means that something corrupts the generated image.

Try to add this at the beginning of your script:
http://www.php.net/manual/en/function.ob-start.php
PHP Code:
ob_start(); 
and this at the end of the script:
http://www.php.net/manual/en/function.ob-end-clean.php
PHP Code:
ob_end_clean(); 
If there is already output before the php opening tag, you will see an error message telling you where the first character has been outputted.

Another good thing would be to raise your error reporting to E_ALL, to be sure you don't have a warning at all.
Put this at the beginning of the file too:
http://www.php.net/manual/en/functio...-reporting.php
PHP Code:
error_reporting(E_ALL); 
Quote:
The server isnt mine so i dont think i have access to that log
Not a problem, you can tell apache to use a specific php log file via .htaccess
Code:
php_flag display_errors false
php_value error_log /var/www/whatever.com/php.log
And this means that every php scripts located in the same directory than the htacces (or in child directories) will save the php error to /var/www/whatever.com/php.log
Adapt to your needs.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 06-07-2010 at 02:24 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-08-2010, 09:21 AM Re: generating images with php not working... help!
ginnybrighton's Avatar
Novice Talker

Posts: 5
Name: Elaine McGovern
Trades: 0
Hi tripy and linxus

I read through your advice and stripped my php right back to the bare essentials and kept looking for ANY output before the header("Content-type: image/png");

And lo and behold, there in the include("database.php") script was the culprit return causing all the trouble...

Once, i fixed that problem, I then realised i had messed up my array so that was another problem but if it helps anyone I will post the updated php which now works for horizontal lines only... (no doubt i will be back looking for more advice before the end of this family tree)

and youre right: beware of any OUTPUT - even return characters... I am so happy that im using netbeans or I may never have seen it... :

function domake($a1){
global $width;
global $height;
global $image;
global $black;
$array = $a1;
$width = 1000;
$height = 1000;
$image = imagecreate($width, $height);
$black = imagecolorallocate($image, 0, 0, 0);

for ($i = 0; $i <= $width; ++$i) {
for ($j = 0; $j <= $height; ++$j) {
$col = imagecolorallocate($image, 255, 255, 255);
imagesetpixel($image, $i, $j, $col);
}
}
$x = -1;
$y = -1;
// get contents of array
for($arr=0; $arr<count($array); $arr++){

$key = $arr%2;
$x = $array[$arr][0]*1;
$y = $array[$arr][1]*1;
//if x and y are both greater than -1 make line
$x1 = $x;
$y1 = $y;
$x2 = $x+45;
$y2 = $y;
if(($x>=0)&&($y>=0)){
imageline($image, $x1, $y1, $x2, $y2, $black);
}
}

// imageline($image, 10, 10, 450, 10, $black);
//finish image
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
}
ginnybrighton is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to generating images with php not working... help!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31982 seconds with 12 queries