loophp / phptree

An implementation of tree data structure
MIT License
93 stars 4 forks source link

Return value of image exporter #18

Closed Samvdg closed 3 years ago

Samvdg commented 3 years ago

Steps required to reproduce the problem

  1. Create a testnode
  2. add another one to it as a child for realism purposes
  3. try to export it to a PNG
  4. die with a "it works??" response

Expected Result

Actual Result


                $testnode = new ValueNode("test");
                $testnode2 = new ValueNode("test2");

                $testnode->add($testnode2);
//                dd($testnode);

                $imageExporter = new Image();
                $imageContent = $imageExporter->setFormat('png')->export($testnode);
                die("itworks???");
Samvdg commented 3 years ago

Fixed my drivers by the way

drupol commented 3 years ago

I just tested locally and everything works fine on my side.

Also, there is no occurrence of 'works' in the source code of phptree.

Which version are you using?

Samvdg commented 3 years ago

We're using version 2.6.3

Samvdg commented 3 years ago

Really? are we missing something? The test code we use right there gives us the error

drupol commented 3 years ago

Yes, I used your example, I copy paste it in a test.php file... and run it without any troubles.

drupol commented 3 years ago

This is the content of the file:

<?php

declare(strict_types=1);

use loophp\phptree\Exporter\Image;
use loophp\phptree\Node\ValueNode;
use loophp\phptree\Tree\Tree;

include 'vendor/autoload.php';

$testnode = new ValueNode("test");
$testnode2 = new ValueNode("test2");

$testnode->add($testnode2);
//                dd($testnode);

$imageExporter = new Image();
$imageContent = $imageExporter->setFormat('png')->export($testnode);
Samvdg commented 3 years ago

Alright well any other information i can give you:

And thats about all.. We;re thinking it could be symfony not liking the package?

drupol commented 3 years ago

No, it's not because of Symfony. PHPTree do not use any Symfony package, so no.

Despite that, here are my recommandations:

  1. I would suggest to use a newer version of Symfony: 4.4 LTS or 5.1
  2. Try to not use PHP 7.2, use latest PHP 7.4.11
  3. Yes, accepted values are only a string in this case.
Samvdg commented 3 years ago

Good morning! (to us atleast) We figured out by debugging that, when the export function is called it will create a tmp file without issue with the correct node information. However it breaks whenever trying to execute the shell command. We tried understanding the code you put in the image.php file but couldn't quite figure it out sadly. What we found:

Furthermore we couldn't quite figure out how the sprintf() function works with the flags and arguments but we do now know that it's the command that's causing our issue.

We also tested it with php 7.4.11 but this didn't give us any better results. We haven't tried testing it with a more recent symfony version yet but this would be our last resort since it would not be applicable with the project.

Hope this information gives more insight

drupol commented 3 years ago

Good morning! It's 10am here ;-)

Let me think, how can I help.

I don't have any Windows to test this thing, I only do tests on Windows: https://github.com/loophp/phptree/runs/1173657702?check_suite_focus=true

Did you tried to run the command in a shell (cmd.exe) on Windows ?

If yes, what are the issues ?

I can give remote assistance through Google Meet if needed, shouldn't take a lot of time.

Samvdg commented 3 years ago

Ahh it is 10am as well over here! (We're located in the Netherlands) The command the function getConvertCommand(string $path): string returns us is the following: dot.exe -Tpng C:\Users\$user\AppData\Local\Temp\phpA716.tmp

Putting this in the cmd or powershell does not work. It doesn't recognize the dot.exe part whatever we do

drupol commented 3 years ago

If you run dot.exe only, does it works?

Samvdg commented 3 years ago

no it does not

drupol commented 3 years ago

Ok, then the issue is from Windows. You should ensure that dot.exe is in the path so it can be run from anywhere. As far as I can see when querying Google, there are a lot of different ways to do that.

I suggest to add the directory where dot.exe is in the paths, see: https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/

Samvdg commented 3 years ago

We can't find an existing dot.exe anywhere? atleast not where we're looking. Any idea where it's supposed to be?

drupol commented 3 years ago

You have to install it: https://graphviz.org/download/

Samvdg commented 3 years ago

ahhaaaa okay, there's our issue. Do you perhaps know if we can just straight up install this on our server? It's a linux server and we can't run ssh commands

drupol commented 3 years ago

yes, graphviz is a very common software so there's a lot of chance that it might be available through the distribution you're using.

To install it on Ubuntu: apt-get install graphviz To install it on Gentoo: emerge media-gfx/graphviz

See more information on the download page: https://graphviz.org/download/

It should be quite easy to do.

Samvdg commented 3 years ago

Alright we're gonna see what we can do! We'll get back to you

Samvdg commented 3 years ago

Update time! Well we got a step closer, we now have an image! I downloaded the graphviz thing and made sure the path is in my env variables of my system. However the only content we receive from the export function = ‰PNG

it creates a tree.png but we cannot open this and when opened with notepad the above content is all we see

drupol commented 3 years ago

Try to change the format, try with jpg or svg ?

Samvdg commented 3 years ago

SVG works, jpg and png both have the same failure This is what ended up in the .jpg file:

ÿØÿà JFIF  ` `  ÿÛ C      



But the SVG returned a correct image. Though we kinda need the .png

drupol commented 3 years ago

Have a look at: https://graphviz.org/doc/info/output.html for supported output format.

I'm afraid but I won't help further than that, I think your issue is out of scope of PHPTree and to be honest, I don't know how to fix that on Windows.

Samvdg commented 3 years ago

alright well that's fine. Thanks for your help! (btw i checked around and couldn't find anywhere where it said we needed to install the graphviz, i reccomend you put it on the readme.md)

drupol commented 3 years ago

I will update it then, thanks!

Samvdg commented 3 years ago

Hey Drupol. sorry to bother again butttt We made the functionality work! we dynamicly can convert any nodes to any file type(if its supported by graphviz ofcourse) Basicly, we took your .dot file creator section and executed the command you commented out underneath it with shell_exec. We assume this was supposed to happen with the image->export() function but this works for us now

Now we have a different question. On the graphviz documentation we found we could do different things with the design of the graph but we don't know where we need to edit stuff to get the graph to change. We figure we need to add attributes to the .dot file but we wouldn't know where. We want the result to be a rotated tree graph from left to right instead one from top to bottom

The actual problem we're facing and trying to fix is that the graphs we're creating are too big and become compressed and unreadable. If there is something else we could do we'd love to hear it

drupol commented 3 years ago

You don't bother, it's just that I'm not going to give support to something I'm not responsible of, I also have life and some bills to pay :)

Just out of curiosity, what is the content of the command you pass to shell_exec() ? Also, what is the difference with the one used in Image.php ? Have you tried to compare?

I would maybe recommend you to use the Gv exporter then, it just produces a .dot file, and from there, you can edit it at your own will. If you want to add attributes to nodes, then it's better to read the tests to see how to have nodes with attributes: https://github.com/loophp/phptree/blob/master/spec/loophp/phptree/Exporter/GvSpec.php

Let me know if it helps.

Zt-freak commented 3 years ago

Hi Drupol, this is how we construct the command and pass it to the shell:

// Get the file type
$fileType = $_GET["fileType"];

// Create the file name and specify the location of the file that is to be created
$fileLocation = "tmp/" . $id . $fileType . "." . $fileType;

// Do the same for the dot file
$dotFileName = "tmp/" . $id . "tmp.dot";

// Graphviz export
$graphvizExporter = new Gv();
$dotScript = $graphvizExporter->export($projectNode);
file_put_contents($dotFileName, $dotScript);

// Assemble the command
$command = "dot -T". $fileType ." ".$dotFileName." -o ".$fileLocation;

// Execute the command
shell_exec($command);
die("lets see the result");
drupol commented 3 years ago

Ok I see.

The only difference is that Image.php doesn't have the -o parameter, I guess this is something I should add.

I'll do that tonight and come back at you asap.

Samvdg commented 3 years ago

sounds good! Looking through the example you sent, i see a "$this->setAttributes()" function call. Can we just call this via the Gv() object? if so, does this have any order in doing so? (like, do we have to set the attributes first before exporting it?)

drupol commented 3 years ago

Dear all,

I just published version 2.6.4, and then 2.6.5 reverting the change that I introduced in 2.6.4 regarding the parameter -o. The Image exporter export an image, meaning that it will return the content of the image file. So there is nothing to change, I just retested everything locally, and everything works.

Here's an example script that you can use to test: https://gist.github.com/drupol/67c86cf298c7635d0d15095e4de7efe5 Copy/paste it at the root of PHPTree and run it like this: php tree.php

You might need the contrib drupol/launcher: composer require drupol/launcher

This should produce a tree like this:

image

drupol commented 3 years ago

Hello,

Any news on this? Did you succeeded?

Samvdg commented 3 years ago

Hi there!

Yes and no. The server didnt have the package installed which we needed and because it's a cloud based server we can not install it on out own. However litteraly just now we found out the server has imagick(or imagemagick?) installed! So we can use a package that uses imagick instead. We found certain packages before but now actually wanted to ask you if you perhaps can recommend a package that uses imagick and works sort of the same way as this package

drupol commented 3 years ago

Ok!

And I don't know any other package that you can use, I know nothing that could replace graphviz. What we could imagine is a simple REST service where you would POST the Gv export (.dot) and it would generate a proper image (idea to dig)

Anyhow, good luck!

drupol commented 3 years ago

Here's something a bit more concrete:

Use this image in your html page:

<img src="https://dot2image.herokuapp.com/api/png/graph graphname {a -- b -- c;b -- d;}"/>

This will generate this image: https://dot2image.herokuapp.com/api/png/graph graphname {a -- b -- c;b -- d;}

You can change the format by modifying the first segment after /api/, if you want to get the same image as SVG:

https://dot2image.herokuapp.com/api/svg/graph graphname {a -- b -- c;b -- d;}

You can also use the form for testing: https://dot2image.herokuapp.com/

You can also use the POST endpoint https://dot2image.herokuapp.com/api/post

image